1 1.6 riastrad /* $NetBSD: ttm_bo_manager.c,v 1.6 2021/12/19 12:37:43 riastradh Exp $ */ 2 1.3 riastrad 3 1.5 riastrad /* SPDX-License-Identifier: GPL-2.0 OR MIT */ 4 1.1 riastrad /************************************************************************** 5 1.1 riastrad * 6 1.1 riastrad * Copyright (c) 2007-2010 VMware, Inc., Palo Alto, CA., USA 7 1.1 riastrad * All Rights Reserved. 8 1.1 riastrad * 9 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 10 1.1 riastrad * copy of this software and associated documentation files (the 11 1.1 riastrad * "Software"), to deal in the Software without restriction, including 12 1.1 riastrad * without limitation the rights to use, copy, modify, merge, publish, 13 1.1 riastrad * distribute, sub license, and/or sell copies of the Software, and to 14 1.1 riastrad * permit persons to whom the Software is furnished to do so, subject to 15 1.1 riastrad * the following conditions: 16 1.1 riastrad * 17 1.1 riastrad * The above copyright notice and this permission notice (including the 18 1.1 riastrad * next paragraph) shall be included in all copies or substantial portions 19 1.1 riastrad * of the Software. 20 1.1 riastrad * 21 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 22 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 23 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 24 1.1 riastrad * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 25 1.1 riastrad * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 26 1.1 riastrad * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 27 1.1 riastrad * USE OR OTHER DEALINGS IN THE SOFTWARE. 28 1.1 riastrad * 29 1.1 riastrad **************************************************************************/ 30 1.1 riastrad /* 31 1.1 riastrad * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> 32 1.1 riastrad */ 33 1.1 riastrad 34 1.3 riastrad #include <sys/cdefs.h> 35 1.6 riastrad __KERNEL_RCSID(0, "$NetBSD: ttm_bo_manager.c,v 1.6 2021/12/19 12:37:43 riastradh Exp $"); 36 1.3 riastrad 37 1.1 riastrad #include <drm/ttm/ttm_module.h> 38 1.1 riastrad #include <drm/ttm/ttm_bo_driver.h> 39 1.1 riastrad #include <drm/ttm/ttm_placement.h> 40 1.1 riastrad #include <drm/drm_mm.h> 41 1.1 riastrad #include <linux/slab.h> 42 1.1 riastrad #include <linux/spinlock.h> 43 1.1 riastrad #include <linux/module.h> 44 1.1 riastrad 45 1.1 riastrad /** 46 1.1 riastrad * Currently we use a spinlock for the lock, but a mutex *may* be 47 1.1 riastrad * more appropriate to reduce scheduling latency if the range manager 48 1.1 riastrad * ends up with very fragmented allocation patterns. 49 1.1 riastrad */ 50 1.1 riastrad 51 1.1 riastrad struct ttm_range_manager { 52 1.1 riastrad struct drm_mm mm; 53 1.1 riastrad spinlock_t lock; 54 1.1 riastrad }; 55 1.1 riastrad 56 1.1 riastrad static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man, 57 1.1 riastrad struct ttm_buffer_object *bo, 58 1.3 riastrad const struct ttm_place *place, 59 1.1 riastrad struct ttm_mem_reg *mem) 60 1.1 riastrad { 61 1.1 riastrad struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv; 62 1.1 riastrad struct drm_mm *mm = &rman->mm; 63 1.5 riastrad struct drm_mm_node *node; 64 1.5 riastrad enum drm_mm_insert_mode mode; 65 1.1 riastrad unsigned long lpfn; 66 1.1 riastrad int ret; 67 1.1 riastrad 68 1.3 riastrad lpfn = place->lpfn; 69 1.1 riastrad if (!lpfn) 70 1.1 riastrad lpfn = man->size; 71 1.1 riastrad 72 1.2 riastrad node = kzalloc(sizeof(*node), GFP_KERNEL); 73 1.2 riastrad if (!node) 74 1.2 riastrad return -ENOMEM; 75 1.2 riastrad 76 1.5 riastrad mode = DRM_MM_INSERT_BEST; 77 1.5 riastrad if (place->flags & TTM_PL_FLAG_TOPDOWN) 78 1.5 riastrad mode = DRM_MM_INSERT_HIGH; 79 1.2 riastrad 80 1.2 riastrad spin_lock(&rman->lock); 81 1.5 riastrad ret = drm_mm_insert_node_in_range(mm, node, 82 1.5 riastrad mem->num_pages, 83 1.2 riastrad mem->page_alignment, 0, 84 1.5 riastrad place->fpfn, lpfn, mode); 85 1.2 riastrad spin_unlock(&rman->lock); 86 1.2 riastrad 87 1.2 riastrad if (unlikely(ret)) { 88 1.2 riastrad kfree(node); 89 1.2 riastrad } else { 90 1.2 riastrad mem->mm_node = node; 91 1.2 riastrad mem->start = node->start; 92 1.2 riastrad } 93 1.1 riastrad 94 1.1 riastrad return 0; 95 1.1 riastrad } 96 1.1 riastrad 97 1.1 riastrad static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man, 98 1.1 riastrad struct ttm_mem_reg *mem) 99 1.1 riastrad { 100 1.1 riastrad struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv; 101 1.1 riastrad 102 1.1 riastrad if (mem->mm_node) { 103 1.1 riastrad spin_lock(&rman->lock); 104 1.2 riastrad drm_mm_remove_node(mem->mm_node); 105 1.1 riastrad spin_unlock(&rman->lock); 106 1.2 riastrad 107 1.2 riastrad kfree(mem->mm_node); 108 1.1 riastrad mem->mm_node = NULL; 109 1.1 riastrad } 110 1.1 riastrad } 111 1.1 riastrad 112 1.1 riastrad static int ttm_bo_man_init(struct ttm_mem_type_manager *man, 113 1.1 riastrad unsigned long p_size) 114 1.1 riastrad { 115 1.1 riastrad struct ttm_range_manager *rman; 116 1.1 riastrad 117 1.1 riastrad rman = kzalloc(sizeof(*rman), GFP_KERNEL); 118 1.1 riastrad if (!rman) 119 1.1 riastrad return -ENOMEM; 120 1.1 riastrad 121 1.2 riastrad drm_mm_init(&rman->mm, 0, p_size); 122 1.1 riastrad spin_lock_init(&rman->lock); 123 1.1 riastrad man->priv = rman; 124 1.1 riastrad return 0; 125 1.1 riastrad } 126 1.1 riastrad 127 1.1 riastrad static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man) 128 1.1 riastrad { 129 1.1 riastrad struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv; 130 1.1 riastrad struct drm_mm *mm = &rman->mm; 131 1.1 riastrad 132 1.1 riastrad spin_lock(&rman->lock); 133 1.1 riastrad if (drm_mm_clean(mm)) { 134 1.1 riastrad drm_mm_takedown(mm); 135 1.1 riastrad spin_unlock(&rman->lock); 136 1.6 riastrad spin_lock_destroy(&rman->lock); 137 1.1 riastrad kfree(rman); 138 1.1 riastrad man->priv = NULL; 139 1.1 riastrad return 0; 140 1.1 riastrad } 141 1.1 riastrad spin_unlock(&rman->lock); 142 1.1 riastrad return -EBUSY; 143 1.1 riastrad } 144 1.1 riastrad 145 1.1 riastrad static void ttm_bo_man_debug(struct ttm_mem_type_manager *man, 146 1.5 riastrad struct drm_printer *printer) 147 1.1 riastrad { 148 1.1 riastrad struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv; 149 1.1 riastrad 150 1.1 riastrad spin_lock(&rman->lock); 151 1.5 riastrad drm_mm_print(&rman->mm, printer); 152 1.1 riastrad spin_unlock(&rman->lock); 153 1.1 riastrad } 154 1.1 riastrad 155 1.1 riastrad const struct ttm_mem_type_manager_func ttm_bo_manager_func = { 156 1.5 riastrad .init = ttm_bo_man_init, 157 1.5 riastrad .takedown = ttm_bo_man_takedown, 158 1.5 riastrad .get_node = ttm_bo_man_get_node, 159 1.5 riastrad .put_node = ttm_bo_man_put_node, 160 1.5 riastrad .debug = ttm_bo_man_debug 161 1.1 riastrad }; 162 1.1 riastrad EXPORT_SYMBOL(ttm_bo_manager_func); 163