18c2ecf20Sopenharmony_ci/**************************************************************************
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * Copyright 2006-2008 Tungsten Graphics, Inc., Cedar Park, TX. USA.
48c2ecf20Sopenharmony_ci * Copyright 2016 Intel Corporation
58c2ecf20Sopenharmony_ci * All Rights Reserved.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
88c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the
98c2ecf20Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
108c2ecf20Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
118c2ecf20Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
128c2ecf20Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
138c2ecf20Sopenharmony_ci * the following conditions:
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the
168c2ecf20Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
178c2ecf20Sopenharmony_ci * of the Software.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
208c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
218c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
228c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
238c2ecf20Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
248c2ecf20Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
258c2ecf20Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci **************************************************************************/
298c2ecf20Sopenharmony_ci/*
308c2ecf20Sopenharmony_ci * Authors:
318c2ecf20Sopenharmony_ci * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#ifndef _DRM_MM_H_
358c2ecf20Sopenharmony_ci#define _DRM_MM_H_
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/*
388c2ecf20Sopenharmony_ci * Generic range manager structs
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_ci#include <linux/bug.h>
418c2ecf20Sopenharmony_ci#include <linux/rbtree.h>
428c2ecf20Sopenharmony_ci#include <linux/kernel.h>
438c2ecf20Sopenharmony_ci#include <linux/mm_types.h>
448c2ecf20Sopenharmony_ci#include <linux/list.h>
458c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
468c2ecf20Sopenharmony_ci#ifdef CONFIG_DRM_DEBUG_MM
478c2ecf20Sopenharmony_ci#include <linux/stackdepot.h>
488c2ecf20Sopenharmony_ci#endif
498c2ecf20Sopenharmony_ci#include <drm/drm_print.h>
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#ifdef CONFIG_DRM_DEBUG_MM
528c2ecf20Sopenharmony_ci#define DRM_MM_BUG_ON(expr) BUG_ON(expr)
538c2ecf20Sopenharmony_ci#else
548c2ecf20Sopenharmony_ci#define DRM_MM_BUG_ON(expr) BUILD_BUG_ON_INVALID(expr)
558c2ecf20Sopenharmony_ci#endif
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/**
588c2ecf20Sopenharmony_ci * enum drm_mm_insert_mode - control search and allocation behaviour
598c2ecf20Sopenharmony_ci *
608c2ecf20Sopenharmony_ci * The &struct drm_mm range manager supports finding a suitable modes using
618c2ecf20Sopenharmony_ci * a number of search trees. These trees are oranised by size, by address and
628c2ecf20Sopenharmony_ci * in most recent eviction order. This allows the user to find either the
638c2ecf20Sopenharmony_ci * smallest hole to reuse, the lowest or highest address to reuse, or simply
648c2ecf20Sopenharmony_ci * reuse the most recent eviction that fits. When allocating the &drm_mm_node
658c2ecf20Sopenharmony_ci * from within the hole, the &drm_mm_insert_mode also dictate whether to
668c2ecf20Sopenharmony_ci * allocate the lowest matching address or the highest.
678c2ecf20Sopenharmony_ci */
688c2ecf20Sopenharmony_cienum drm_mm_insert_mode {
698c2ecf20Sopenharmony_ci	/**
708c2ecf20Sopenharmony_ci	 * @DRM_MM_INSERT_BEST:
718c2ecf20Sopenharmony_ci	 *
728c2ecf20Sopenharmony_ci	 * Search for the smallest hole (within the search range) that fits
738c2ecf20Sopenharmony_ci	 * the desired node.
748c2ecf20Sopenharmony_ci	 *
758c2ecf20Sopenharmony_ci	 * Allocates the node from the bottom of the found hole.
768c2ecf20Sopenharmony_ci	 */
778c2ecf20Sopenharmony_ci	DRM_MM_INSERT_BEST = 0,
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	/**
808c2ecf20Sopenharmony_ci	 * @DRM_MM_INSERT_LOW:
818c2ecf20Sopenharmony_ci	 *
828c2ecf20Sopenharmony_ci	 * Search for the lowest hole (address closest to 0, within the search
838c2ecf20Sopenharmony_ci	 * range) that fits the desired node.
848c2ecf20Sopenharmony_ci	 *
858c2ecf20Sopenharmony_ci	 * Allocates the node from the bottom of the found hole.
868c2ecf20Sopenharmony_ci	 */
878c2ecf20Sopenharmony_ci	DRM_MM_INSERT_LOW,
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	/**
908c2ecf20Sopenharmony_ci	 * @DRM_MM_INSERT_HIGH:
918c2ecf20Sopenharmony_ci	 *
928c2ecf20Sopenharmony_ci	 * Search for the highest hole (address closest to U64_MAX, within the
938c2ecf20Sopenharmony_ci	 * search range) that fits the desired node.
948c2ecf20Sopenharmony_ci	 *
958c2ecf20Sopenharmony_ci	 * Allocates the node from the *top* of the found hole. The specified
968c2ecf20Sopenharmony_ci	 * alignment for the node is applied to the base of the node
978c2ecf20Sopenharmony_ci	 * (&drm_mm_node.start).
988c2ecf20Sopenharmony_ci	 */
998c2ecf20Sopenharmony_ci	DRM_MM_INSERT_HIGH,
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	/**
1028c2ecf20Sopenharmony_ci	 * @DRM_MM_INSERT_EVICT:
1038c2ecf20Sopenharmony_ci	 *
1048c2ecf20Sopenharmony_ci	 * Search for the most recently evicted hole (within the search range)
1058c2ecf20Sopenharmony_ci	 * that fits the desired node. This is appropriate for use immediately
1068c2ecf20Sopenharmony_ci	 * after performing an eviction scan (see drm_mm_scan_init()) and
1078c2ecf20Sopenharmony_ci	 * removing the selected nodes to form a hole.
1088c2ecf20Sopenharmony_ci	 *
1098c2ecf20Sopenharmony_ci	 * Allocates the node from the bottom of the found hole.
1108c2ecf20Sopenharmony_ci	 */
1118c2ecf20Sopenharmony_ci	DRM_MM_INSERT_EVICT,
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	/**
1148c2ecf20Sopenharmony_ci	 * @DRM_MM_INSERT_ONCE:
1158c2ecf20Sopenharmony_ci	 *
1168c2ecf20Sopenharmony_ci	 * Only check the first hole for suitablity and report -ENOSPC
1178c2ecf20Sopenharmony_ci	 * immediately otherwise, rather than check every hole until a
1188c2ecf20Sopenharmony_ci	 * suitable one is found. Can only be used in conjunction with another
1198c2ecf20Sopenharmony_ci	 * search method such as DRM_MM_INSERT_HIGH or DRM_MM_INSERT_LOW.
1208c2ecf20Sopenharmony_ci	 */
1218c2ecf20Sopenharmony_ci	DRM_MM_INSERT_ONCE = BIT(31),
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	/**
1248c2ecf20Sopenharmony_ci	 * @DRM_MM_INSERT_HIGHEST:
1258c2ecf20Sopenharmony_ci	 *
1268c2ecf20Sopenharmony_ci	 * Only check the highest hole (the hole with the largest address) and
1278c2ecf20Sopenharmony_ci	 * insert the node at the top of the hole or report -ENOSPC if
1288c2ecf20Sopenharmony_ci	 * unsuitable.
1298c2ecf20Sopenharmony_ci	 *
1308c2ecf20Sopenharmony_ci	 * Does not search all holes.
1318c2ecf20Sopenharmony_ci	 */
1328c2ecf20Sopenharmony_ci	DRM_MM_INSERT_HIGHEST = DRM_MM_INSERT_HIGH | DRM_MM_INSERT_ONCE,
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	/**
1358c2ecf20Sopenharmony_ci	 * @DRM_MM_INSERT_LOWEST:
1368c2ecf20Sopenharmony_ci	 *
1378c2ecf20Sopenharmony_ci	 * Only check the lowest hole (the hole with the smallest address) and
1388c2ecf20Sopenharmony_ci	 * insert the node at the bottom of the hole or report -ENOSPC if
1398c2ecf20Sopenharmony_ci	 * unsuitable.
1408c2ecf20Sopenharmony_ci	 *
1418c2ecf20Sopenharmony_ci	 * Does not search all holes.
1428c2ecf20Sopenharmony_ci	 */
1438c2ecf20Sopenharmony_ci	DRM_MM_INSERT_LOWEST  = DRM_MM_INSERT_LOW | DRM_MM_INSERT_ONCE,
1448c2ecf20Sopenharmony_ci};
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci/**
1478c2ecf20Sopenharmony_ci * struct drm_mm_node - allocated block in the DRM allocator
1488c2ecf20Sopenharmony_ci *
1498c2ecf20Sopenharmony_ci * This represents an allocated block in a &drm_mm allocator. Except for
1508c2ecf20Sopenharmony_ci * pre-reserved nodes inserted using drm_mm_reserve_node() the structure is
1518c2ecf20Sopenharmony_ci * entirely opaque and should only be accessed through the provided funcions.
1528c2ecf20Sopenharmony_ci * Since allocation of these nodes is entirely handled by the driver they can be
1538c2ecf20Sopenharmony_ci * embedded.
1548c2ecf20Sopenharmony_ci */
1558c2ecf20Sopenharmony_cistruct drm_mm_node {
1568c2ecf20Sopenharmony_ci	/** @color: Opaque driver-private tag. */
1578c2ecf20Sopenharmony_ci	unsigned long color;
1588c2ecf20Sopenharmony_ci	/** @start: Start address of the allocated block. */
1598c2ecf20Sopenharmony_ci	u64 start;
1608c2ecf20Sopenharmony_ci	/** @size: Size of the allocated block. */
1618c2ecf20Sopenharmony_ci	u64 size;
1628c2ecf20Sopenharmony_ci	/* private: */
1638c2ecf20Sopenharmony_ci	struct drm_mm *mm;
1648c2ecf20Sopenharmony_ci	struct list_head node_list;
1658c2ecf20Sopenharmony_ci	struct list_head hole_stack;
1668c2ecf20Sopenharmony_ci	struct rb_node rb;
1678c2ecf20Sopenharmony_ci	struct rb_node rb_hole_size;
1688c2ecf20Sopenharmony_ci	struct rb_node rb_hole_addr;
1698c2ecf20Sopenharmony_ci	u64 __subtree_last;
1708c2ecf20Sopenharmony_ci	u64 hole_size;
1718c2ecf20Sopenharmony_ci	u64 subtree_max_hole;
1728c2ecf20Sopenharmony_ci	unsigned long flags;
1738c2ecf20Sopenharmony_ci#define DRM_MM_NODE_ALLOCATED_BIT	0
1748c2ecf20Sopenharmony_ci#define DRM_MM_NODE_SCANNED_BIT		1
1758c2ecf20Sopenharmony_ci#ifdef CONFIG_DRM_DEBUG_MM
1768c2ecf20Sopenharmony_ci	depot_stack_handle_t stack;
1778c2ecf20Sopenharmony_ci#endif
1788c2ecf20Sopenharmony_ci};
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci/**
1818c2ecf20Sopenharmony_ci * struct drm_mm - DRM allocator
1828c2ecf20Sopenharmony_ci *
1838c2ecf20Sopenharmony_ci * DRM range allocator with a few special functions and features geared towards
1848c2ecf20Sopenharmony_ci * managing GPU memory. Except for the @color_adjust callback the structure is
1858c2ecf20Sopenharmony_ci * entirely opaque and should only be accessed through the provided functions
1868c2ecf20Sopenharmony_ci * and macros. This structure can be embedded into larger driver structures.
1878c2ecf20Sopenharmony_ci */
1888c2ecf20Sopenharmony_cistruct drm_mm {
1898c2ecf20Sopenharmony_ci	/**
1908c2ecf20Sopenharmony_ci	 * @color_adjust:
1918c2ecf20Sopenharmony_ci	 *
1928c2ecf20Sopenharmony_ci	 * Optional driver callback to further apply restrictions on a hole. The
1938c2ecf20Sopenharmony_ci	 * node argument points at the node containing the hole from which the
1948c2ecf20Sopenharmony_ci	 * block would be allocated (see drm_mm_hole_follows() and friends). The
1958c2ecf20Sopenharmony_ci	 * other arguments are the size of the block to be allocated. The driver
1968c2ecf20Sopenharmony_ci	 * can adjust the start and end as needed to e.g. insert guard pages.
1978c2ecf20Sopenharmony_ci	 */
1988c2ecf20Sopenharmony_ci	void (*color_adjust)(const struct drm_mm_node *node,
1998c2ecf20Sopenharmony_ci			     unsigned long color,
2008c2ecf20Sopenharmony_ci			     u64 *start, u64 *end);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	/* private: */
2038c2ecf20Sopenharmony_ci	/* List of all memory nodes that immediately precede a free hole. */
2048c2ecf20Sopenharmony_ci	struct list_head hole_stack;
2058c2ecf20Sopenharmony_ci	/* head_node.node_list is the list of all memory nodes, ordered
2068c2ecf20Sopenharmony_ci	 * according to the (increasing) start address of the memory node. */
2078c2ecf20Sopenharmony_ci	struct drm_mm_node head_node;
2088c2ecf20Sopenharmony_ci	/* Keep an interval_tree for fast lookup of drm_mm_nodes by address. */
2098c2ecf20Sopenharmony_ci	struct rb_root_cached interval_tree;
2108c2ecf20Sopenharmony_ci	struct rb_root_cached holes_size;
2118c2ecf20Sopenharmony_ci	struct rb_root holes_addr;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	unsigned long scan_active;
2148c2ecf20Sopenharmony_ci};
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci/**
2178c2ecf20Sopenharmony_ci * struct drm_mm_scan - DRM allocator eviction roaster data
2188c2ecf20Sopenharmony_ci *
2198c2ecf20Sopenharmony_ci * This structure tracks data needed for the eviction roaster set up using
2208c2ecf20Sopenharmony_ci * drm_mm_scan_init(), and used with drm_mm_scan_add_block() and
2218c2ecf20Sopenharmony_ci * drm_mm_scan_remove_block(). The structure is entirely opaque and should only
2228c2ecf20Sopenharmony_ci * be accessed through the provided functions and macros. It is meant to be
2238c2ecf20Sopenharmony_ci * allocated temporarily by the driver on the stack.
2248c2ecf20Sopenharmony_ci */
2258c2ecf20Sopenharmony_cistruct drm_mm_scan {
2268c2ecf20Sopenharmony_ci	/* private: */
2278c2ecf20Sopenharmony_ci	struct drm_mm *mm;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	u64 size;
2308c2ecf20Sopenharmony_ci	u64 alignment;
2318c2ecf20Sopenharmony_ci	u64 remainder_mask;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	u64 range_start;
2348c2ecf20Sopenharmony_ci	u64 range_end;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	u64 hit_start;
2378c2ecf20Sopenharmony_ci	u64 hit_end;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	unsigned long color;
2408c2ecf20Sopenharmony_ci	enum drm_mm_insert_mode mode;
2418c2ecf20Sopenharmony_ci};
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci/**
2448c2ecf20Sopenharmony_ci * drm_mm_node_allocated - checks whether a node is allocated
2458c2ecf20Sopenharmony_ci * @node: drm_mm_node to check
2468c2ecf20Sopenharmony_ci *
2478c2ecf20Sopenharmony_ci * Drivers are required to clear a node prior to using it with the
2488c2ecf20Sopenharmony_ci * drm_mm range manager.
2498c2ecf20Sopenharmony_ci *
2508c2ecf20Sopenharmony_ci * Drivers should use this helper for proper encapsulation of drm_mm
2518c2ecf20Sopenharmony_ci * internals.
2528c2ecf20Sopenharmony_ci *
2538c2ecf20Sopenharmony_ci * Returns:
2548c2ecf20Sopenharmony_ci * True if the @node is allocated.
2558c2ecf20Sopenharmony_ci */
2568c2ecf20Sopenharmony_cistatic inline bool drm_mm_node_allocated(const struct drm_mm_node *node)
2578c2ecf20Sopenharmony_ci{
2588c2ecf20Sopenharmony_ci	return test_bit(DRM_MM_NODE_ALLOCATED_BIT, &node->flags);
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci/**
2628c2ecf20Sopenharmony_ci * drm_mm_initialized - checks whether an allocator is initialized
2638c2ecf20Sopenharmony_ci * @mm: drm_mm to check
2648c2ecf20Sopenharmony_ci *
2658c2ecf20Sopenharmony_ci * Drivers should clear the struct drm_mm prior to initialisation if they
2668c2ecf20Sopenharmony_ci * want to use this function.
2678c2ecf20Sopenharmony_ci *
2688c2ecf20Sopenharmony_ci * Drivers should use this helper for proper encapsulation of drm_mm
2698c2ecf20Sopenharmony_ci * internals.
2708c2ecf20Sopenharmony_ci *
2718c2ecf20Sopenharmony_ci * Returns:
2728c2ecf20Sopenharmony_ci * True if the @mm is initialized.
2738c2ecf20Sopenharmony_ci */
2748c2ecf20Sopenharmony_cistatic inline bool drm_mm_initialized(const struct drm_mm *mm)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	return READ_ONCE(mm->hole_stack.next);
2778c2ecf20Sopenharmony_ci}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci/**
2808c2ecf20Sopenharmony_ci * drm_mm_hole_follows - checks whether a hole follows this node
2818c2ecf20Sopenharmony_ci * @node: drm_mm_node to check
2828c2ecf20Sopenharmony_ci *
2838c2ecf20Sopenharmony_ci * Holes are embedded into the drm_mm using the tail of a drm_mm_node.
2848c2ecf20Sopenharmony_ci * If you wish to know whether a hole follows this particular node,
2858c2ecf20Sopenharmony_ci * query this function. See also drm_mm_hole_node_start() and
2868c2ecf20Sopenharmony_ci * drm_mm_hole_node_end().
2878c2ecf20Sopenharmony_ci *
2888c2ecf20Sopenharmony_ci * Returns:
2898c2ecf20Sopenharmony_ci * True if a hole follows the @node.
2908c2ecf20Sopenharmony_ci */
2918c2ecf20Sopenharmony_cistatic inline bool drm_mm_hole_follows(const struct drm_mm_node *node)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	return node->hole_size;
2948c2ecf20Sopenharmony_ci}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic inline u64 __drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
2978c2ecf20Sopenharmony_ci{
2988c2ecf20Sopenharmony_ci	return hole_node->start + hole_node->size;
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci/**
3028c2ecf20Sopenharmony_ci * drm_mm_hole_node_start - computes the start of the hole following @node
3038c2ecf20Sopenharmony_ci * @hole_node: drm_mm_node which implicitly tracks the following hole
3048c2ecf20Sopenharmony_ci *
3058c2ecf20Sopenharmony_ci * This is useful for driver-specific debug dumpers. Otherwise drivers should
3068c2ecf20Sopenharmony_ci * not inspect holes themselves. Drivers must check first whether a hole indeed
3078c2ecf20Sopenharmony_ci * follows by looking at drm_mm_hole_follows()
3088c2ecf20Sopenharmony_ci *
3098c2ecf20Sopenharmony_ci * Returns:
3108c2ecf20Sopenharmony_ci * Start of the subsequent hole.
3118c2ecf20Sopenharmony_ci */
3128c2ecf20Sopenharmony_cistatic inline u64 drm_mm_hole_node_start(const struct drm_mm_node *hole_node)
3138c2ecf20Sopenharmony_ci{
3148c2ecf20Sopenharmony_ci	DRM_MM_BUG_ON(!drm_mm_hole_follows(hole_node));
3158c2ecf20Sopenharmony_ci	return __drm_mm_hole_node_start(hole_node);
3168c2ecf20Sopenharmony_ci}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_cistatic inline u64 __drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	return list_next_entry(hole_node, node_list)->start;
3218c2ecf20Sopenharmony_ci}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci/**
3248c2ecf20Sopenharmony_ci * drm_mm_hole_node_end - computes the end of the hole following @node
3258c2ecf20Sopenharmony_ci * @hole_node: drm_mm_node which implicitly tracks the following hole
3268c2ecf20Sopenharmony_ci *
3278c2ecf20Sopenharmony_ci * This is useful for driver-specific debug dumpers. Otherwise drivers should
3288c2ecf20Sopenharmony_ci * not inspect holes themselves. Drivers must check first whether a hole indeed
3298c2ecf20Sopenharmony_ci * follows by looking at drm_mm_hole_follows().
3308c2ecf20Sopenharmony_ci *
3318c2ecf20Sopenharmony_ci * Returns:
3328c2ecf20Sopenharmony_ci * End of the subsequent hole.
3338c2ecf20Sopenharmony_ci */
3348c2ecf20Sopenharmony_cistatic inline u64 drm_mm_hole_node_end(const struct drm_mm_node *hole_node)
3358c2ecf20Sopenharmony_ci{
3368c2ecf20Sopenharmony_ci	return __drm_mm_hole_node_end(hole_node);
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci/**
3408c2ecf20Sopenharmony_ci * drm_mm_nodes - list of nodes under the drm_mm range manager
3418c2ecf20Sopenharmony_ci * @mm: the struct drm_mm range manager
3428c2ecf20Sopenharmony_ci *
3438c2ecf20Sopenharmony_ci * As the drm_mm range manager hides its node_list deep with its
3448c2ecf20Sopenharmony_ci * structure, extracting it looks painful and repetitive. This is
3458c2ecf20Sopenharmony_ci * not expected to be used outside of the drm_mm_for_each_node()
3468c2ecf20Sopenharmony_ci * macros and similar internal functions.
3478c2ecf20Sopenharmony_ci *
3488c2ecf20Sopenharmony_ci * Returns:
3498c2ecf20Sopenharmony_ci * The node list, may be empty.
3508c2ecf20Sopenharmony_ci */
3518c2ecf20Sopenharmony_ci#define drm_mm_nodes(mm) (&(mm)->head_node.node_list)
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci/**
3548c2ecf20Sopenharmony_ci * drm_mm_for_each_node - iterator to walk over all allocated nodes
3558c2ecf20Sopenharmony_ci * @entry: &struct drm_mm_node to assign to in each iteration step
3568c2ecf20Sopenharmony_ci * @mm: &drm_mm allocator to walk
3578c2ecf20Sopenharmony_ci *
3588c2ecf20Sopenharmony_ci * This iterator walks over all nodes in the range allocator. It is implemented
3598c2ecf20Sopenharmony_ci * with list_for_each(), so not save against removal of elements.
3608c2ecf20Sopenharmony_ci */
3618c2ecf20Sopenharmony_ci#define drm_mm_for_each_node(entry, mm) \
3628c2ecf20Sopenharmony_ci	list_for_each_entry(entry, drm_mm_nodes(mm), node_list)
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci/**
3658c2ecf20Sopenharmony_ci * drm_mm_for_each_node_safe - iterator to walk over all allocated nodes
3668c2ecf20Sopenharmony_ci * @entry: &struct drm_mm_node to assign to in each iteration step
3678c2ecf20Sopenharmony_ci * @next: &struct drm_mm_node to store the next step
3688c2ecf20Sopenharmony_ci * @mm: &drm_mm allocator to walk
3698c2ecf20Sopenharmony_ci *
3708c2ecf20Sopenharmony_ci * This iterator walks over all nodes in the range allocator. It is implemented
3718c2ecf20Sopenharmony_ci * with list_for_each_safe(), so save against removal of elements.
3728c2ecf20Sopenharmony_ci */
3738c2ecf20Sopenharmony_ci#define drm_mm_for_each_node_safe(entry, next, mm) \
3748c2ecf20Sopenharmony_ci	list_for_each_entry_safe(entry, next, drm_mm_nodes(mm), node_list)
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci/**
3778c2ecf20Sopenharmony_ci * drm_mm_for_each_hole - iterator to walk over all holes
3788c2ecf20Sopenharmony_ci * @pos: &drm_mm_node used internally to track progress
3798c2ecf20Sopenharmony_ci * @mm: &drm_mm allocator to walk
3808c2ecf20Sopenharmony_ci * @hole_start: ulong variable to assign the hole start to on each iteration
3818c2ecf20Sopenharmony_ci * @hole_end: ulong variable to assign the hole end to on each iteration
3828c2ecf20Sopenharmony_ci *
3838c2ecf20Sopenharmony_ci * This iterator walks over all holes in the range allocator. It is implemented
3848c2ecf20Sopenharmony_ci * with list_for_each(), so not save against removal of elements. @entry is used
3858c2ecf20Sopenharmony_ci * internally and will not reflect a real drm_mm_node for the very first hole.
3868c2ecf20Sopenharmony_ci * Hence users of this iterator may not access it.
3878c2ecf20Sopenharmony_ci *
3888c2ecf20Sopenharmony_ci * Implementation Note:
3898c2ecf20Sopenharmony_ci * We need to inline list_for_each_entry in order to be able to set hole_start
3908c2ecf20Sopenharmony_ci * and hole_end on each iteration while keeping the macro sane.
3918c2ecf20Sopenharmony_ci */
3928c2ecf20Sopenharmony_ci#define drm_mm_for_each_hole(pos, mm, hole_start, hole_end) \
3938c2ecf20Sopenharmony_ci	for (pos = list_first_entry(&(mm)->hole_stack, \
3948c2ecf20Sopenharmony_ci				    typeof(*pos), hole_stack); \
3958c2ecf20Sopenharmony_ci	     &pos->hole_stack != &(mm)->hole_stack ? \
3968c2ecf20Sopenharmony_ci	     hole_start = drm_mm_hole_node_start(pos), \
3978c2ecf20Sopenharmony_ci	     hole_end = hole_start + pos->hole_size, \
3988c2ecf20Sopenharmony_ci	     1 : 0; \
3998c2ecf20Sopenharmony_ci	     pos = list_next_entry(pos, hole_stack))
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci/*
4028c2ecf20Sopenharmony_ci * Basic range manager support (drm_mm.c)
4038c2ecf20Sopenharmony_ci */
4048c2ecf20Sopenharmony_ciint drm_mm_reserve_node(struct drm_mm *mm, struct drm_mm_node *node);
4058c2ecf20Sopenharmony_ciint drm_mm_insert_node_in_range(struct drm_mm *mm,
4068c2ecf20Sopenharmony_ci				struct drm_mm_node *node,
4078c2ecf20Sopenharmony_ci				u64 size,
4088c2ecf20Sopenharmony_ci				u64 alignment,
4098c2ecf20Sopenharmony_ci				unsigned long color,
4108c2ecf20Sopenharmony_ci				u64 start,
4118c2ecf20Sopenharmony_ci				u64 end,
4128c2ecf20Sopenharmony_ci				enum drm_mm_insert_mode mode);
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci/**
4158c2ecf20Sopenharmony_ci * drm_mm_insert_node_generic - search for space and insert @node
4168c2ecf20Sopenharmony_ci * @mm: drm_mm to allocate from
4178c2ecf20Sopenharmony_ci * @node: preallocate node to insert
4188c2ecf20Sopenharmony_ci * @size: size of the allocation
4198c2ecf20Sopenharmony_ci * @alignment: alignment of the allocation
4208c2ecf20Sopenharmony_ci * @color: opaque tag value to use for this node
4218c2ecf20Sopenharmony_ci * @mode: fine-tune the allocation search and placement
4228c2ecf20Sopenharmony_ci *
4238c2ecf20Sopenharmony_ci * This is a simplified version of drm_mm_insert_node_in_range() with no
4248c2ecf20Sopenharmony_ci * range restrictions applied.
4258c2ecf20Sopenharmony_ci *
4268c2ecf20Sopenharmony_ci * The preallocated node must be cleared to 0.
4278c2ecf20Sopenharmony_ci *
4288c2ecf20Sopenharmony_ci * Returns:
4298c2ecf20Sopenharmony_ci * 0 on success, -ENOSPC if there's no suitable hole.
4308c2ecf20Sopenharmony_ci */
4318c2ecf20Sopenharmony_cistatic inline int
4328c2ecf20Sopenharmony_cidrm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node,
4338c2ecf20Sopenharmony_ci			   u64 size, u64 alignment,
4348c2ecf20Sopenharmony_ci			   unsigned long color,
4358c2ecf20Sopenharmony_ci			   enum drm_mm_insert_mode mode)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci	return drm_mm_insert_node_in_range(mm, node,
4388c2ecf20Sopenharmony_ci					   size, alignment, color,
4398c2ecf20Sopenharmony_ci					   0, U64_MAX, mode);
4408c2ecf20Sopenharmony_ci}
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci/**
4438c2ecf20Sopenharmony_ci * drm_mm_insert_node - search for space and insert @node
4448c2ecf20Sopenharmony_ci * @mm: drm_mm to allocate from
4458c2ecf20Sopenharmony_ci * @node: preallocate node to insert
4468c2ecf20Sopenharmony_ci * @size: size of the allocation
4478c2ecf20Sopenharmony_ci *
4488c2ecf20Sopenharmony_ci * This is a simplified version of drm_mm_insert_node_generic() with @color set
4498c2ecf20Sopenharmony_ci * to 0.
4508c2ecf20Sopenharmony_ci *
4518c2ecf20Sopenharmony_ci * The preallocated node must be cleared to 0.
4528c2ecf20Sopenharmony_ci *
4538c2ecf20Sopenharmony_ci * Returns:
4548c2ecf20Sopenharmony_ci * 0 on success, -ENOSPC if there's no suitable hole.
4558c2ecf20Sopenharmony_ci */
4568c2ecf20Sopenharmony_cistatic inline int drm_mm_insert_node(struct drm_mm *mm,
4578c2ecf20Sopenharmony_ci				     struct drm_mm_node *node,
4588c2ecf20Sopenharmony_ci				     u64 size)
4598c2ecf20Sopenharmony_ci{
4608c2ecf20Sopenharmony_ci	return drm_mm_insert_node_generic(mm, node, size, 0, 0, 0);
4618c2ecf20Sopenharmony_ci}
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_civoid drm_mm_remove_node(struct drm_mm_node *node);
4648c2ecf20Sopenharmony_civoid drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new);
4658c2ecf20Sopenharmony_civoid drm_mm_init(struct drm_mm *mm, u64 start, u64 size);
4668c2ecf20Sopenharmony_civoid drm_mm_takedown(struct drm_mm *mm);
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci/**
4698c2ecf20Sopenharmony_ci * drm_mm_clean - checks whether an allocator is clean
4708c2ecf20Sopenharmony_ci * @mm: drm_mm allocator to check
4718c2ecf20Sopenharmony_ci *
4728c2ecf20Sopenharmony_ci * Returns:
4738c2ecf20Sopenharmony_ci * True if the allocator is completely free, false if there's still a node
4748c2ecf20Sopenharmony_ci * allocated in it.
4758c2ecf20Sopenharmony_ci */
4768c2ecf20Sopenharmony_cistatic inline bool drm_mm_clean(const struct drm_mm *mm)
4778c2ecf20Sopenharmony_ci{
4788c2ecf20Sopenharmony_ci	return list_empty(drm_mm_nodes(mm));
4798c2ecf20Sopenharmony_ci}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_cistruct drm_mm_node *
4828c2ecf20Sopenharmony_ci__drm_mm_interval_first(const struct drm_mm *mm, u64 start, u64 last);
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci/**
4858c2ecf20Sopenharmony_ci * drm_mm_for_each_node_in_range - iterator to walk over a range of
4868c2ecf20Sopenharmony_ci * allocated nodes
4878c2ecf20Sopenharmony_ci * @node__: drm_mm_node structure to assign to in each iteration step
4888c2ecf20Sopenharmony_ci * @mm__: drm_mm allocator to walk
4898c2ecf20Sopenharmony_ci * @start__: starting offset, the first node will overlap this
4908c2ecf20Sopenharmony_ci * @end__: ending offset, the last node will start before this (but may overlap)
4918c2ecf20Sopenharmony_ci *
4928c2ecf20Sopenharmony_ci * This iterator walks over all nodes in the range allocator that lie
4938c2ecf20Sopenharmony_ci * between @start and @end. It is implemented similarly to list_for_each(),
4948c2ecf20Sopenharmony_ci * but using the internal interval tree to accelerate the search for the
4958c2ecf20Sopenharmony_ci * starting node, and so not safe against removal of elements. It assumes
4968c2ecf20Sopenharmony_ci * that @end is within (or is the upper limit of) the drm_mm allocator.
4978c2ecf20Sopenharmony_ci * If [@start, @end] are beyond the range of the drm_mm, the iterator may walk
4988c2ecf20Sopenharmony_ci * over the special _unallocated_ &drm_mm.head_node, and may even continue
4998c2ecf20Sopenharmony_ci * indefinitely.
5008c2ecf20Sopenharmony_ci */
5018c2ecf20Sopenharmony_ci#define drm_mm_for_each_node_in_range(node__, mm__, start__, end__)	\
5028c2ecf20Sopenharmony_ci	for (node__ = __drm_mm_interval_first((mm__), (start__), (end__)-1); \
5038c2ecf20Sopenharmony_ci	     node__->start < (end__);					\
5048c2ecf20Sopenharmony_ci	     node__ = list_next_entry(node__, node_list))
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_civoid drm_mm_scan_init_with_range(struct drm_mm_scan *scan,
5078c2ecf20Sopenharmony_ci				 struct drm_mm *mm,
5088c2ecf20Sopenharmony_ci				 u64 size, u64 alignment, unsigned long color,
5098c2ecf20Sopenharmony_ci				 u64 start, u64 end,
5108c2ecf20Sopenharmony_ci				 enum drm_mm_insert_mode mode);
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci/**
5138c2ecf20Sopenharmony_ci * drm_mm_scan_init - initialize lru scanning
5148c2ecf20Sopenharmony_ci * @scan: scan state
5158c2ecf20Sopenharmony_ci * @mm: drm_mm to scan
5168c2ecf20Sopenharmony_ci * @size: size of the allocation
5178c2ecf20Sopenharmony_ci * @alignment: alignment of the allocation
5188c2ecf20Sopenharmony_ci * @color: opaque tag value to use for the allocation
5198c2ecf20Sopenharmony_ci * @mode: fine-tune the allocation search and placement
5208c2ecf20Sopenharmony_ci *
5218c2ecf20Sopenharmony_ci * This is a simplified version of drm_mm_scan_init_with_range() with no range
5228c2ecf20Sopenharmony_ci * restrictions applied.
5238c2ecf20Sopenharmony_ci *
5248c2ecf20Sopenharmony_ci * This simply sets up the scanning routines with the parameters for the desired
5258c2ecf20Sopenharmony_ci * hole.
5268c2ecf20Sopenharmony_ci *
5278c2ecf20Sopenharmony_ci * Warning:
5288c2ecf20Sopenharmony_ci * As long as the scan list is non-empty, no other operations than
5298c2ecf20Sopenharmony_ci * adding/removing nodes to/from the scan list are allowed.
5308c2ecf20Sopenharmony_ci */
5318c2ecf20Sopenharmony_cistatic inline void drm_mm_scan_init(struct drm_mm_scan *scan,
5328c2ecf20Sopenharmony_ci				    struct drm_mm *mm,
5338c2ecf20Sopenharmony_ci				    u64 size,
5348c2ecf20Sopenharmony_ci				    u64 alignment,
5358c2ecf20Sopenharmony_ci				    unsigned long color,
5368c2ecf20Sopenharmony_ci				    enum drm_mm_insert_mode mode)
5378c2ecf20Sopenharmony_ci{
5388c2ecf20Sopenharmony_ci	drm_mm_scan_init_with_range(scan, mm,
5398c2ecf20Sopenharmony_ci				    size, alignment, color,
5408c2ecf20Sopenharmony_ci				    0, U64_MAX, mode);
5418c2ecf20Sopenharmony_ci}
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_cibool drm_mm_scan_add_block(struct drm_mm_scan *scan,
5448c2ecf20Sopenharmony_ci			   struct drm_mm_node *node);
5458c2ecf20Sopenharmony_cibool drm_mm_scan_remove_block(struct drm_mm_scan *scan,
5468c2ecf20Sopenharmony_ci			      struct drm_mm_node *node);
5478c2ecf20Sopenharmony_cistruct drm_mm_node *drm_mm_scan_color_evict(struct drm_mm_scan *scan);
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_civoid drm_mm_print(const struct drm_mm *mm, struct drm_printer *p);
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci#endif
552