| 1 | /* SPDX-License-Identifier: GPL-2.0 OR MIT */ |
| 2 | /************************************************************************** |
| 3 | * |
| 4 | * Copyright (c) 2007-2010 VMware, Inc., Palo Alto, CA., USA |
| 5 | * All Rights Reserved. |
| 6 | * |
| 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: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice (including the |
| 16 | * next paragraph) shall be included in all copies or substantial portions |
| 17 | * of the Software. |
| 18 | * |
| 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. |
| 26 | * |
| 27 | **************************************************************************/ |
| 28 | /* |
| 29 | * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> |
| 30 | */ |
| 31 | |
| 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.h> |
| 36 | #include <drm/drm_mm.h> |
| 37 | |
| 38 | #include <linux/export.h> |
| 39 | #include <linux/slab.h> |
| 40 | #include <linux/spinlock.h> |
| 41 | |
| 42 | /* |
| 43 | * Currently we use a spinlock for the lock, but a mutex *may* be |
| 44 | * more appropriate to reduce scheduling latency if the range manager |
| 45 | * ends up with very fragmented allocation patterns. |
| 46 | */ |
| 47 | |
| 48 | struct ttm_range_manager { |
| 49 | struct ttm_resource_manager manager; |
| 50 | struct drm_mm mm; |
| 51 | spinlock_t lock; |
| 52 | }; |
| 53 | |
| 54 | static inline struct ttm_range_manager * |
| 55 | to_range_manager(struct ttm_resource_manager *man) |
| 56 | { |
| 57 | return container_of(man, struct ttm_range_manager, manager); |
| 58 | } |
| 59 | |
| 60 | static int ttm_range_man_alloc(struct ttm_resource_manager *man, |
| 61 | struct ttm_buffer_object *bo, |
| 62 | const struct ttm_place *place, |
| 63 | struct ttm_resource **res) |
| 64 | { |
| 65 | struct ttm_range_manager *rman = to_range_manager(man); |
| 66 | struct ttm_range_mgr_node *node; |
| 67 | struct drm_mm *mm = &rman->mm; |
| 68 | enum drm_mm_insert_mode mode; |
| 69 | unsigned long lpfn; |
| 70 | int ret; |
| 71 | |
| 72 | lpfn = place->lpfn; |
| 73 | if (!lpfn) |
| 74 | lpfn = man->size; |
| 75 | |
| 76 | node = kzalloc(struct_size(node, mm_nodes, 1), GFP_KERNEL); |
| 77 | if (!node) |
| 78 | return -ENOMEM; |
| 79 | |
| 80 | mode = DRM_MM_INSERT_BEST; |
| 81 | if (place->flags & TTM_PL_FLAG_TOPDOWN) |
| 82 | mode = DRM_MM_INSERT_HIGH; |
| 83 | |
| 84 | ttm_resource_init(bo, place, res: &node->base); |
| 85 | |
| 86 | spin_lock(lock: &rman->lock); |
| 87 | ret = drm_mm_insert_node_in_range(mm, node: &node->mm_nodes[0], |
| 88 | PFN_UP(node->base.size), |
| 89 | alignment: bo->page_alignment, color: 0, |
| 90 | start: place->fpfn, end: lpfn, mode); |
| 91 | spin_unlock(lock: &rman->lock); |
| 92 | |
| 93 | if (unlikely(ret)) { |
| 94 | ttm_resource_fini(man, res: &node->base); |
| 95 | kfree(objp: node); |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | node->base.start = node->mm_nodes[0].start; |
| 100 | *res = &node->base; |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static void ttm_range_man_free(struct ttm_resource_manager *man, |
| 105 | struct ttm_resource *res) |
| 106 | { |
| 107 | struct ttm_range_mgr_node *node = to_ttm_range_mgr_node(res); |
| 108 | struct ttm_range_manager *rman = to_range_manager(man); |
| 109 | |
| 110 | spin_lock(lock: &rman->lock); |
| 111 | drm_mm_remove_node(node: &node->mm_nodes[0]); |
| 112 | spin_unlock(lock: &rman->lock); |
| 113 | |
| 114 | ttm_resource_fini(man, res); |
| 115 | kfree(objp: node); |
| 116 | } |
| 117 | |
| 118 | static bool ttm_range_man_intersects(struct ttm_resource_manager *man, |
| 119 | struct ttm_resource *res, |
| 120 | const struct ttm_place *place, |
| 121 | size_t size) |
| 122 | { |
| 123 | struct drm_mm_node *node = &to_ttm_range_mgr_node(res)->mm_nodes[0]; |
| 124 | u32 num_pages = PFN_UP(size); |
| 125 | |
| 126 | /* Don't evict BOs outside of the requested placement range */ |
| 127 | if (place->fpfn >= (node->start + num_pages) || |
| 128 | (place->lpfn && place->lpfn <= node->start)) |
| 129 | return false; |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | static bool ttm_range_man_compatible(struct ttm_resource_manager *man, |
| 135 | struct ttm_resource *res, |
| 136 | const struct ttm_place *place, |
| 137 | size_t size) |
| 138 | { |
| 139 | struct drm_mm_node *node = &to_ttm_range_mgr_node(res)->mm_nodes[0]; |
| 140 | u32 num_pages = PFN_UP(size); |
| 141 | |
| 142 | if (node->start < place->fpfn || |
| 143 | (place->lpfn && (node->start + num_pages) > place->lpfn)) |
| 144 | return false; |
| 145 | |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | static void ttm_range_man_debug(struct ttm_resource_manager *man, |
| 150 | struct drm_printer *printer) |
| 151 | { |
| 152 | struct ttm_range_manager *rman = to_range_manager(man); |
| 153 | |
| 154 | spin_lock(lock: &rman->lock); |
| 155 | drm_mm_print(mm: &rman->mm, p: printer); |
| 156 | spin_unlock(lock: &rman->lock); |
| 157 | } |
| 158 | |
| 159 | static const struct ttm_resource_manager_func ttm_range_manager_func = { |
| 160 | .alloc = ttm_range_man_alloc, |
| 161 | .free = ttm_range_man_free, |
| 162 | .intersects = ttm_range_man_intersects, |
| 163 | .compatible = ttm_range_man_compatible, |
| 164 | .debug = ttm_range_man_debug |
| 165 | }; |
| 166 | |
| 167 | /** |
| 168 | * ttm_range_man_init_nocheck - Initialise a generic range manager for the |
| 169 | * selected memory type. |
| 170 | * |
| 171 | * @bdev: ttm device |
| 172 | * @type: memory manager type |
| 173 | * @use_tt: if the memory manager uses tt |
| 174 | * @p_size: size of area to be managed in pages. |
| 175 | * |
| 176 | * The range manager is installed for this device in the type slot. |
| 177 | * |
| 178 | * Return: %0 on success or a negative error code on failure |
| 179 | */ |
| 180 | int ttm_range_man_init_nocheck(struct ttm_device *bdev, |
| 181 | unsigned type, bool use_tt, |
| 182 | unsigned long p_size) |
| 183 | { |
| 184 | struct ttm_resource_manager *man; |
| 185 | struct ttm_range_manager *rman; |
| 186 | |
| 187 | rman = kzalloc(sizeof(*rman), GFP_KERNEL); |
| 188 | if (!rman) |
| 189 | return -ENOMEM; |
| 190 | |
| 191 | man = &rman->manager; |
| 192 | man->use_tt = use_tt; |
| 193 | |
| 194 | man->func = &ttm_range_manager_func; |
| 195 | |
| 196 | ttm_resource_manager_init(man, bdev, size: p_size); |
| 197 | |
| 198 | drm_mm_init(mm: &rman->mm, start: 0, size: p_size); |
| 199 | spin_lock_init(&rman->lock); |
| 200 | |
| 201 | ttm_set_driver_manager(bdev, type, manager: &rman->manager); |
| 202 | ttm_resource_manager_set_used(man, used: true); |
| 203 | return 0; |
| 204 | } |
| 205 | EXPORT_SYMBOL(ttm_range_man_init_nocheck); |
| 206 | |
| 207 | /** |
| 208 | * ttm_range_man_fini_nocheck - Remove the generic range manager from a slot |
| 209 | * and tear it down. |
| 210 | * |
| 211 | * @bdev: ttm device |
| 212 | * @type: memory manager type |
| 213 | * |
| 214 | * Return: %0 on success or a negative error code on failure |
| 215 | */ |
| 216 | int ttm_range_man_fini_nocheck(struct ttm_device *bdev, |
| 217 | unsigned type) |
| 218 | { |
| 219 | struct ttm_resource_manager *man = ttm_manager_type(bdev, mem_type: type); |
| 220 | struct ttm_range_manager *rman = to_range_manager(man); |
| 221 | struct drm_mm *mm = &rman->mm; |
| 222 | int ret; |
| 223 | |
| 224 | if (!man) |
| 225 | return 0; |
| 226 | |
| 227 | ttm_resource_manager_set_used(man, used: false); |
| 228 | |
| 229 | ret = ttm_resource_manager_evict_all(bdev, man); |
| 230 | if (ret) |
| 231 | return ret; |
| 232 | |
| 233 | spin_lock(lock: &rman->lock); |
| 234 | drm_mm_takedown(mm); |
| 235 | spin_unlock(lock: &rman->lock); |
| 236 | |
| 237 | ttm_resource_manager_cleanup(man); |
| 238 | ttm_set_driver_manager(bdev, type, NULL); |
| 239 | kfree(objp: rman); |
| 240 | return 0; |
| 241 | } |
| 242 | EXPORT_SYMBOL(ttm_range_man_fini_nocheck); |
| 243 | |