xref: /freebsd/sys/dev/drm2/ttm/ttm_bo_manager.c (revision 685dc743)
1592ffb21SWarner Losh /**************************************************************************
2592ffb21SWarner Losh  *
3592ffb21SWarner Losh  * Copyright (c) 2007-2010 VMware, Inc., Palo Alto, CA., USA
4592ffb21SWarner Losh  * All Rights Reserved.
5592ffb21SWarner Losh  *
6592ffb21SWarner Losh  * Permission is hereby granted, free of charge, to any person obtaining a
7592ffb21SWarner Losh  * copy of this software and associated documentation files (the
8592ffb21SWarner Losh  * "Software"), to deal in the Software without restriction, including
9592ffb21SWarner Losh  * without limitation the rights to use, copy, modify, merge, publish,
10592ffb21SWarner Losh  * distribute, sub license, and/or sell copies of the Software, and to
11592ffb21SWarner Losh  * permit persons to whom the Software is furnished to do so, subject to
12592ffb21SWarner Losh  * the following conditions:
13592ffb21SWarner Losh  *
14592ffb21SWarner Losh  * The above copyright notice and this permission notice (including the
15592ffb21SWarner Losh  * next paragraph) shall be included in all copies or substantial portions
16592ffb21SWarner Losh  * of the Software.
17592ffb21SWarner Losh  *
18592ffb21SWarner Losh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19592ffb21SWarner Losh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20592ffb21SWarner Losh  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21592ffb21SWarner Losh  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22592ffb21SWarner Losh  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23592ffb21SWarner Losh  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24592ffb21SWarner Losh  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25592ffb21SWarner Losh  *
26592ffb21SWarner Losh  **************************************************************************/
27592ffb21SWarner Losh /*
28592ffb21SWarner Losh  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29592ffb21SWarner Losh  */
30592ffb21SWarner Losh 
31592ffb21SWarner Losh #include <sys/cdefs.h>
32592ffb21SWarner Losh #include <dev/drm2/drmP.h>
33592ffb21SWarner Losh #include <dev/drm2/ttm/ttm_module.h>
34592ffb21SWarner Losh #include <dev/drm2/ttm/ttm_bo_driver.h>
35592ffb21SWarner Losh #include <dev/drm2/ttm/ttm_placement.h>
36592ffb21SWarner Losh #include <dev/drm2/drm_mm.h>
37592ffb21SWarner Losh 
38592ffb21SWarner Losh /**
39592ffb21SWarner Losh  * Currently we use a spinlock for the lock, but a mutex *may* be
40592ffb21SWarner Losh  * more appropriate to reduce scheduling latency if the range manager
41592ffb21SWarner Losh  * ends up with very fragmented allocation patterns.
42592ffb21SWarner Losh  */
43592ffb21SWarner Losh 
44592ffb21SWarner Losh struct ttm_range_manager {
45592ffb21SWarner Losh 	struct drm_mm mm;
46592ffb21SWarner Losh 	struct mtx lock;
47592ffb21SWarner Losh };
48592ffb21SWarner Losh 
49592ffb21SWarner Losh MALLOC_DEFINE(M_TTM_RMAN, "ttm_rman", "TTM Range Manager");
50592ffb21SWarner 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)51592ffb21SWarner Losh static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man,
52592ffb21SWarner Losh 			       struct ttm_buffer_object *bo,
53592ffb21SWarner Losh 			       struct ttm_placement *placement,
54592ffb21SWarner Losh 			       struct ttm_mem_reg *mem)
55592ffb21SWarner Losh {
56592ffb21SWarner Losh 	struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
57592ffb21SWarner Losh 	struct drm_mm *mm = &rman->mm;
58592ffb21SWarner Losh 	struct drm_mm_node *node = NULL;
59592ffb21SWarner Losh 	unsigned long lpfn;
60592ffb21SWarner Losh 	int ret;
61592ffb21SWarner Losh 
62592ffb21SWarner Losh 	lpfn = placement->lpfn;
63592ffb21SWarner Losh 	if (!lpfn)
64592ffb21SWarner Losh 		lpfn = man->size;
65592ffb21SWarner Losh 	do {
66592ffb21SWarner Losh 		ret = drm_mm_pre_get(mm);
67592ffb21SWarner Losh 		if (unlikely(ret))
68592ffb21SWarner Losh 			return ret;
69592ffb21SWarner Losh 
70592ffb21SWarner Losh 		mtx_lock(&rman->lock);
71592ffb21SWarner Losh 		node = drm_mm_search_free_in_range(mm,
72592ffb21SWarner Losh 					mem->num_pages, mem->page_alignment,
73592ffb21SWarner Losh 					placement->fpfn, lpfn, 1);
74592ffb21SWarner Losh 		if (unlikely(node == NULL)) {
75592ffb21SWarner Losh 			mtx_unlock(&rman->lock);
76592ffb21SWarner Losh 			return 0;
77592ffb21SWarner Losh 		}
78592ffb21SWarner Losh 		node = drm_mm_get_block_atomic_range(node, mem->num_pages,
79592ffb21SWarner Losh 						     mem->page_alignment,
80592ffb21SWarner Losh 						     placement->fpfn,
81592ffb21SWarner Losh 						     lpfn);
82592ffb21SWarner Losh 		mtx_unlock(&rman->lock);
83592ffb21SWarner Losh 	} while (node == NULL);
84592ffb21SWarner Losh 
85592ffb21SWarner Losh 	mem->mm_node = node;
86592ffb21SWarner Losh 	mem->start = node->start;
87592ffb21SWarner Losh 	return 0;
88592ffb21SWarner Losh }
89592ffb21SWarner Losh 
ttm_bo_man_put_node(struct ttm_mem_type_manager * man,struct ttm_mem_reg * mem)90592ffb21SWarner Losh static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man,
91592ffb21SWarner Losh 				struct ttm_mem_reg *mem)
92592ffb21SWarner Losh {
93592ffb21SWarner Losh 	struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
94592ffb21SWarner Losh 
95592ffb21SWarner Losh 	if (mem->mm_node) {
96592ffb21SWarner Losh 		mtx_lock(&rman->lock);
97592ffb21SWarner Losh 		drm_mm_put_block(mem->mm_node);
98592ffb21SWarner Losh 		mtx_unlock(&rman->lock);
99592ffb21SWarner Losh 		mem->mm_node = NULL;
100592ffb21SWarner Losh 	}
101592ffb21SWarner Losh }
102592ffb21SWarner Losh 
ttm_bo_man_init(struct ttm_mem_type_manager * man,unsigned long p_size)103592ffb21SWarner Losh static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
104592ffb21SWarner Losh 			   unsigned long p_size)
105592ffb21SWarner Losh {
106592ffb21SWarner Losh 	struct ttm_range_manager *rman;
107592ffb21SWarner Losh 	int ret;
108592ffb21SWarner Losh 
109592ffb21SWarner Losh 	rman = malloc(sizeof(*rman), M_TTM_RMAN, M_ZERO | M_WAITOK);
110592ffb21SWarner Losh 	ret = drm_mm_init(&rman->mm, 0, p_size);
111592ffb21SWarner Losh 	if (ret) {
112592ffb21SWarner Losh 		free(rman, M_TTM_RMAN);
113592ffb21SWarner Losh 		return ret;
114592ffb21SWarner Losh 	}
115592ffb21SWarner Losh 
116592ffb21SWarner Losh 	mtx_init(&rman->lock, "ttmrman", NULL, MTX_DEF);
117592ffb21SWarner Losh 	man->priv = rman;
118592ffb21SWarner Losh 	return 0;
119592ffb21SWarner Losh }
120592ffb21SWarner Losh 
ttm_bo_man_takedown(struct ttm_mem_type_manager * man)121592ffb21SWarner Losh static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
122592ffb21SWarner Losh {
123592ffb21SWarner Losh 	struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
124592ffb21SWarner Losh 	struct drm_mm *mm = &rman->mm;
125592ffb21SWarner Losh 
126592ffb21SWarner Losh 	mtx_lock(&rman->lock);
127592ffb21SWarner Losh 	if (drm_mm_clean(mm)) {
128592ffb21SWarner Losh 		drm_mm_takedown(mm);
129592ffb21SWarner Losh 		mtx_unlock(&rman->lock);
130592ffb21SWarner Losh 		mtx_destroy(&rman->lock);
131592ffb21SWarner Losh 		free(rman, M_TTM_RMAN);
132592ffb21SWarner Losh 		man->priv = NULL;
133592ffb21SWarner Losh 		return 0;
134592ffb21SWarner Losh 	}
135592ffb21SWarner Losh 	mtx_unlock(&rman->lock);
136592ffb21SWarner Losh 	return -EBUSY;
137592ffb21SWarner Losh }
138592ffb21SWarner Losh 
ttm_bo_man_debug(struct ttm_mem_type_manager * man,const char * prefix)139592ffb21SWarner Losh static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
140592ffb21SWarner Losh 			     const char *prefix)
141592ffb21SWarner Losh {
142592ffb21SWarner Losh 	struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
143592ffb21SWarner Losh 
144592ffb21SWarner Losh 	mtx_lock(&rman->lock);
145592ffb21SWarner Losh 	drm_mm_debug_table(&rman->mm, prefix);
146592ffb21SWarner Losh 	mtx_unlock(&rman->lock);
147592ffb21SWarner Losh }
148592ffb21SWarner Losh 
149592ffb21SWarner Losh const struct ttm_mem_type_manager_func ttm_bo_manager_func = {
150592ffb21SWarner Losh 	ttm_bo_man_init,
151592ffb21SWarner Losh 	ttm_bo_man_takedown,
152592ffb21SWarner Losh 	ttm_bo_man_get_node,
153592ffb21SWarner Losh 	ttm_bo_man_put_node,
154592ffb21SWarner Losh 	ttm_bo_man_debug
155592ffb21SWarner Losh };
156