drm/ttm: convert to unified vma offset manager
[dragonfly.git] / sys / dev / drm / ttm / ttm_bo_manager.c
CommitLineData
5c9d63cf
FT
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>
5c9d63cf
FT
29 */
30
216f7a2c
FT
31#include <drm/ttm/ttm_module.h>
32#include <drm/ttm/ttm_bo_driver.h>
33#include <drm/ttm/ttm_placement.h>
18e26a6d 34#include <drm/drm_mm.h>
a34b4168
MD
35#include <linux/slab.h>
36#include <linux/spinlock.h>
37#include <linux/module.h>
5c9d63cf
FT
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
45struct ttm_range_manager {
46 struct drm_mm mm;
1716017e 47 struct lock lock;
5c9d63cf
FT
48};
49
5c9d63cf
FT
50static int ttm_bo_man_get_node(struct ttm_mem_type_manager *man,
51 struct ttm_buffer_object *bo,
a34b4168 52 struct ttm_placement *placement,
5c9d63cf
FT
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;
c6f73aab 58 enum drm_mm_allocator_flags aflags = DRM_MM_CREATE_DEFAULT;
5c9d63cf
FT
59 unsigned long lpfn;
60 int ret;
61
a34b4168 62 lpfn = placement->lpfn;
5c9d63cf
FT
63 if (!lpfn)
64 lpfn = man->size;
5c9d63cf 65
4cd92098 66 node = kzalloc(sizeof(*node), GFP_KERNEL);
67 if (!node)
68 return -ENOMEM;
a34b4168
MD
69 /* not in yet ?
70 if (placement->flags & TTM_PL_FLAG_TOPDOWN)
c6f73aab 71 aflags = DRM_MM_CREATE_TOP;
a34b4168 72 */
c6f73aab 73
4cd92098 74 lockmgr(&rman->lock, LK_EXCLUSIVE);
c6f73aab 75 ret = drm_mm_insert_node_in_range_generic(mm, node, mem->num_pages,
a34b4168
MD
76 mem->page_alignment, 0,
77 placement->fpfn, lpfn,
78 DRM_MM_SEARCH_BEST,
79 aflags);
4cd92098 80 lockmgr(&rman->lock, LK_RELEASE);
81
82 if (unlikely(ret)) {
83 kfree(node);
84 } else {
85 mem->mm_node = node;
86 mem->start = node->start;
87 }
5c9d63cf
FT
88 return 0;
89}
90
91static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man,
92 struct ttm_mem_reg *mem)
93{
94 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
95
96 if (mem->mm_node) {
1716017e 97 lockmgr(&rman->lock, LK_EXCLUSIVE);
a34b4168 98 drm_mm_put_block(mem->mm_node);
1716017e 99 lockmgr(&rman->lock, LK_RELEASE);
5c9d63cf
FT
100 mem->mm_node = NULL;
101 }
102}
103
104static int ttm_bo_man_init(struct ttm_mem_type_manager *man,
105 unsigned long p_size)
106{
107 struct ttm_range_manager *rman;
5c9d63cf 108
175896df 109 rman = kzalloc(sizeof(*rman), GFP_KERNEL);
c19c6249
FT
110 if (!rman)
111 return -ENOMEM;
112
b6fbc077 113 drm_mm_init(&rman->mm, 0, p_size);
1716017e 114 lockinit(&rman->lock, "ttmrman", 0, LK_CANRECURSE);
5c9d63cf
FT
115 man->priv = rman;
116 return 0;
117}
118
119static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man)
120{
121 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
122 struct drm_mm *mm = &rman->mm;
123
1716017e 124 lockmgr(&rman->lock, LK_EXCLUSIVE);
5c9d63cf
FT
125 if (drm_mm_clean(mm)) {
126 drm_mm_takedown(mm);
1716017e 127 lockmgr(&rman->lock, LK_RELEASE);
158486a6 128 kfree(rman);
5c9d63cf
FT
129 man->priv = NULL;
130 return 0;
131 }
1716017e 132 lockmgr(&rman->lock, LK_RELEASE);
5c9d63cf
FT
133 return -EBUSY;
134}
135
136static void ttm_bo_man_debug(struct ttm_mem_type_manager *man,
137 const char *prefix)
138{
139 struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv;
140
1716017e 141 lockmgr(&rman->lock, LK_EXCLUSIVE);
5c9d63cf 142 drm_mm_debug_table(&rman->mm, prefix);
1716017e 143 lockmgr(&rman->lock, LK_RELEASE);
5c9d63cf
FT
144}
145
146const struct ttm_mem_type_manager_func ttm_bo_manager_func = {
147 ttm_bo_man_init,
148 ttm_bo_man_takedown,
149 ttm_bo_man_get_node,
150 ttm_bo_man_put_node,
151 ttm_bo_man_debug
152};
c19c6249 153EXPORT_SYMBOL(ttm_bo_manager_func);