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