1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
4 * Copyright (c) 2007-2010 VMware, Inc., Palo Alto, CA., USA
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
32 #include <drm/ttm/ttm_module.h>
33 #include <drm/ttm/ttm_bo_driver.h>
34 #include <drm/ttm/ttm_placement.h>
35 #include <drm/drm_mm.h>
36 #include <linux/slab.h>
37 #include <linux/spinlock.h>
38 #include <linux/module.h>
41 * Currently we use a spinlock for the lock, but a mutex *may* be
42 * more appropriate to reduce scheduling latency if the range manager
43 * ends up with very fragmented allocation patterns.
46 struct ttm_range_manager
{
51 static int ttm_bo_man_get_node(struct ttm_mem_type_manager
*man
,
52 struct ttm_buffer_object
*bo
,
53 const struct ttm_place
*place
,
54 struct ttm_mem_reg
*mem
)
56 struct ttm_range_manager
*rman
= (struct ttm_range_manager
*) man
->priv
;
57 struct drm_mm
*mm
= &rman
->mm
;
58 struct drm_mm_node
*node
;
59 enum drm_mm_insert_mode mode
;
67 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
71 mode
= DRM_MM_INSERT_BEST
;
72 if (place
->flags
& TTM_PL_FLAG_TOPDOWN
)
73 mode
= DRM_MM_INSERT_HIGH
;
75 spin_lock(&rman
->lock
);
76 ret
= drm_mm_insert_node_in_range(mm
, node
,
78 mem
->page_alignment
, 0,
79 place
->fpfn
, lpfn
, mode
);
80 spin_unlock(&rman
->lock
);
86 mem
->start
= node
->start
;
92 static void ttm_bo_man_put_node(struct ttm_mem_type_manager
*man
,
93 struct ttm_mem_reg
*mem
)
95 struct ttm_range_manager
*rman
= (struct ttm_range_manager
*) man
->priv
;
98 spin_lock(&rman
->lock
);
99 drm_mm_remove_node(mem
->mm_node
);
100 spin_unlock(&rman
->lock
);
107 static int ttm_bo_man_init(struct ttm_mem_type_manager
*man
,
108 unsigned long p_size
)
110 struct ttm_range_manager
*rman
;
112 rman
= kzalloc(sizeof(*rman
), GFP_KERNEL
);
116 drm_mm_init(&rman
->mm
, 0, p_size
);
117 spin_lock_init(&rman
->lock
);
122 static int ttm_bo_man_takedown(struct ttm_mem_type_manager
*man
)
124 struct ttm_range_manager
*rman
= (struct ttm_range_manager
*) man
->priv
;
125 struct drm_mm
*mm
= &rman
->mm
;
127 spin_lock(&rman
->lock
);
128 if (drm_mm_clean(mm
)) {
130 spin_unlock(&rman
->lock
);
135 spin_unlock(&rman
->lock
);
139 static void ttm_bo_man_debug(struct ttm_mem_type_manager
*man
,
140 struct drm_printer
*printer
)
142 struct ttm_range_manager
*rman
= (struct ttm_range_manager
*) man
->priv
;
144 spin_lock(&rman
->lock
);
145 drm_mm_print(&rman
->mm
, printer
);
146 spin_unlock(&rman
->lock
);
149 const struct ttm_mem_type_manager_func ttm_bo_manager_func
= {
150 .init
= ttm_bo_man_init
,
151 .takedown
= ttm_bo_man_takedown
,
152 .get_node
= ttm_bo_man_get_node
,
153 .put_node
= ttm_bo_man_put_node
,
154 .debug
= ttm_bo_man_debug
156 EXPORT_SYMBOL(ttm_bo_manager_func
);