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_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> 38 39 /* 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. 43 */ 44 45 struct ttm_range_manager { 46 struct ttm_resource_manager manager; 47 struct drm_mm mm; 48 spinlock_t lock; 49 }; 50 51 static inline struct ttm_range_manager * 52 to_range_manager(struct ttm_resource_manager *man) 53 { 54 return container_of(man, struct ttm_range_manager, manager); 55 } 56 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) 61 { 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; 66 unsigned long lpfn; 67 int ret; 68 69 lpfn = place->lpfn; 70 if (!lpfn) 71 lpfn = man->size; 72 73 node = kzalloc(sizeof(*node), GFP_KERNEL); 74 if (!node) 75 return -ENOMEM; 76 77 mode = DRM_MM_INSERT_BEST; 78 if (place->flags & TTM_PL_FLAG_TOPDOWN) 79 mode = DRM_MM_INSERT_HIGH; 80 81 spin_lock(&rman->lock); 82 ret = drm_mm_insert_node_in_range(mm, node, mem->num_pages, 83 bo->page_alignment, 0, 84 place->fpfn, lpfn, mode); 85 spin_unlock(&rman->lock); 86 87 if (unlikely(ret)) { 88 kfree(node); 89 } else { 90 mem->mm_node = node; 91 mem->start = node->start; 92 } 93 94 return ret; 95 } 96 97 static void ttm_range_man_free(struct ttm_resource_manager *man, 98 struct ttm_resource *mem) 99 { 100 struct ttm_range_manager *rman = to_range_manager(man); 101 102 if (mem->mm_node) { 103 spin_lock(&rman->lock); 104 drm_mm_remove_node(mem->mm_node); 105 spin_unlock(&rman->lock); 106 107 kfree(mem->mm_node); 108 mem->mm_node = NULL; 109 } 110 } 111 112 static void ttm_range_man_debug(struct ttm_resource_manager *man, 113 struct drm_printer *printer) 114 { 115 struct ttm_range_manager *rman = to_range_manager(man); 116 117 spin_lock(&rman->lock); 118 drm_mm_print(&rman->mm, printer); 119 spin_unlock(&rman->lock); 120 } 121 122 static const struct ttm_resource_manager_func ttm_range_manager_func = { 123 .alloc = ttm_range_man_alloc, 124 .free = ttm_range_man_free, 125 .debug = ttm_range_man_debug 126 }; 127 128 int ttm_range_man_init(struct ttm_device *bdev, 129 unsigned type, bool use_tt, 130 unsigned long p_size) 131 { 132 struct ttm_resource_manager *man; 133 struct ttm_range_manager *rman; 134 135 rman = kzalloc(sizeof(*rman), GFP_KERNEL); 136 if (!rman) 137 return -ENOMEM; 138 139 man = &rman->manager; 140 man->use_tt = use_tt; 141 142 man->func = &ttm_range_manager_func; 143 144 ttm_resource_manager_init(man, p_size); 145 146 drm_mm_init(&rman->mm, 0, p_size); 147 spin_lock_init(&rman->lock); 148 149 ttm_set_driver_manager(bdev, type, &rman->manager); 150 ttm_resource_manager_set_used(man, true); 151 return 0; 152 } 153 EXPORT_SYMBOL(ttm_range_man_init); 154 155 int ttm_range_man_fini(struct ttm_device *bdev, 156 unsigned type) 157 { 158 struct ttm_resource_manager *man = ttm_manager_type(bdev, type); 159 struct ttm_range_manager *rman = to_range_manager(man); 160 struct drm_mm *mm = &rman->mm; 161 int ret; 162 163 ttm_resource_manager_set_used(man, false); 164 165 ret = ttm_resource_manager_evict_all(bdev, man); 166 if (ret) 167 return ret; 168 169 spin_lock(&rman->lock); 170 drm_mm_clean(mm); 171 drm_mm_takedown(mm); 172 spin_unlock(&rman->lock); 173 174 ttm_resource_manager_cleanup(man); 175 ttm_set_driver_manager(bdev, type, NULL); 176 kfree(rman); 177 return 0; 178 } 179 EXPORT_SYMBOL(ttm_range_man_fini); 180