Linux 2.6.20.7
[linux/fpc-iii.git] / drivers / char / drm / drm_sman.c
blob19c81d2e13d01f2cb9cea0be7ecd936294dbed2a
1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Bismarck., ND., USA.
4 * All Rights Reserved.
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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
27 **************************************************************************/
29 * Simple memory manager interface that keeps track on allocate regions on a
30 * per "owner" basis. All regions associated with an "owner" can be released
31 * with a simple call. Typically if the "owner" exists. The owner is any
32 * "unsigned long" identifier. Can typically be a pointer to a file private
33 * struct or a context identifier.
35 * Authors:
36 * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
39 #include "drm_sman.h"
41 typedef struct drm_owner_item {
42 drm_hash_item_t owner_hash;
43 struct list_head sman_list;
44 struct list_head mem_blocks;
45 } drm_owner_item_t;
47 void drm_sman_takedown(drm_sman_t * sman)
49 drm_ht_remove(&sman->user_hash_tab);
50 drm_ht_remove(&sman->owner_hash_tab);
51 if (sman->mm)
52 drm_free(sman->mm, sman->num_managers * sizeof(*sman->mm),
53 DRM_MEM_MM);
56 EXPORT_SYMBOL(drm_sman_takedown);
58 int
59 drm_sman_init(drm_sman_t * sman, unsigned int num_managers,
60 unsigned int user_order, unsigned int owner_order)
62 int ret = 0;
64 sman->mm = (drm_sman_mm_t *) drm_calloc(num_managers, sizeof(*sman->mm),
65 DRM_MEM_MM);
66 if (!sman->mm) {
67 ret = -ENOMEM;
68 goto out;
70 sman->num_managers = num_managers;
71 INIT_LIST_HEAD(&sman->owner_items);
72 ret = drm_ht_create(&sman->owner_hash_tab, owner_order);
73 if (ret)
74 goto out1;
75 ret = drm_ht_create(&sman->user_hash_tab, user_order);
76 if (!ret)
77 goto out;
79 drm_ht_remove(&sman->owner_hash_tab);
80 out1:
81 drm_free(sman->mm, num_managers * sizeof(*sman->mm), DRM_MEM_MM);
82 out:
83 return ret;
86 EXPORT_SYMBOL(drm_sman_init);
88 static void *drm_sman_mm_allocate(void *private, unsigned long size,
89 unsigned alignment)
91 drm_mm_t *mm = (drm_mm_t *) private;
92 drm_mm_node_t *tmp;
94 tmp = drm_mm_search_free(mm, size, alignment, 1);
95 if (!tmp) {
96 return NULL;
98 tmp = drm_mm_get_block(tmp, size, alignment);
99 return tmp;
102 static void drm_sman_mm_free(void *private, void *ref)
104 drm_mm_t *mm = (drm_mm_t *) private;
105 drm_mm_node_t *node = (drm_mm_node_t *) ref;
107 drm_mm_put_block(mm, node);
110 static void drm_sman_mm_destroy(void *private)
112 drm_mm_t *mm = (drm_mm_t *) private;
113 drm_mm_takedown(mm);
114 drm_free(mm, sizeof(*mm), DRM_MEM_MM);
117 static unsigned long drm_sman_mm_offset(void *private, void *ref)
119 drm_mm_node_t *node = (drm_mm_node_t *) ref;
120 return node->start;
124 drm_sman_set_range(drm_sman_t * sman, unsigned int manager,
125 unsigned long start, unsigned long size)
127 drm_sman_mm_t *sman_mm;
128 drm_mm_t *mm;
129 int ret;
131 BUG_ON(manager >= sman->num_managers);
133 sman_mm = &sman->mm[manager];
134 mm = drm_calloc(1, sizeof(*mm), DRM_MEM_MM);
135 if (!mm) {
136 return -ENOMEM;
138 sman_mm->private = mm;
139 ret = drm_mm_init(mm, start, size);
141 if (ret) {
142 drm_free(mm, sizeof(*mm), DRM_MEM_MM);
143 return ret;
146 sman_mm->allocate = drm_sman_mm_allocate;
147 sman_mm->free = drm_sman_mm_free;
148 sman_mm->destroy = drm_sman_mm_destroy;
149 sman_mm->offset = drm_sman_mm_offset;
151 return 0;
154 EXPORT_SYMBOL(drm_sman_set_range);
157 drm_sman_set_manager(drm_sman_t * sman, unsigned int manager,
158 drm_sman_mm_t * allocator)
160 BUG_ON(manager >= sman->num_managers);
161 sman->mm[manager] = *allocator;
163 return 0;
165 EXPORT_SYMBOL(drm_sman_set_manager);
167 static drm_owner_item_t *drm_sman_get_owner_item(drm_sman_t * sman,
168 unsigned long owner)
170 int ret;
171 drm_hash_item_t *owner_hash_item;
172 drm_owner_item_t *owner_item;
174 ret = drm_ht_find_item(&sman->owner_hash_tab, owner, &owner_hash_item);
175 if (!ret) {
176 return drm_hash_entry(owner_hash_item, drm_owner_item_t,
177 owner_hash);
180 owner_item = drm_calloc(1, sizeof(*owner_item), DRM_MEM_MM);
181 if (!owner_item)
182 goto out;
184 INIT_LIST_HEAD(&owner_item->mem_blocks);
185 owner_item->owner_hash.key = owner;
186 if (drm_ht_insert_item(&sman->owner_hash_tab, &owner_item->owner_hash))
187 goto out1;
189 list_add_tail(&owner_item->sman_list, &sman->owner_items);
190 return owner_item;
192 out1:
193 drm_free(owner_item, sizeof(*owner_item), DRM_MEM_MM);
194 out:
195 return NULL;
198 drm_memblock_item_t *drm_sman_alloc(drm_sman_t *sman, unsigned int manager,
199 unsigned long size, unsigned alignment,
200 unsigned long owner)
202 void *tmp;
203 drm_sman_mm_t *sman_mm;
204 drm_owner_item_t *owner_item;
205 drm_memblock_item_t *memblock;
207 BUG_ON(manager >= sman->num_managers);
209 sman_mm = &sman->mm[manager];
210 tmp = sman_mm->allocate(sman_mm->private, size, alignment);
212 if (!tmp) {
213 return NULL;
216 memblock = drm_calloc(1, sizeof(*memblock), DRM_MEM_MM);
218 if (!memblock)
219 goto out;
221 memblock->mm_info = tmp;
222 memblock->mm = sman_mm;
223 memblock->sman = sman;
225 if (drm_ht_just_insert_please
226 (&sman->user_hash_tab, &memblock->user_hash,
227 (unsigned long)memblock, 32, 0, 0))
228 goto out1;
230 owner_item = drm_sman_get_owner_item(sman, owner);
231 if (!owner_item)
232 goto out2;
234 list_add_tail(&memblock->owner_list, &owner_item->mem_blocks);
236 return memblock;
238 out2:
239 drm_ht_remove_item(&sman->user_hash_tab, &memblock->user_hash);
240 out1:
241 drm_free(memblock, sizeof(*memblock), DRM_MEM_MM);
242 out:
243 sman_mm->free(sman_mm->private, tmp);
245 return NULL;
248 EXPORT_SYMBOL(drm_sman_alloc);
250 static void drm_sman_free(drm_memblock_item_t *item)
252 drm_sman_t *sman = item->sman;
254 list_del(&item->owner_list);
255 drm_ht_remove_item(&sman->user_hash_tab, &item->user_hash);
256 item->mm->free(item->mm->private, item->mm_info);
257 drm_free(item, sizeof(*item), DRM_MEM_MM);
260 int drm_sman_free_key(drm_sman_t *sman, unsigned int key)
262 drm_hash_item_t *hash_item;
263 drm_memblock_item_t *memblock_item;
265 if (drm_ht_find_item(&sman->user_hash_tab, key, &hash_item))
266 return -EINVAL;
268 memblock_item = drm_hash_entry(hash_item, drm_memblock_item_t, user_hash);
269 drm_sman_free(memblock_item);
270 return 0;
273 EXPORT_SYMBOL(drm_sman_free_key);
275 static void drm_sman_remove_owner(drm_sman_t *sman,
276 drm_owner_item_t *owner_item)
278 list_del(&owner_item->sman_list);
279 drm_ht_remove_item(&sman->owner_hash_tab, &owner_item->owner_hash);
280 drm_free(owner_item, sizeof(*owner_item), DRM_MEM_MM);
283 int drm_sman_owner_clean(drm_sman_t *sman, unsigned long owner)
286 drm_hash_item_t *hash_item;
287 drm_owner_item_t *owner_item;
289 if (drm_ht_find_item(&sman->owner_hash_tab, owner, &hash_item)) {
290 return -1;
293 owner_item = drm_hash_entry(hash_item, drm_owner_item_t, owner_hash);
294 if (owner_item->mem_blocks.next == &owner_item->mem_blocks) {
295 drm_sman_remove_owner(sman, owner_item);
296 return -1;
299 return 0;
302 EXPORT_SYMBOL(drm_sman_owner_clean);
304 static void drm_sman_do_owner_cleanup(drm_sman_t *sman,
305 drm_owner_item_t *owner_item)
307 drm_memblock_item_t *entry, *next;
309 list_for_each_entry_safe(entry, next, &owner_item->mem_blocks,
310 owner_list) {
311 drm_sman_free(entry);
313 drm_sman_remove_owner(sman, owner_item);
316 void drm_sman_owner_cleanup(drm_sman_t *sman, unsigned long owner)
319 drm_hash_item_t *hash_item;
320 drm_owner_item_t *owner_item;
322 if (drm_ht_find_item(&sman->owner_hash_tab, owner, &hash_item)) {
324 return;
327 owner_item = drm_hash_entry(hash_item, drm_owner_item_t, owner_hash);
328 drm_sman_do_owner_cleanup(sman, owner_item);
331 EXPORT_SYMBOL(drm_sman_owner_cleanup);
333 void drm_sman_cleanup(drm_sman_t *sman)
335 drm_owner_item_t *entry, *next;
336 unsigned int i;
337 drm_sman_mm_t *sman_mm;
339 list_for_each_entry_safe(entry, next, &sman->owner_items, sman_list) {
340 drm_sman_do_owner_cleanup(sman, entry);
342 if (sman->mm) {
343 for (i = 0; i < sman->num_managers; ++i) {
344 sman_mm = &sman->mm[i];
345 if (sman_mm->private) {
346 sman_mm->destroy(sman_mm->private);
347 sman_mm->private = NULL;
353 EXPORT_SYMBOL(drm_sman_cleanup);