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 * __FBSDID("$FreeBSD: src/sys/dev/drm/drm_sman.c,v 1.1 2010/01/31 14:25:29 rnoland Exp $");
28 **************************************************************************/
31 * Simple memory manager interface that keeps track on allocate regions on a
32 * per "owner" basis. All regions associated with an "owner" can be released
33 * with a simple call. Typically if the "owner" exists. The owner is any
34 * "unsigned long" identifier. Can typically be a pointer to a file private
35 * struct or a context identifier.
38 * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
41 #include "dev/drm/drmP.h"
42 #include "dev/drm/drm_sman.h"
44 struct drm_owner_item
{
45 struct drm_hash_item owner_hash
;
46 struct list_head sman_list
;
47 struct list_head mem_blocks
;
50 void drm_sman_takedown(struct drm_sman
* sman
)
52 drm_ht_remove(&sman
->user_hash_tab
);
53 drm_ht_remove(&sman
->owner_hash_tab
);
55 drm_free(sman
->mm
, sman
->num_managers
* sizeof(*sman
->mm
),
60 drm_sman_init(struct drm_sman
* sman
, unsigned int num_managers
,
61 unsigned int user_order
, unsigned int owner_order
)
65 sman
->mm
= (struct drm_sman_mm
*) drm_calloc(num_managers
,
66 sizeof(*sman
->mm
), DRM_MEM_MM
);
71 sman
->num_managers
= num_managers
;
72 INIT_LIST_HEAD(&sman
->owner_items
);
73 ret
= drm_ht_create(&sman
->owner_hash_tab
, owner_order
);
76 ret
= drm_ht_create(&sman
->user_hash_tab
, user_order
);
80 drm_ht_remove(&sman
->owner_hash_tab
);
82 drm_free(sman
->mm
, num_managers
* sizeof(*sman
->mm
), DRM_MEM_MM
);
87 static void *drm_sman_mm_allocate(void *private, unsigned long size
,
90 struct drm_mm
*mm
= (struct drm_mm
*) private;
91 struct drm_mm_node
*tmp
;
93 tmp
= drm_mm_search_free(mm
, size
, alignment
, 1);
97 tmp
= drm_mm_get_block(tmp
, size
, alignment
);
101 static void drm_sman_mm_free(void *private, void *ref
)
103 struct drm_mm_node
*node
= (struct drm_mm_node
*) ref
;
105 drm_mm_put_block(node
);
108 static void drm_sman_mm_destroy(void *private)
110 struct drm_mm
*mm
= (struct drm_mm
*) private;
112 drm_free(mm
, sizeof(*mm
), DRM_MEM_MM
);
115 static unsigned long drm_sman_mm_offset(void *private, void *ref
)
117 struct drm_mm_node
*node
= (struct drm_mm_node
*) ref
;
122 drm_sman_set_range(struct drm_sman
* sman
, unsigned int manager
,
123 unsigned long start
, unsigned long size
)
125 struct drm_sman_mm
*sman_mm
;
129 KASSERT(manager
< sman
->num_managers
, ("Invalid manager"));
131 sman_mm
= &sman
->mm
[manager
];
132 mm
= malloc(sizeof(*mm
), DRM_MEM_MM
, M_WAITOK
| M_ZERO
);
136 sman_mm
->private = mm
;
137 ret
= drm_mm_init(mm
, start
, size
);
140 drm_free(mm
, sizeof(*mm
), DRM_MEM_MM
);
144 sman_mm
->allocate
= drm_sman_mm_allocate
;
145 sman_mm
->free
= drm_sman_mm_free
;
146 sman_mm
->destroy
= drm_sman_mm_destroy
;
147 sman_mm
->offset
= drm_sman_mm_offset
;
153 drm_sman_set_manager(struct drm_sman
* sman
, unsigned int manager
,
154 struct drm_sman_mm
* allocator
)
156 KASSERT(manager
< sman
->num_managers
, ("Invalid manager"));
157 sman
->mm
[manager
] = *allocator
;
162 static struct drm_owner_item
*drm_sman_get_owner_item(struct drm_sman
* sman
,
166 struct drm_hash_item
*owner_hash_item
;
167 struct drm_owner_item
*owner_item
;
169 ret
= drm_ht_find_item(&sman
->owner_hash_tab
, owner
, &owner_hash_item
);
171 return drm_hash_entry(owner_hash_item
, struct drm_owner_item
,
175 owner_item
= malloc(sizeof(*owner_item
), DRM_MEM_MM
, M_WAITOK
| M_ZERO
);
179 INIT_LIST_HEAD(&owner_item
->mem_blocks
);
180 owner_item
->owner_hash
.key
= owner
;
181 DRM_DEBUG("owner_item = %p, mem_blocks = %p\n", owner_item
, &owner_item
->mem_blocks
);
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
);
189 drm_free(owner_item
, sizeof(*owner_item
), DRM_MEM_MM
);
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 KASSERT(manager
< sman
->num_managers
, ("Invalid manager"));
205 sman_mm
= &sman
->mm
[manager
];
206 tmp
= sman_mm
->allocate(sman_mm
->private, size
, alignment
);
212 memblock
= malloc(sizeof(*memblock
), DRM_MEM_MM
, M_WAITOK
| M_ZERO
);
213 DRM_DEBUG("allocated mem_block %p\n", memblock
);
217 memblock
->mm_info
= tmp
;
218 memblock
->mm
= sman_mm
;
219 memblock
->sman
= sman
;
220 INIT_LIST_HEAD(&memblock
->owner_list
);
222 if (drm_ht_just_insert_please
223 (&sman
->user_hash_tab
, &memblock
->user_hash
,
224 (unsigned long)memblock
, 32, 0, 0))
227 owner_item
= drm_sman_get_owner_item(sman
, owner
);
231 DRM_DEBUG("owner_item = %p, mem_blocks = %p\n", owner_item
, &owner_item
->mem_blocks
);
232 DRM_DEBUG("owner_list.prev = %p, mem_blocks.prev = %p\n", memblock
->owner_list
.prev
, owner_item
->mem_blocks
.prev
);
233 DRM_DEBUG("owner_list.next = %p, mem_blocks.next = %p\n", memblock
->owner_list
.next
, owner_item
->mem_blocks
.next
);
234 list_add_tail(&memblock
->owner_list
, &owner_item
->mem_blocks
);
236 DRM_DEBUG("Complete\n");
240 drm_ht_remove_item(&sman
->user_hash_tab
, &memblock
->user_hash
);
242 drm_free(memblock
, sizeof(*memblock
), DRM_MEM_MM
);
244 sman_mm
->free(sman_mm
->private, tmp
);
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 static void drm_sman_remove_owner(struct drm_sman
*sman
,
274 struct drm_owner_item
*owner_item
)
276 list_del(&owner_item
->sman_list
);
277 drm_ht_remove_item(&sman
->owner_hash_tab
, &owner_item
->owner_hash
);
278 drm_free(owner_item
, sizeof(*owner_item
), DRM_MEM_MM
);
281 int drm_sman_owner_clean(struct drm_sman
*sman
, unsigned long owner
)
284 struct drm_hash_item
*hash_item
;
285 struct drm_owner_item
*owner_item
;
287 if (drm_ht_find_item(&sman
->owner_hash_tab
, owner
, &hash_item
)) {
291 owner_item
= drm_hash_entry(hash_item
, struct drm_owner_item
, owner_hash
);
292 DRM_DEBUG("cleaning owner_item %p\n", owner_item
);
293 if (owner_item
->mem_blocks
.next
== &owner_item
->mem_blocks
) {
294 drm_sman_remove_owner(sman
, owner_item
);
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_DEBUG("freeing mem_block %p\n", entry
);
309 drm_sman_free(entry
);
311 drm_sman_remove_owner(sman
, owner_item
);
314 void drm_sman_owner_cleanup(struct drm_sman
*sman
, unsigned long owner
)
317 struct drm_hash_item
*hash_item
;
318 struct drm_owner_item
*owner_item
;
320 if (drm_ht_find_item(&sman
->owner_hash_tab
, owner
, &hash_item
)) {
325 owner_item
= drm_hash_entry(hash_item
, struct drm_owner_item
, owner_hash
);
326 drm_sman_do_owner_cleanup(sman
, owner_item
);
329 void drm_sman_cleanup(struct drm_sman
*sman
)
331 struct drm_owner_item
*entry
, *next
;
333 struct drm_sman_mm
*sman_mm
;
335 DRM_DEBUG("sman = %p, owner_items = %p\n",
336 sman
, &sman
->owner_items
);
337 list_for_each_entry_safe(entry
, next
, &sman
->owner_items
, sman_list
) {
338 DRM_DEBUG("cleaning owner_item = %p\n", entry
);
339 drm_sman_do_owner_cleanup(sman
, entry
);
342 for (i
= 0; i
< sman
->num_managers
; ++i
) {
343 sman_mm
= &sman
->mm
[i
];
344 if (sman_mm
->private) {
345 sman_mm
->destroy(sman_mm
->private);
346 sman_mm
->private = NULL
;