1 /**************************************************************************
3 * Copyright (c) 2007-2010 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31 #include <drm/ttm/ttm_module.h>
32 #include <drm/ttm/ttm_bo_driver.h>
33 #include <drm/ttm/ttm_placement.h>
34 #include <drm/drm_mm.h>
35 #include <linux/slab.h>
36 #include <linux/spinlock.h>
37 #include <linux/module.h>
40 * Currently we use a spinlock for the lock, but a mutex *may* be
41 * more appropriate to reduce scheduling latency if the range manager
42 * ends up with very fragmented allocation patterns.
45 struct ttm_range_manager
{
50 static int ttm_bo_man_get_node(struct ttm_mem_type_manager
*man
,
51 struct ttm_buffer_object
*bo
,
52 const struct ttm_place
*place
,
53 struct ttm_mem_reg
*mem
)
55 struct ttm_range_manager
*rman
= (struct ttm_range_manager
*) man
->priv
;
56 struct drm_mm
*mm
= &rman
->mm
;
57 struct drm_mm_node
*node
= NULL
;
58 enum drm_mm_search_flags sflags
= DRM_MM_SEARCH_BEST
;
59 enum drm_mm_allocator_flags aflags
= DRM_MM_CREATE_DEFAULT
;
67 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
71 if (place
->flags
& TTM_PL_FLAG_TOPDOWN
) {
72 sflags
= DRM_MM_SEARCH_BELOW
;
73 aflags
= DRM_MM_CREATE_TOP
;
76 spin_lock(&rman
->lock
);
77 ret
= drm_mm_insert_node_in_range_generic(mm
, node
, mem
->num_pages
,
78 mem
->page_alignment
, 0,
81 spin_unlock(&rman
->lock
);
87 mem
->start
= node
->start
;
93 static void ttm_bo_man_put_node(struct ttm_mem_type_manager
*man
,
94 struct ttm_mem_reg
*mem
)
96 struct ttm_range_manager
*rman
= (struct ttm_range_manager
*) man
->priv
;
99 spin_lock(&rman
->lock
);
100 drm_mm_remove_node(mem
->mm_node
);
101 spin_unlock(&rman
->lock
);
108 static int ttm_bo_man_init(struct ttm_mem_type_manager
*man
,
109 unsigned long p_size
)
111 struct ttm_range_manager
*rman
;
113 rman
= kzalloc(sizeof(*rman
), GFP_KERNEL
);
117 drm_mm_init(&rman
->mm
, 0, p_size
);
118 spin_lock_init(&rman
->lock
);
123 static int ttm_bo_man_takedown(struct ttm_mem_type_manager
*man
)
125 struct ttm_range_manager
*rman
= (struct ttm_range_manager
*) man
->priv
;
126 struct drm_mm
*mm
= &rman
->mm
;
128 spin_lock(&rman
->lock
);
129 if (drm_mm_clean(mm
)) {
131 spin_unlock(&rman
->lock
);
136 spin_unlock(&rman
->lock
);
140 static void ttm_bo_man_debug(struct ttm_mem_type_manager
*man
,
143 struct ttm_range_manager
*rman
= (struct ttm_range_manager
*) man
->priv
;
144 struct drm_printer p
= drm_debug_printer(prefix
);
146 spin_lock(&rman
->lock
);
147 drm_mm_print(&rman
->mm
, &p
);
148 spin_unlock(&rman
->lock
);
151 const struct ttm_mem_type_manager_func ttm_bo_manager_func
= {
152 .init
= ttm_bo_man_init
,
153 .takedown
= ttm_bo_man_takedown
,
154 .get_node
= ttm_bo_man_get_node
,
155 .put_node
= ttm_bo_man_put_node
,
156 .debug
= ttm_bo_man_debug
158 EXPORT_SYMBOL(ttm_bo_manager_func
);