xref: /dragonfly/sys/dev/drm/ttm/ttm_bo_manager.c (revision 1aa0974c)
1 /**************************************************************************
2  *
3  * Copyright (c) 2007-2010 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 /*
28  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29  *
30  * $FreeBSD: head/sys/dev/drm2/ttm/ttm_bo_manager.c 247835 2013-03-05 09:49:34Z kib $
31  */
32 
33 #include <drm/ttm/ttm_module.h>
34 #include <drm/ttm/ttm_bo_driver.h>
35 #include <drm/ttm/ttm_placement.h>
36 #include <drm/drm_mm.h>
37 #include <linux/export.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 drm_mm mm;
47 	struct lock lock;
48 };
49 
50 static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man,
51 			       struct ttm_buffer_object *bo,
52 			       struct ttm_placement *placement,
53 			       struct ttm_mem_reg *mem)
54 {
55 	struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
56 	struct drm_mm *mm = &rman->mm;
57 	struct drm_mm_node *node = NULL;
58 	unsigned long lpfn;
59 	int ret;
60 
61 	lpfn = placement->lpfn;
62 	if (!lpfn)
63 		lpfn = man->size;
64 	do {
65 		ret = drm_mm_pre_get(mm);
66 		if (unlikely(ret))
67 			return ret;
68 
69 		lockmgr(&rman->lock, LK_EXCLUSIVE);
70 		node = drm_mm_search_free_in_range(mm,
71 					mem->num_pages, mem->page_alignment,
72 					placement->fpfn, lpfn, 1);
73 		if (unlikely(node == NULL)) {
74 			lockmgr(&rman->lock, LK_RELEASE);
75 			return 0;
76 		}
77 		node = drm_mm_get_block_atomic_range(node, mem->num_pages,
78 						     mem->page_alignment,
79 						     placement->fpfn,
80 						     lpfn);
81 		lockmgr(&rman->lock, LK_RELEASE);
82 	} while (node == NULL);
83 
84 	mem->mm_node = node;
85 	mem->start = node->start;
86 	return 0;
87 }
88 
89 static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man,
90 				struct ttm_mem_reg *mem)
91 {
92 	struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
93 
94 	if (mem->mm_node) {
95 		lockmgr(&rman->lock, LK_EXCLUSIVE);
96 		drm_mm_put_block(mem->mm_node);
97 		lockmgr(&rman->lock, LK_RELEASE);
98 		mem->mm_node = NULL;
99 	}
100 }
101 
102 static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
103 			   unsigned long p_size)
104 {
105 	struct ttm_range_manager *rman;
106 
107 	rman = kmalloc(sizeof(*rman), M_DRM, M_ZERO | M_WAITOK);
108 	if (!rman)
109 		return -ENOMEM;
110 
111 	drm_mm_init(&rman->mm, 0, p_size);
112 
113 	lockinit(&rman->lock, "ttmrman", 0, LK_CANRECURSE);
114 	man->priv = rman;
115 	return 0;
116 }
117 
118 static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
119 {
120 	struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
121 	struct drm_mm *mm = &rman->mm;
122 
123 	lockmgr(&rman->lock, LK_EXCLUSIVE);
124 	if (drm_mm_clean(mm)) {
125 		drm_mm_takedown(mm);
126 		lockmgr(&rman->lock, LK_RELEASE);
127 		kfree(rman);
128 		man->priv = NULL;
129 		return 0;
130 	}
131 	lockmgr(&rman->lock, LK_RELEASE);
132 	return -EBUSY;
133 }
134 
135 static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
136 			     const char *prefix)
137 {
138 	struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
139 
140 	lockmgr(&rman->lock, LK_EXCLUSIVE);
141 	drm_mm_debug_table(&rman->mm, prefix);
142 	lockmgr(&rman->lock, LK_RELEASE);
143 }
144 
145 const struct ttm_mem_type_manager_func ttm_bo_manager_func = {
146 	ttm_bo_man_init,
147 	ttm_bo_man_takedown,
148 	ttm_bo_man_get_node,
149 	ttm_bo_man_put_node,
150 	ttm_bo_man_debug
151 };
152 EXPORT_SYMBOL(ttm_bo_manager_func);
153