1 /**************************************************************************
3 * Copyright (c) 2007-2010 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30 * $FreeBSD: head/sys/dev/drm2/ttm/ttm_bo_manager.c 247835 2013-03-05 09:49:34Z kib $
33 #include <dev/drm2/drmP.h>
34 #include <dev/drm2/ttm/ttm_module.h>
35 #include <dev/drm2/ttm/ttm_bo_driver.h>
36 #include <dev/drm2/ttm/ttm_placement.h>
37 #include <dev/drm2/drm_mm.h>
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.
45 struct ttm_range_manager {
50 MALLOC_DEFINE(M_TTM_RMAN, "ttm_rman", "TTM Range Manager");
52 static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man,
53 struct ttm_buffer_object *bo,
54 struct ttm_placement *placement,
55 struct ttm_mem_reg *mem)
57 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
58 struct drm_mm *mm = &rman->mm;
59 struct drm_mm_node *node = NULL;
63 lpfn = placement->lpfn;
67 ret = drm_mm_pre_get(mm);
71 lockmgr(&rman->lock, LK_EXCLUSIVE);
72 node = drm_mm_search_free_in_range(mm,
73 mem->num_pages, mem->page_alignment,
74 placement->fpfn, lpfn, 1);
75 if (unlikely(node == NULL)) {
76 lockmgr(&rman->lock, LK_RELEASE);
79 node = drm_mm_get_block_atomic_range(node, mem->num_pages,
83 lockmgr(&rman->lock, LK_RELEASE);
84 } while (node == NULL);
87 mem->start = node->start;
91 static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man,
92 struct ttm_mem_reg *mem)
94 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
97 lockmgr(&rman->lock, LK_EXCLUSIVE);
98 drm_mm_put_block(mem->mm_node);
99 lockmgr(&rman->lock, LK_RELEASE);
104 static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
105 unsigned long p_size)
107 struct ttm_range_manager *rman;
110 rman = kmalloc(sizeof(*rman), M_TTM_RMAN, M_ZERO | M_WAITOK);
111 ret = drm_mm_init(&rman->mm, 0, p_size);
113 drm_free(rman, M_TTM_RMAN);
117 lockinit(&rman->lock, "ttmrman", 0, LK_CANRECURSE);
122 static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
124 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
125 struct drm_mm *mm = &rman->mm;
127 lockmgr(&rman->lock, LK_EXCLUSIVE);
128 if (drm_mm_clean(mm)) {
130 lockmgr(&rman->lock, LK_RELEASE);
131 lockuninit(&rman->lock);
132 drm_free(rman, M_TTM_RMAN);
136 lockmgr(&rman->lock, LK_RELEASE);
140 static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
143 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
145 lockmgr(&rman->lock, LK_EXCLUSIVE);
146 drm_mm_debug_table(&rman->mm, prefix);
147 lockmgr(&rman->lock, LK_RELEASE);
150 const struct ttm_mem_type_manager_func ttm_bo_manager_func = {