Commit | Line | Data |
---|---|---|
d961db75 BS |
1 | /************************************************************************** |
2 | * | |
d7a67cb1 | 3 | * Copyright (c) 2007-2010 VMware, Inc., Palo Alto, CA., USA |
d961db75 BS |
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 | ||
760285e7 DH |
31 | #include <drm/ttm/ttm_module.h> |
32 | #include <drm/ttm/ttm_bo_driver.h> | |
33 | #include <drm/ttm/ttm_placement.h> | |
34 | #include <drm/drm_mm.h> | |
d961db75 | 35 | #include <linux/slab.h> |
d7a67cb1 | 36 | #include <linux/spinlock.h> |
d961db75 BS |
37 | #include <linux/module.h> |
38 | ||
d7a67cb1 TH |
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 | spinlock_t lock; | |
48 | }; | |
49 | ||
d961db75 BS |
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, | |
e3f20279 | 53 | uint32_t flags, |
d961db75 BS |
54 | struct ttm_mem_reg *mem) |
55 | { | |
d7a67cb1 TH |
56 | struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv; |
57 | struct drm_mm *mm = &rman->mm; | |
d961db75 | 58 | struct drm_mm_node *node = NULL; |
62347f9e | 59 | enum drm_mm_allocator_flags aflags = DRM_MM_CREATE_DEFAULT; |
d961db75 BS |
60 | unsigned long lpfn; |
61 | int ret; | |
62 | ||
63 | lpfn = placement->lpfn; | |
64 | if (!lpfn) | |
65 | lpfn = man->size; | |
d961db75 | 66 | |
78af329a DH |
67 | node = kzalloc(sizeof(*node), GFP_KERNEL); |
68 | if (!node) | |
69 | return -ENOMEM; | |
70 | ||
e3f20279 | 71 | if (flags & TTM_PL_FLAG_TOPDOWN) |
62347f9e LK |
72 | aflags = DRM_MM_CREATE_TOP; |
73 | ||
78af329a | 74 | spin_lock(&rman->lock); |
62347f9e LK |
75 | ret = drm_mm_insert_node_in_range_generic(mm, node, mem->num_pages, |
76 | mem->page_alignment, 0, | |
78af329a | 77 | placement->fpfn, lpfn, |
62347f9e LK |
78 | DRM_MM_SEARCH_BEST, |
79 | aflags); | |
78af329a DH |
80 | spin_unlock(&rman->lock); |
81 | ||
82 | if (unlikely(ret)) { | |
83 | kfree(node); | |
84 | } else { | |
85 | mem->mm_node = node; | |
86 | mem->start = node->start; | |
87 | } | |
d961db75 | 88 | |
d961db75 BS |
89 | return 0; |
90 | } | |
91 | ||
92 | static void ttm_bo_man_put_node(struct ttm_mem_type_manager *man, | |
93 | struct ttm_mem_reg *mem) | |
94 | { | |
d7a67cb1 | 95 | struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv; |
d961db75 BS |
96 | |
97 | if (mem->mm_node) { | |
d7a67cb1 | 98 | spin_lock(&rman->lock); |
78af329a | 99 | drm_mm_remove_node(mem->mm_node); |
d7a67cb1 | 100 | spin_unlock(&rman->lock); |
78af329a DH |
101 | |
102 | kfree(mem->mm_node); | |
d961db75 BS |
103 | mem->mm_node = NULL; |
104 | } | |
105 | } | |
106 | ||
107 | static int ttm_bo_man_init(struct ttm_mem_type_manager *man, | |
108 | unsigned long p_size) | |
109 | { | |
d7a67cb1 | 110 | struct ttm_range_manager *rman; |
d961db75 | 111 | |
d7a67cb1 TH |
112 | rman = kzalloc(sizeof(*rman), GFP_KERNEL); |
113 | if (!rman) | |
d961db75 BS |
114 | return -ENOMEM; |
115 | ||
77ef8bbc | 116 | drm_mm_init(&rman->mm, 0, p_size); |
d7a67cb1 TH |
117 | spin_lock_init(&rman->lock); |
118 | man->priv = rman; | |
d961db75 BS |
119 | return 0; |
120 | } | |
121 | ||
122 | static int ttm_bo_man_takedown(struct ttm_mem_type_manager *man) | |
123 | { | |
d7a67cb1 TH |
124 | struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv; |
125 | struct drm_mm *mm = &rman->mm; | |
d961db75 | 126 | |
d7a67cb1 | 127 | spin_lock(&rman->lock); |
d961db75 BS |
128 | if (drm_mm_clean(mm)) { |
129 | drm_mm_takedown(mm); | |
d7a67cb1 TH |
130 | spin_unlock(&rman->lock); |
131 | kfree(rman); | |
d961db75 | 132 | man->priv = NULL; |
d7a67cb1 TH |
133 | return 0; |
134 | } | |
135 | spin_unlock(&rman->lock); | |
136 | return -EBUSY; | |
d961db75 BS |
137 | } |
138 | ||
139 | static void ttm_bo_man_debug(struct ttm_mem_type_manager *man, | |
140 | const char *prefix) | |
141 | { | |
d7a67cb1 | 142 | struct ttm_range_manager *rman = (struct ttm_range_manager *) man->priv; |
d961db75 | 143 | |
d7a67cb1 TH |
144 | spin_lock(&rman->lock); |
145 | drm_mm_debug_table(&rman->mm, prefix); | |
146 | spin_unlock(&rman->lock); | |
d961db75 BS |
147 | } |
148 | ||
149 | const struct ttm_mem_type_manager_func ttm_bo_manager_func = { | |
150 | ttm_bo_man_init, | |
151 | ttm_bo_man_takedown, | |
152 | ttm_bo_man_get_node, | |
153 | ttm_bo_man_put_node, | |
154 | ttm_bo_man_debug | |
155 | }; | |
156 | EXPORT_SYMBOL(ttm_bo_manager_func); |