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_device.h>
33 #include <drm/ttm/ttm_placement.h>
34 #include <drm/ttm/ttm_range_manager.h>
35 #include <drm/ttm/ttm_bo_api.h>
36 #include <drm/drm_mm.h>
37 #include <linux/slab.h>
38 #include <linux/spinlock.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 *
53 to_range_manager(struct ttm_resource_manager *man)
55 return container_of(man, struct ttm_range_manager, manager);
58 static int ttm_range_man_alloc(struct ttm_resource_manager *man,
59 struct ttm_buffer_object *bo,
60 const struct ttm_place *place,
61 struct ttm_resource **res)
63 struct ttm_range_manager *rman = to_range_manager(man);
64 struct ttm_range_mgr_node *node;
65 struct drm_mm *mm = &rman->mm;
66 enum drm_mm_insert_mode mode;
74 node = kzalloc(struct_size(node, mm_nodes, 1), GFP_KERNEL);
78 mode = DRM_MM_INSERT_BEST;
79 if (place->flags & TTM_PL_FLAG_TOPDOWN)
80 mode = DRM_MM_INSERT_HIGH;
82 ttm_resource_init(bo, place, &node->base);
84 spin_lock(&rman->lock);
85 ret = drm_mm_insert_node_in_range(mm, &node->mm_nodes[0],
87 bo->page_alignment, 0,
88 place->fpfn, lpfn, mode);
89 spin_unlock(&rman->lock);
92 ttm_resource_fini(man, &node->base);
97 node->base.start = node->mm_nodes[0].start;
102 static void ttm_range_man_free(struct ttm_resource_manager *man,
103 struct ttm_resource *res)
105 struct ttm_range_mgr_node *node = to_ttm_range_mgr_node(res);
106 struct ttm_range_manager *rman = to_range_manager(man);
108 spin_lock(&rman->lock);
109 drm_mm_remove_node(&node->mm_nodes[0]);
110 spin_unlock(&rman->lock);
112 ttm_resource_fini(man, res);
116 static void ttm_range_man_debug(struct ttm_resource_manager *man,
117 struct drm_printer *printer)
119 struct ttm_range_manager *rman = to_range_manager(man);
121 spin_lock(&rman->lock);
122 drm_mm_print(&rman->mm, printer);
123 spin_unlock(&rman->lock);
126 static const struct ttm_resource_manager_func ttm_range_manager_func = {
127 .alloc = ttm_range_man_alloc,
128 .free = ttm_range_man_free,
129 .debug = ttm_range_man_debug
133 * ttm_range_man_init_nocheck - Initialise a generic range manager for the
134 * selected memory type.
137 * @type: memory manager type
138 * @use_tt: if the memory manager uses tt
139 * @p_size: size of area to be managed in pages.
141 * The range manager is installed for this device in the type slot.
143 * Return: %0 on success or a negative error code on failure
145 int ttm_range_man_init_nocheck(struct ttm_device *bdev,
146 unsigned type, bool use_tt,
147 unsigned long p_size)
149 struct ttm_resource_manager *man;
150 struct ttm_range_manager *rman;
152 rman = kzalloc(sizeof(*rman), GFP_KERNEL);
156 man = &rman->manager;
157 man->use_tt = use_tt;
159 man->func = &ttm_range_manager_func;
161 ttm_resource_manager_init(man, bdev, p_size);
163 drm_mm_init(&rman->mm, 0, p_size);
164 spin_lock_init(&rman->lock);
166 ttm_set_driver_manager(bdev, type, &rman->manager);
167 ttm_resource_manager_set_used(man, true);
170 EXPORT_SYMBOL(ttm_range_man_init_nocheck);
173 * ttm_range_man_fini_nocheck - Remove the generic range manager from a slot
177 * @type: memory manager type
179 * Return: %0 on success or a negative error code on failure
181 int ttm_range_man_fini_nocheck(struct ttm_device *bdev,
184 struct ttm_resource_manager *man = ttm_manager_type(bdev, type);
185 struct ttm_range_manager *rman = to_range_manager(man);
186 struct drm_mm *mm = &rman->mm;
192 ttm_resource_manager_set_used(man, false);
194 ret = ttm_resource_manager_evict_all(bdev, man);
198 spin_lock(&rman->lock);
201 spin_unlock(&rman->lock);
203 ttm_resource_manager_cleanup(man);
204 ttm_set_driver_manager(bdev, type, NULL);
208 EXPORT_SYMBOL(ttm_range_man_fini_nocheck);