drm_sman.c 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/**************************************************************************
 *
 * Copyright 2006 Tungsten Graphics, Inc., Bismarck., ND., USA.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 *
 **************************************************************************/
/*
 * Simple memory manager interface that keeps track on allocate regions on a
 * per "owner" basis. All regions associated with an "owner" can be released
 * with a simple call. Typically if the "owner" exists. The owner is any
 * "unsigned long" identifier. Can typically be a pointer to a file private
 * struct or a context identifier.
 *
 * Authors:
36
 * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
37 38 39 40
 */

#include "drm_sman.h"

41 42
struct drm_owner_item {
	struct drm_hash_item owner_hash;
43 44
	struct list_head sman_list;
	struct list_head mem_blocks;
45
};
46

D
Dave Airlie 已提交
47
void drm_sman_takedown(struct drm_sman * sman)
48 49 50
{
	drm_ht_remove(&sman->user_hash_tab);
	drm_ht_remove(&sman->owner_hash_tab);
51
	kfree(sman->mm);
52 53 54 55 56
}

EXPORT_SYMBOL(drm_sman_takedown);

int
D
Dave Airlie 已提交
57
drm_sman_init(struct drm_sman * sman, unsigned int num_managers,
58 59 60 61
	      unsigned int user_order, unsigned int owner_order)
{
	int ret = 0;

62
	sman->mm = kcalloc(num_managers, sizeof(*sman->mm), GFP_KERNEL);
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	if (!sman->mm) {
		ret = -ENOMEM;
		goto out;
	}
	sman->num_managers = num_managers;
	INIT_LIST_HEAD(&sman->owner_items);
	ret = drm_ht_create(&sman->owner_hash_tab, owner_order);
	if (ret)
		goto out1;
	ret = drm_ht_create(&sman->user_hash_tab, user_order);
	if (!ret)
		goto out;

	drm_ht_remove(&sman->owner_hash_tab);
out1:
78
	kfree(sman->mm);
79 80 81 82 83 84 85 86 87
out:
	return ret;
}

EXPORT_SYMBOL(drm_sman_init);

static void *drm_sman_mm_allocate(void *private, unsigned long size,
				  unsigned alignment)
{
D
Dave Airlie 已提交
88 89
	struct drm_mm *mm = (struct drm_mm *) private;
	struct drm_mm_node *tmp;
90

91
	tmp = drm_mm_search_free(mm, size, alignment, 1);
92 93 94 95 96 97 98 99 100
	if (!tmp) {
		return NULL;
	}
	tmp = drm_mm_get_block(tmp, size, alignment);
	return tmp;
}

static void drm_sman_mm_free(void *private, void *ref)
{
D
Dave Airlie 已提交
101
	struct drm_mm_node *node = (struct drm_mm_node *) ref;
102

103
	drm_mm_put_block(node);
104 105 106 107
}

static void drm_sman_mm_destroy(void *private)
{
D
Dave Airlie 已提交
108
	struct drm_mm *mm = (struct drm_mm *) private;
109
	drm_mm_takedown(mm);
110
	kfree(mm);
111 112
}

A
Adrian Bunk 已提交
113
static unsigned long drm_sman_mm_offset(void *private, void *ref)
114
{
D
Dave Airlie 已提交
115
	struct drm_mm_node *node = (struct drm_mm_node *) ref;
116 117 118 119
	return node->start;
}

int
D
Dave Airlie 已提交
120
drm_sman_set_range(struct drm_sman * sman, unsigned int manager,
121 122
		   unsigned long start, unsigned long size)
{
D
Dave Airlie 已提交
123
	struct drm_sman_mm *sman_mm;
D
Dave Airlie 已提交
124
	struct drm_mm *mm;
125 126 127 128 129
	int ret;

	BUG_ON(manager >= sman->num_managers);

	sman_mm = &sman->mm[manager];
130
	mm = kzalloc(sizeof(*mm), GFP_KERNEL);
131 132 133 134 135 136 137
	if (!mm) {
		return -ENOMEM;
	}
	sman_mm->private = mm;
	ret = drm_mm_init(mm, start, size);

	if (ret) {
138
		kfree(mm);
139 140 141 142 143 144 145 146 147 148 149 150 151 152
		return ret;
	}

	sman_mm->allocate = drm_sman_mm_allocate;
	sman_mm->free = drm_sman_mm_free;
	sman_mm->destroy = drm_sman_mm_destroy;
	sman_mm->offset = drm_sman_mm_offset;

	return 0;
}

EXPORT_SYMBOL(drm_sman_set_range);

int
D
Dave Airlie 已提交
153 154
drm_sman_set_manager(struct drm_sman * sman, unsigned int manager,
		     struct drm_sman_mm * allocator)
155 156 157 158 159 160
{
	BUG_ON(manager >= sman->num_managers);
	sman->mm[manager] = *allocator;

	return 0;
}
A
Andrew Morton 已提交
161
EXPORT_SYMBOL(drm_sman_set_manager);
162

163
static struct drm_owner_item *drm_sman_get_owner_item(struct drm_sman * sman,
164 165 166
						 unsigned long owner)
{
	int ret;
167 168
	struct drm_hash_item *owner_hash_item;
	struct drm_owner_item *owner_item;
169 170 171

	ret = drm_ht_find_item(&sman->owner_hash_tab, owner, &owner_hash_item);
	if (!ret) {
172
		return drm_hash_entry(owner_hash_item, struct drm_owner_item,
173 174 175
				      owner_hash);
	}

176
	owner_item = kzalloc(sizeof(*owner_item), GFP_KERNEL);
177 178 179 180 181 182 183 184 185 186 187 188
	if (!owner_item)
		goto out;

	INIT_LIST_HEAD(&owner_item->mem_blocks);
	owner_item->owner_hash.key = owner;
	if (drm_ht_insert_item(&sman->owner_hash_tab, &owner_item->owner_hash))
		goto out1;

	list_add_tail(&owner_item->sman_list, &sman->owner_items);
	return owner_item;

out1:
189
	kfree(owner_item);
190 191 192 193
out:
	return NULL;
}

D
Dave Airlie 已提交
194
struct drm_memblock_item *drm_sman_alloc(struct drm_sman *sman, unsigned int manager,
195 196 197 198
				    unsigned long size, unsigned alignment,
				    unsigned long owner)
{
	void *tmp;
D
Dave Airlie 已提交
199
	struct drm_sman_mm *sman_mm;
200
	struct drm_owner_item *owner_item;
D
Dave Airlie 已提交
201
	struct drm_memblock_item *memblock;
202 203 204 205 206 207 208 209 210 211

	BUG_ON(manager >= sman->num_managers);

	sman_mm = &sman->mm[manager];
	tmp = sman_mm->allocate(sman_mm->private, size, alignment);

	if (!tmp) {
		return NULL;
	}

212
	memblock = kzalloc(sizeof(*memblock), GFP_KERNEL);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

	if (!memblock)
		goto out;

	memblock->mm_info = tmp;
	memblock->mm = sman_mm;
	memblock->sman = sman;

	if (drm_ht_just_insert_please
	    (&sman->user_hash_tab, &memblock->user_hash,
	     (unsigned long)memblock, 32, 0, 0))
		goto out1;

	owner_item = drm_sman_get_owner_item(sman, owner);
	if (!owner_item)
		goto out2;

	list_add_tail(&memblock->owner_list, &owner_item->mem_blocks);

	return memblock;

out2:
	drm_ht_remove_item(&sman->user_hash_tab, &memblock->user_hash);
out1:
237
	kfree(memblock);
238 239 240 241 242 243 244 245
out:
	sman_mm->free(sman_mm->private, tmp);

	return NULL;
}

EXPORT_SYMBOL(drm_sman_alloc);

D
Dave Airlie 已提交
246
static void drm_sman_free(struct drm_memblock_item *item)
247
{
D
Dave Airlie 已提交
248
	struct drm_sman *sman = item->sman;
249 250 251 252

	list_del(&item->owner_list);
	drm_ht_remove_item(&sman->user_hash_tab, &item->user_hash);
	item->mm->free(item->mm->private, item->mm_info);
253
	kfree(item);
254 255
}

D
Dave Airlie 已提交
256
int drm_sman_free_key(struct drm_sman *sman, unsigned int key)
257
{
258
	struct drm_hash_item *hash_item;
D
Dave Airlie 已提交
259
	struct drm_memblock_item *memblock_item;
260 261 262 263

	if (drm_ht_find_item(&sman->user_hash_tab, key, &hash_item))
		return -EINVAL;

D
Dave Airlie 已提交
264 265
	memblock_item = drm_hash_entry(hash_item, struct drm_memblock_item,
				       user_hash);
266 267 268 269 270 271
	drm_sman_free(memblock_item);
	return 0;
}

EXPORT_SYMBOL(drm_sman_free_key);

D
Dave Airlie 已提交
272
static void drm_sman_remove_owner(struct drm_sman *sman,
273
				  struct drm_owner_item *owner_item)
274 275 276
{
	list_del(&owner_item->sman_list);
	drm_ht_remove_item(&sman->owner_hash_tab, &owner_item->owner_hash);
277
	kfree(owner_item);
278 279
}

D
Dave Airlie 已提交
280
int drm_sman_owner_clean(struct drm_sman *sman, unsigned long owner)
281 282
{

283 284
	struct drm_hash_item *hash_item;
	struct drm_owner_item *owner_item;
285 286 287 288 289

	if (drm_ht_find_item(&sman->owner_hash_tab, owner, &hash_item)) {
		return -1;
	}

290
	owner_item = drm_hash_entry(hash_item, struct drm_owner_item, owner_hash);
291 292 293 294 295 296 297 298 299 300
	if (owner_item->mem_blocks.next == &owner_item->mem_blocks) {
		drm_sman_remove_owner(sman, owner_item);
		return -1;
	}

	return 0;
}

EXPORT_SYMBOL(drm_sman_owner_clean);

D
Dave Airlie 已提交
301
static void drm_sman_do_owner_cleanup(struct drm_sman *sman,
302
				      struct drm_owner_item *owner_item)
303
{
D
Dave Airlie 已提交
304
	struct drm_memblock_item *entry, *next;
305 306 307 308 309 310 311 312

	list_for_each_entry_safe(entry, next, &owner_item->mem_blocks,
				 owner_list) {
		drm_sman_free(entry);
	}
	drm_sman_remove_owner(sman, owner_item);
}

D
Dave Airlie 已提交
313
void drm_sman_owner_cleanup(struct drm_sman *sman, unsigned long owner)
314 315
{

316 317
	struct drm_hash_item *hash_item;
	struct drm_owner_item *owner_item;
318 319 320 321 322 323

	if (drm_ht_find_item(&sman->owner_hash_tab, owner, &hash_item)) {

		return;
	}

324
	owner_item = drm_hash_entry(hash_item, struct drm_owner_item, owner_hash);
325 326 327 328 329
	drm_sman_do_owner_cleanup(sman, owner_item);
}

EXPORT_SYMBOL(drm_sman_owner_cleanup);

D
Dave Airlie 已提交
330
void drm_sman_cleanup(struct drm_sman *sman)
331
{
332
	struct drm_owner_item *entry, *next;
333
	unsigned int i;
D
Dave Airlie 已提交
334
	struct drm_sman_mm *sman_mm;
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350

	list_for_each_entry_safe(entry, next, &sman->owner_items, sman_list) {
		drm_sman_do_owner_cleanup(sman, entry);
	}
	if (sman->mm) {
		for (i = 0; i < sman->num_managers; ++i) {
			sman_mm = &sman->mm[i];
			if (sman_mm->private) {
				sman_mm->destroy(sman_mm->private);
				sman_mm->private = NULL;
			}
		}
	}
}

EXPORT_SYMBOL(drm_sman_cleanup);