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