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,
117 uint32_t available_caching,
118 uint32_t default_caching,
120 unsigned long p_size)
122 struct ttm_resource_manager *man;
123 struct ttm_range_manager *rman;
125 rman = kzalloc(sizeof(*rman), GFP_KERNEL);
129 man = &rman->manager;
130 man->available_caching = available_caching;
131 man->default_caching = default_caching;
132 man->use_tt = use_tt;
134 man->func = &ttm_range_manager_func;
136 ttm_resource_manager_init(man, p_size);
138 drm_mm_init(&rman->mm, 0, p_size);
139 spin_lock_init(&rman->lock);
141 ttm_set_driver_manager(bdev, type, &rman->manager);
142 ttm_resource_manager_set_used(man, true);
145 EXPORT_SYMBOL(ttm_range_man_init);
147 int ttm_range_man_fini(struct ttm_bo_device *bdev,
150 struct ttm_resource_manager *man = ttm_manager_type(bdev, type);
151 struct ttm_range_manager *rman = to_range_manager(man);
152 struct drm_mm *mm = &rman->mm;
155 ttm_resource_manager_set_used(man, false);
157 ret = ttm_resource_manager_force_list_clean(bdev, man);
161 spin_lock(&rman->lock);
164 spin_unlock(&rman->lock);
166 ttm_resource_manager_cleanup(man);
167 ttm_set_driver_manager(bdev, type, NULL);
171 EXPORT_SYMBOL(ttm_range_man_fini);
173 static void ttm_range_man_debug(struct ttm_resource_manager *man,
174 struct drm_printer *printer)
176 struct ttm_range_manager *rman = to_range_manager(man);
178 spin_lock(&rman->lock);
179 drm_mm_print(&rman->mm, printer);
180 spin_unlock(&rman->lock);
183 static const struct ttm_resource_manager_func ttm_range_manager_func = {
184 .alloc = ttm_range_man_alloc,
185 .free = ttm_range_man_free,
186 .debug = ttm_range_man_debug