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
{
47 struct ttm_resource_manager manager
;
52 static inline struct ttm_range_manager
*to_range_manager(struct ttm_resource_manager
*man
)
54 return container_of(man
, struct ttm_range_manager
, manager
);
57 static int ttm_range_man_alloc(struct ttm_resource_manager
*man
,
58 struct ttm_buffer_object
*bo
,
59 const struct ttm_place
*place
,
60 struct ttm_resource
*mem
)
62 struct ttm_range_manager
*rman
= to_range_manager(man
);
63 struct drm_mm
*mm
= &rman
->mm
;
64 struct drm_mm_node
*node
;
65 enum drm_mm_insert_mode mode
;
73 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
77 mode
= DRM_MM_INSERT_BEST
;
78 if (place
->flags
& TTM_PL_FLAG_TOPDOWN
)
79 mode
= DRM_MM_INSERT_HIGH
;
81 spin_lock(&rman
->lock
);
82 ret
= drm_mm_insert_node_in_range(mm
, node
,
84 mem
->page_alignment
, 0,
85 place
->fpfn
, lpfn
, mode
);
86 spin_unlock(&rman
->lock
);
92 mem
->start
= node
->start
;
98 static void ttm_range_man_free(struct ttm_resource_manager
*man
,
99 struct ttm_resource
*mem
)
101 struct ttm_range_manager
*rman
= to_range_manager(man
);
104 spin_lock(&rman
->lock
);
105 drm_mm_remove_node(mem
->mm_node
);
106 spin_unlock(&rman
->lock
);
113 static const struct ttm_resource_manager_func ttm_range_manager_func
;
115 int ttm_range_man_init(struct ttm_bo_device
*bdev
,
116 unsigned type
, bool use_tt
,
117 unsigned long p_size
)
119 struct ttm_resource_manager
*man
;
120 struct ttm_range_manager
*rman
;
122 rman
= kzalloc(sizeof(*rman
), GFP_KERNEL
);
126 man
= &rman
->manager
;
127 man
->use_tt
= use_tt
;
129 man
->func
= &ttm_range_manager_func
;
131 ttm_resource_manager_init(man
, p_size
);
133 drm_mm_init(&rman
->mm
, 0, p_size
);
134 spin_lock_init(&rman
->lock
);
136 ttm_set_driver_manager(bdev
, type
, &rman
->manager
);
137 ttm_resource_manager_set_used(man
, true);
140 EXPORT_SYMBOL(ttm_range_man_init
);
142 int ttm_range_man_fini(struct ttm_bo_device
*bdev
,
145 struct ttm_resource_manager
*man
= ttm_manager_type(bdev
, type
);
146 struct ttm_range_manager
*rman
= to_range_manager(man
);
147 struct drm_mm
*mm
= &rman
->mm
;
150 ttm_resource_manager_set_used(man
, false);
152 ret
= ttm_resource_manager_evict_all(bdev
, man
);
156 spin_lock(&rman
->lock
);
159 spin_unlock(&rman
->lock
);
161 ttm_resource_manager_cleanup(man
);
162 ttm_set_driver_manager(bdev
, type
, NULL
);
166 EXPORT_SYMBOL(ttm_range_man_fini
);
168 static void ttm_range_man_debug(struct ttm_resource_manager
*man
,
169 struct drm_printer
*printer
)
171 struct ttm_range_manager
*rman
= to_range_manager(man
);
173 spin_lock(&rman
->lock
);
174 drm_mm_print(&rman
->mm
, printer
);
175 spin_unlock(&rman
->lock
);
178 static const struct ttm_resource_manager_func ttm_range_manager_func
= {
179 .alloc
= ttm_range_man_alloc
,
180 .free
= ttm_range_man_free
,
181 .debug
= ttm_range_man_debug