1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Bismarck., ND., 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 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
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.
36 * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
41 struct drm_owner_item
{
42 struct drm_hash_item owner_hash
;
43 struct list_head sman_list
;
44 struct list_head mem_blocks
;
47 void drm_sman_takedown(struct drm_sman
* sman
)
49 drm_ht_remove(&sman
->user_hash_tab
);
50 drm_ht_remove(&sman
->owner_hash_tab
);
54 EXPORT_SYMBOL(drm_sman_takedown
);
57 drm_sman_init(struct drm_sman
* sman
, unsigned int num_managers
,
58 unsigned int user_order
, unsigned int owner_order
)
62 sman
->mm
= kcalloc(num_managers
, sizeof(*sman
->mm
), GFP_KERNEL
);
67 sman
->num_managers
= num_managers
;
68 INIT_LIST_HEAD(&sman
->owner_items
);
69 ret
= drm_ht_create(&sman
->owner_hash_tab
, owner_order
);
72 ret
= drm_ht_create(&sman
->user_hash_tab
, user_order
);
76 drm_ht_remove(&sman
->owner_hash_tab
);
83 EXPORT_SYMBOL(drm_sman_init
);
85 static void *drm_sman_mm_allocate(void *private, unsigned long size
,
88 struct drm_mm
*mm
= (struct drm_mm
*) private;
89 struct drm_mm_node
*tmp
;
91 tmp
= drm_mm_search_free(mm
, size
, alignment
, 1);
95 tmp
= drm_mm_get_block(tmp
, size
, alignment
);
99 static void drm_sman_mm_free(void *private, void *ref
)
101 struct drm_mm_node
*node
= (struct drm_mm_node
*) ref
;
103 drm_mm_put_block(node
);
106 static void drm_sman_mm_destroy(void *private)
108 struct drm_mm
*mm
= (struct drm_mm
*) private;
113 static unsigned long drm_sman_mm_offset(void *private, void *ref
)
115 struct drm_mm_node
*node
= (struct drm_mm_node
*) ref
;
120 drm_sman_set_range(struct drm_sman
* sman
, unsigned int manager
,
121 unsigned long start
, unsigned long size
)
123 struct drm_sman_mm
*sman_mm
;
127 BUG_ON(manager
>= sman
->num_managers
);
129 sman_mm
= &sman
->mm
[manager
];
130 mm
= kzalloc(sizeof(*mm
), GFP_KERNEL
);
134 sman_mm
->private = mm
;
135 ret
= drm_mm_init(mm
, start
, size
);
142 sman_mm
->allocate
= drm_sman_mm_allocate
;
143 sman_mm
->free
= drm_sman_mm_free
;
144 sman_mm
->destroy
= drm_sman_mm_destroy
;
145 sman_mm
->offset
= drm_sman_mm_offset
;
150 EXPORT_SYMBOL(drm_sman_set_range
);
153 drm_sman_set_manager(struct drm_sman
* sman
, unsigned int manager
,
154 struct drm_sman_mm
* allocator
)
156 BUG_ON(manager
>= sman
->num_managers
);
157 sman
->mm
[manager
] = *allocator
;
161 EXPORT_SYMBOL(drm_sman_set_manager
);
163 static struct drm_owner_item
*drm_sman_get_owner_item(struct drm_sman
* sman
,
167 struct drm_hash_item
*owner_hash_item
;
168 struct drm_owner_item
*owner_item
;
170 ret
= drm_ht_find_item(&sman
->owner_hash_tab
, owner
, &owner_hash_item
);
172 return drm_hash_entry(owner_hash_item
, struct drm_owner_item
,
176 owner_item
= kzalloc(sizeof(*owner_item
), GFP_KERNEL
);
180 INIT_LIST_HEAD(&owner_item
->mem_blocks
);
181 owner_item
->owner_hash
.key
= owner
;
182 if (drm_ht_insert_item(&sman
->owner_hash_tab
, &owner_item
->owner_hash
))
185 list_add_tail(&owner_item
->sman_list
, &sman
->owner_items
);
194 struct drm_memblock_item
*drm_sman_alloc(struct drm_sman
*sman
, unsigned int manager
,
195 unsigned long size
, unsigned alignment
,
199 struct drm_sman_mm
*sman_mm
;
200 struct drm_owner_item
*owner_item
;
201 struct drm_memblock_item
*memblock
;
203 BUG_ON(manager
>= sman
->num_managers
);
205 sman_mm
= &sman
->mm
[manager
];
206 tmp
= sman_mm
->allocate(sman_mm
->private, size
, alignment
);
212 memblock
= kzalloc(sizeof(*memblock
), GFP_KERNEL
);
217 memblock
->mm_info
= tmp
;
218 memblock
->mm
= sman_mm
;
219 memblock
->sman
= sman
;
221 if (drm_ht_just_insert_please
222 (&sman
->user_hash_tab
, &memblock
->user_hash
,
223 (unsigned long)memblock
, 32, 0, 0))
226 owner_item
= drm_sman_get_owner_item(sman
, owner
);
230 list_add_tail(&memblock
->owner_list
, &owner_item
->mem_blocks
);
235 drm_ht_remove_item(&sman
->user_hash_tab
, &memblock
->user_hash
);
239 sman_mm
->free(sman_mm
->private, tmp
);
244 EXPORT_SYMBOL(drm_sman_alloc
);
246 static void drm_sman_free(struct drm_memblock_item
*item
)
248 struct drm_sman
*sman
= item
->sman
;
250 list_del(&item
->owner_list
);
251 drm_ht_remove_item(&sman
->user_hash_tab
, &item
->user_hash
);
252 item
->mm
->free(item
->mm
->private, item
->mm_info
);
256 int drm_sman_free_key(struct drm_sman
*sman
, unsigned int key
)
258 struct drm_hash_item
*hash_item
;
259 struct drm_memblock_item
*memblock_item
;
261 if (drm_ht_find_item(&sman
->user_hash_tab
, key
, &hash_item
))
264 memblock_item
= drm_hash_entry(hash_item
, struct drm_memblock_item
,
266 drm_sman_free(memblock_item
);
270 EXPORT_SYMBOL(drm_sman_free_key
);
272 static void drm_sman_remove_owner(struct drm_sman
*sman
,
273 struct drm_owner_item
*owner_item
)
275 list_del(&owner_item
->sman_list
);
276 drm_ht_remove_item(&sman
->owner_hash_tab
, &owner_item
->owner_hash
);
280 int drm_sman_owner_clean(struct drm_sman
*sman
, unsigned long owner
)
283 struct drm_hash_item
*hash_item
;
284 struct drm_owner_item
*owner_item
;
286 if (drm_ht_find_item(&sman
->owner_hash_tab
, owner
, &hash_item
)) {
290 owner_item
= drm_hash_entry(hash_item
, struct drm_owner_item
, owner_hash
);
291 if (owner_item
->mem_blocks
.next
== &owner_item
->mem_blocks
) {
292 drm_sman_remove_owner(sman
, owner_item
);
299 EXPORT_SYMBOL(drm_sman_owner_clean
);
301 static void drm_sman_do_owner_cleanup(struct drm_sman
*sman
,
302 struct drm_owner_item
*owner_item
)
304 struct drm_memblock_item
*entry
, *next
;
306 list_for_each_entry_safe(entry
, next
, &owner_item
->mem_blocks
,
308 drm_sman_free(entry
);
310 drm_sman_remove_owner(sman
, owner_item
);
313 void drm_sman_owner_cleanup(struct drm_sman
*sman
, unsigned long owner
)
316 struct drm_hash_item
*hash_item
;
317 struct drm_owner_item
*owner_item
;
319 if (drm_ht_find_item(&sman
->owner_hash_tab
, owner
, &hash_item
)) {
324 owner_item
= drm_hash_entry(hash_item
, struct drm_owner_item
, owner_hash
);
325 drm_sman_do_owner_cleanup(sman
, owner_item
);
328 EXPORT_SYMBOL(drm_sman_owner_cleanup
);
330 void drm_sman_cleanup(struct drm_sman
*sman
)
332 struct drm_owner_item
*entry
, *next
;
334 struct drm_sman_mm
*sman_mm
;
336 list_for_each_entry_safe(entry
, next
, &sman
->owner_items
, sman_list
) {
337 drm_sman_do_owner_cleanup(sman
, entry
);
340 for (i
= 0; i
< sman
->num_managers
; ++i
) {
341 sman_mm
= &sman
->mm
[i
];
342 if (sman_mm
->private) {
343 sman_mm
->destroy(sman_mm
->private);
344 sman_mm
->private = NULL
;
350 EXPORT_SYMBOL(drm_sman_cleanup
);