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
);
52 drm_free(sman
->mm
, sman
->num_managers
* sizeof(*sman
->mm
),
56 EXPORT_SYMBOL(drm_sman_takedown
);
59 drm_sman_init(struct drm_sman
* sman
, unsigned int num_managers
,
60 unsigned int user_order
, unsigned int owner_order
)
64 sman
->mm
= (struct drm_sman_mm
*) drm_calloc(num_managers
, sizeof(*sman
->mm
),
70 sman
->num_managers
= num_managers
;
71 INIT_LIST_HEAD(&sman
->owner_items
);
72 ret
= drm_ht_create(&sman
->owner_hash_tab
, owner_order
);
75 ret
= drm_ht_create(&sman
->user_hash_tab
, user_order
);
79 drm_ht_remove(&sman
->owner_hash_tab
);
81 drm_free(sman
->mm
, num_managers
* sizeof(*sman
->mm
), DRM_MEM_MM
);
86 EXPORT_SYMBOL(drm_sman_init
);
88 static void *drm_sman_mm_allocate(void *private, unsigned long size
,
91 struct drm_mm
*mm
= (struct drm_mm
*) private;
92 struct drm_mm_node
*tmp
;
94 tmp
= drm_mm_search_free(mm
, size
, alignment
, 1);
98 tmp
= drm_mm_get_block(tmp
, size
, alignment
);
102 static void drm_sman_mm_free(void *private, void *ref
)
104 struct drm_mm_node
*node
= (struct drm_mm_node
*) ref
;
106 drm_mm_put_block(node
);
109 static void drm_sman_mm_destroy(void *private)
111 struct drm_mm
*mm
= (struct drm_mm
*) private;
113 drm_free(mm
, sizeof(*mm
), DRM_MEM_MM
);
116 static unsigned long drm_sman_mm_offset(void *private, void *ref
)
118 struct drm_mm_node
*node
= (struct drm_mm_node
*) ref
;
123 drm_sman_set_range(struct drm_sman
* sman
, unsigned int manager
,
124 unsigned long start
, unsigned long size
)
126 struct drm_sman_mm
*sman_mm
;
130 BUG_ON(manager
>= sman
->num_managers
);
132 sman_mm
= &sman
->mm
[manager
];
133 mm
= drm_calloc(1, sizeof(*mm
), DRM_MEM_MM
);
137 sman_mm
->private = mm
;
138 ret
= drm_mm_init(mm
, start
, size
);
141 drm_free(mm
, sizeof(*mm
), DRM_MEM_MM
);
145 sman_mm
->allocate
= drm_sman_mm_allocate
;
146 sman_mm
->free
= drm_sman_mm_free
;
147 sman_mm
->destroy
= drm_sman_mm_destroy
;
148 sman_mm
->offset
= drm_sman_mm_offset
;
153 EXPORT_SYMBOL(drm_sman_set_range
);
156 drm_sman_set_manager(struct drm_sman
* sman
, unsigned int manager
,
157 struct drm_sman_mm
* allocator
)
159 BUG_ON(manager
>= sman
->num_managers
);
160 sman
->mm
[manager
] = *allocator
;
164 EXPORT_SYMBOL(drm_sman_set_manager
);
166 static struct drm_owner_item
*drm_sman_get_owner_item(struct drm_sman
* sman
,
170 struct drm_hash_item
*owner_hash_item
;
171 struct drm_owner_item
*owner_item
;
173 ret
= drm_ht_find_item(&sman
->owner_hash_tab
, owner
, &owner_hash_item
);
175 return drm_hash_entry(owner_hash_item
, struct drm_owner_item
,
179 owner_item
= drm_calloc(1, sizeof(*owner_item
), DRM_MEM_MM
);
183 INIT_LIST_HEAD(&owner_item
->mem_blocks
);
184 owner_item
->owner_hash
.key
= owner
;
185 if (drm_ht_insert_item(&sman
->owner_hash_tab
, &owner_item
->owner_hash
))
188 list_add_tail(&owner_item
->sman_list
, &sman
->owner_items
);
192 drm_free(owner_item
, sizeof(*owner_item
), DRM_MEM_MM
);
197 struct drm_memblock_item
*drm_sman_alloc(struct drm_sman
*sman
, unsigned int manager
,
198 unsigned long size
, unsigned alignment
,
202 struct drm_sman_mm
*sman_mm
;
203 struct drm_owner_item
*owner_item
;
204 struct drm_memblock_item
*memblock
;
206 BUG_ON(manager
>= sman
->num_managers
);
208 sman_mm
= &sman
->mm
[manager
];
209 tmp
= sman_mm
->allocate(sman_mm
->private, size
, alignment
);
215 memblock
= drm_calloc(1, sizeof(*memblock
), DRM_MEM_MM
);
220 memblock
->mm_info
= tmp
;
221 memblock
->mm
= sman_mm
;
222 memblock
->sman
= sman
;
224 if (drm_ht_just_insert_please
225 (&sman
->user_hash_tab
, &memblock
->user_hash
,
226 (unsigned long)memblock
, 32, 0, 0))
229 owner_item
= drm_sman_get_owner_item(sman
, owner
);
233 list_add_tail(&memblock
->owner_list
, &owner_item
->mem_blocks
);
238 drm_ht_remove_item(&sman
->user_hash_tab
, &memblock
->user_hash
);
240 drm_free(memblock
, sizeof(*memblock
), DRM_MEM_MM
);
242 sman_mm
->free(sman_mm
->private, tmp
);
247 EXPORT_SYMBOL(drm_sman_alloc
);
249 static void drm_sman_free(struct drm_memblock_item
*item
)
251 struct drm_sman
*sman
= item
->sman
;
253 list_del(&item
->owner_list
);
254 drm_ht_remove_item(&sman
->user_hash_tab
, &item
->user_hash
);
255 item
->mm
->free(item
->mm
->private, item
->mm_info
);
256 drm_free(item
, sizeof(*item
), DRM_MEM_MM
);
259 int drm_sman_free_key(struct drm_sman
*sman
, unsigned int key
)
261 struct drm_hash_item
*hash_item
;
262 struct drm_memblock_item
*memblock_item
;
264 if (drm_ht_find_item(&sman
->user_hash_tab
, key
, &hash_item
))
267 memblock_item
= drm_hash_entry(hash_item
, struct drm_memblock_item
,
269 drm_sman_free(memblock_item
);
273 EXPORT_SYMBOL(drm_sman_free_key
);
275 static void drm_sman_remove_owner(struct drm_sman
*sman
,
276 struct drm_owner_item
*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(struct drm_sman
*sman
, unsigned long owner
)
286 struct drm_hash_item
*hash_item
;
287 struct drm_owner_item
*owner_item
;
289 if (drm_ht_find_item(&sman
->owner_hash_tab
, owner
, &hash_item
)) {
293 owner_item
= drm_hash_entry(hash_item
, struct drm_owner_item
, owner_hash
);
294 if (owner_item
->mem_blocks
.next
== &owner_item
->mem_blocks
) {
295 drm_sman_remove_owner(sman
, owner_item
);
302 EXPORT_SYMBOL(drm_sman_owner_clean
);
304 static void drm_sman_do_owner_cleanup(struct drm_sman
*sman
,
305 struct drm_owner_item
*owner_item
)
307 struct drm_memblock_item
*entry
, *next
;
309 list_for_each_entry_safe(entry
, next
, &owner_item
->mem_blocks
,
311 drm_sman_free(entry
);
313 drm_sman_remove_owner(sman
, owner_item
);
316 void drm_sman_owner_cleanup(struct drm_sman
*sman
, unsigned long owner
)
319 struct drm_hash_item
*hash_item
;
320 struct drm_owner_item
*owner_item
;
322 if (drm_ht_find_item(&sman
->owner_hash_tab
, owner
, &hash_item
)) {
327 owner_item
= drm_hash_entry(hash_item
, struct drm_owner_item
, owner_hash
);
328 drm_sman_do_owner_cleanup(sman
, owner_item
);
331 EXPORT_SYMBOL(drm_sman_owner_cleanup
);
333 void drm_sman_cleanup(struct drm_sman
*sman
)
335 struct drm_owner_item
*entry
, *next
;
337 struct drm_sman_mm
*sman_mm
;
339 list_for_each_entry_safe(entry
, next
, &sman
->owner_items
, sman_list
) {
340 drm_sman_do_owner_cleanup(sman
, entry
);
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
);