blob: cebce45f442943efe4202d9f8b4b3763cd2b7ab8 [file] [log] [blame]
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +10001/**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Bismarck., ND., USA.
4 * All Rights Reserved.
5 *
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:
13 *
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.
21 *
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.
25 *
26 *
27 **************************************************************************/
28/*
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.
34 *
35 * Authors:
Jan Engelhardt96de0e22007-10-19 23:21:04 +020036 * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +100037 */
38
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040039#include <linux/export.h>
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +100040#include "drm_sman.h"
41
Dave Airliee0be4282007-07-12 10:26:44 +100042struct drm_owner_item {
43 struct drm_hash_item owner_hash;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +100044 struct list_head sman_list;
45 struct list_head mem_blocks;
Dave Airliee0be4282007-07-12 10:26:44 +100046};
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +100047
Dave Airlie9698b4d2007-07-12 10:21:05 +100048void drm_sman_takedown(struct drm_sman * sman)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +100049{
50 drm_ht_remove(&sman->user_hash_tab);
51 drm_ht_remove(&sman->owner_hash_tab);
Eric Anholt9a298b22009-03-24 12:23:04 -070052 kfree(sman->mm);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +100053}
54
55EXPORT_SYMBOL(drm_sman_takedown);
56
57int
Dave Airlie9698b4d2007-07-12 10:21:05 +100058drm_sman_init(struct drm_sman * sman, unsigned int num_managers,
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +100059 unsigned int user_order, unsigned int owner_order)
60{
61 int ret = 0;
62
Jesper Juhl288db882010-11-09 00:08:25 +010063 sman->mm = kcalloc(num_managers, sizeof(*sman->mm), GFP_KERNEL);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +100064 if (!sman->mm) {
65 ret = -ENOMEM;
66 goto out;
67 }
68 sman->num_managers = num_managers;
69 INIT_LIST_HEAD(&sman->owner_items);
70 ret = drm_ht_create(&sman->owner_hash_tab, owner_order);
71 if (ret)
72 goto out1;
73 ret = drm_ht_create(&sman->user_hash_tab, user_order);
74 if (!ret)
75 goto out;
76
77 drm_ht_remove(&sman->owner_hash_tab);
78out1:
Eric Anholt9a298b22009-03-24 12:23:04 -070079 kfree(sman->mm);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +100080out:
81 return ret;
82}
83
84EXPORT_SYMBOL(drm_sman_init);
85
86static void *drm_sman_mm_allocate(void *private, unsigned long size,
87 unsigned alignment)
88{
Dave Airlie55910512007-07-11 16:53:40 +100089 struct drm_mm *mm = (struct drm_mm *) private;
90 struct drm_mm_node *tmp;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +100091
Andrew Mortona1d0fcf2006-08-14 11:35:15 +100092 tmp = drm_mm_search_free(mm, size, alignment, 1);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +100093 if (!tmp) {
94 return NULL;
95 }
96 tmp = drm_mm_get_block(tmp, size, alignment);
97 return tmp;
98}
99
100static void drm_sman_mm_free(void *private, void *ref)
101{
Dave Airlie55910512007-07-11 16:53:40 +1000102 struct drm_mm_node *node = (struct drm_mm_node *) ref;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000103
Thomas Hellstrom1d584202007-01-08 22:25:47 +1100104 drm_mm_put_block(node);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000105}
106
107static void drm_sman_mm_destroy(void *private)
108{
Dave Airlie55910512007-07-11 16:53:40 +1000109 struct drm_mm *mm = (struct drm_mm *) private;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000110 drm_mm_takedown(mm);
Eric Anholt9a298b22009-03-24 12:23:04 -0700111 kfree(mm);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000112}
113
Adrian Bunkfb41e542006-08-16 11:55:18 +1000114static unsigned long drm_sman_mm_offset(void *private, void *ref)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000115{
Dave Airlie55910512007-07-11 16:53:40 +1000116 struct drm_mm_node *node = (struct drm_mm_node *) ref;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000117 return node->start;
118}
119
120int
Dave Airlie9698b4d2007-07-12 10:21:05 +1000121drm_sman_set_range(struct drm_sman * sman, unsigned int manager,
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000122 unsigned long start, unsigned long size)
123{
Dave Airlie9698b4d2007-07-12 10:21:05 +1000124 struct drm_sman_mm *sman_mm;
Dave Airlie55910512007-07-11 16:53:40 +1000125 struct drm_mm *mm;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000126 int ret;
127
128 BUG_ON(manager >= sman->num_managers);
129
130 sman_mm = &sman->mm[manager];
Eric Anholt9a298b22009-03-24 12:23:04 -0700131 mm = kzalloc(sizeof(*mm), GFP_KERNEL);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000132 if (!mm) {
133 return -ENOMEM;
134 }
135 sman_mm->private = mm;
136 ret = drm_mm_init(mm, start, size);
137
138 if (ret) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700139 kfree(mm);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000140 return ret;
141 }
142
143 sman_mm->allocate = drm_sman_mm_allocate;
144 sman_mm->free = drm_sman_mm_free;
145 sman_mm->destroy = drm_sman_mm_destroy;
146 sman_mm->offset = drm_sman_mm_offset;
147
148 return 0;
149}
150
151EXPORT_SYMBOL(drm_sman_set_range);
152
153int
Dave Airlie9698b4d2007-07-12 10:21:05 +1000154drm_sman_set_manager(struct drm_sman * sman, unsigned int manager,
155 struct drm_sman_mm * allocator)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000156{
157 BUG_ON(manager >= sman->num_managers);
158 sman->mm[manager] = *allocator;
159
160 return 0;
161}
Andrew Mortona1e85372006-12-06 20:31:33 -0800162EXPORT_SYMBOL(drm_sman_set_manager);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000163
Dave Airliee0be4282007-07-12 10:26:44 +1000164static struct drm_owner_item *drm_sman_get_owner_item(struct drm_sman * sman,
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000165 unsigned long owner)
166{
167 int ret;
Dave Airliee0be4282007-07-12 10:26:44 +1000168 struct drm_hash_item *owner_hash_item;
169 struct drm_owner_item *owner_item;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000170
171 ret = drm_ht_find_item(&sman->owner_hash_tab, owner, &owner_hash_item);
172 if (!ret) {
Dave Airliee0be4282007-07-12 10:26:44 +1000173 return drm_hash_entry(owner_hash_item, struct drm_owner_item,
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000174 owner_hash);
175 }
176
Eric Anholt9a298b22009-03-24 12:23:04 -0700177 owner_item = kzalloc(sizeof(*owner_item), GFP_KERNEL);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000178 if (!owner_item)
179 goto out;
180
181 INIT_LIST_HEAD(&owner_item->mem_blocks);
182 owner_item->owner_hash.key = owner;
183 if (drm_ht_insert_item(&sman->owner_hash_tab, &owner_item->owner_hash))
184 goto out1;
185
186 list_add_tail(&owner_item->sman_list, &sman->owner_items);
187 return owner_item;
188
189out1:
Eric Anholt9a298b22009-03-24 12:23:04 -0700190 kfree(owner_item);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000191out:
192 return NULL;
193}
194
Dave Airlie9698b4d2007-07-12 10:21:05 +1000195struct drm_memblock_item *drm_sman_alloc(struct drm_sman *sman, unsigned int manager,
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000196 unsigned long size, unsigned alignment,
197 unsigned long owner)
198{
199 void *tmp;
Dave Airlie9698b4d2007-07-12 10:21:05 +1000200 struct drm_sman_mm *sman_mm;
Dave Airliee0be4282007-07-12 10:26:44 +1000201 struct drm_owner_item *owner_item;
Dave Airlie9698b4d2007-07-12 10:21:05 +1000202 struct drm_memblock_item *memblock;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000203
204 BUG_ON(manager >= sman->num_managers);
205
206 sman_mm = &sman->mm[manager];
207 tmp = sman_mm->allocate(sman_mm->private, size, alignment);
208
209 if (!tmp) {
210 return NULL;
211 }
212
Eric Anholt9a298b22009-03-24 12:23:04 -0700213 memblock = kzalloc(sizeof(*memblock), GFP_KERNEL);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000214
215 if (!memblock)
216 goto out;
217
218 memblock->mm_info = tmp;
219 memblock->mm = sman_mm;
220 memblock->sman = sman;
221
222 if (drm_ht_just_insert_please
223 (&sman->user_hash_tab, &memblock->user_hash,
224 (unsigned long)memblock, 32, 0, 0))
225 goto out1;
226
227 owner_item = drm_sman_get_owner_item(sman, owner);
228 if (!owner_item)
229 goto out2;
230
231 list_add_tail(&memblock->owner_list, &owner_item->mem_blocks);
232
233 return memblock;
234
235out2:
236 drm_ht_remove_item(&sman->user_hash_tab, &memblock->user_hash);
237out1:
Eric Anholt9a298b22009-03-24 12:23:04 -0700238 kfree(memblock);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000239out:
240 sman_mm->free(sman_mm->private, tmp);
241
242 return NULL;
243}
244
245EXPORT_SYMBOL(drm_sman_alloc);
246
Dave Airlie9698b4d2007-07-12 10:21:05 +1000247static void drm_sman_free(struct drm_memblock_item *item)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000248{
Dave Airlie9698b4d2007-07-12 10:21:05 +1000249 struct drm_sman *sman = item->sman;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000250
251 list_del(&item->owner_list);
252 drm_ht_remove_item(&sman->user_hash_tab, &item->user_hash);
253 item->mm->free(item->mm->private, item->mm_info);
Eric Anholt9a298b22009-03-24 12:23:04 -0700254 kfree(item);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000255}
256
Dave Airlie9698b4d2007-07-12 10:21:05 +1000257int drm_sman_free_key(struct drm_sman *sman, unsigned int key)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000258{
Dave Airliee0be4282007-07-12 10:26:44 +1000259 struct drm_hash_item *hash_item;
Dave Airlie9698b4d2007-07-12 10:21:05 +1000260 struct drm_memblock_item *memblock_item;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000261
262 if (drm_ht_find_item(&sman->user_hash_tab, key, &hash_item))
263 return -EINVAL;
264
Dave Airlie9698b4d2007-07-12 10:21:05 +1000265 memblock_item = drm_hash_entry(hash_item, struct drm_memblock_item,
266 user_hash);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000267 drm_sman_free(memblock_item);
268 return 0;
269}
270
271EXPORT_SYMBOL(drm_sman_free_key);
272
Dave Airlie9698b4d2007-07-12 10:21:05 +1000273static void drm_sman_remove_owner(struct drm_sman *sman,
Dave Airliee0be4282007-07-12 10:26:44 +1000274 struct drm_owner_item *owner_item)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000275{
276 list_del(&owner_item->sman_list);
277 drm_ht_remove_item(&sman->owner_hash_tab, &owner_item->owner_hash);
Eric Anholt9a298b22009-03-24 12:23:04 -0700278 kfree(owner_item);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000279}
280
Dave Airlie9698b4d2007-07-12 10:21:05 +1000281int drm_sman_owner_clean(struct drm_sman *sman, unsigned long owner)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000282{
283
Dave Airliee0be4282007-07-12 10:26:44 +1000284 struct drm_hash_item *hash_item;
285 struct drm_owner_item *owner_item;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000286
287 if (drm_ht_find_item(&sman->owner_hash_tab, owner, &hash_item)) {
288 return -1;
289 }
290
Dave Airliee0be4282007-07-12 10:26:44 +1000291 owner_item = drm_hash_entry(hash_item, struct drm_owner_item, owner_hash);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000292 if (owner_item->mem_blocks.next == &owner_item->mem_blocks) {
293 drm_sman_remove_owner(sman, owner_item);
294 return -1;
295 }
296
297 return 0;
298}
299
300EXPORT_SYMBOL(drm_sman_owner_clean);
301
Dave Airlie9698b4d2007-07-12 10:21:05 +1000302static void drm_sman_do_owner_cleanup(struct drm_sman *sman,
Dave Airliee0be4282007-07-12 10:26:44 +1000303 struct drm_owner_item *owner_item)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000304{
Dave Airlie9698b4d2007-07-12 10:21:05 +1000305 struct drm_memblock_item *entry, *next;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000306
307 list_for_each_entry_safe(entry, next, &owner_item->mem_blocks,
308 owner_list) {
309 drm_sman_free(entry);
310 }
311 drm_sman_remove_owner(sman, owner_item);
312}
313
Dave Airlie9698b4d2007-07-12 10:21:05 +1000314void drm_sman_owner_cleanup(struct drm_sman *sman, unsigned long owner)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000315{
316
Dave Airliee0be4282007-07-12 10:26:44 +1000317 struct drm_hash_item *hash_item;
318 struct drm_owner_item *owner_item;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000319
320 if (drm_ht_find_item(&sman->owner_hash_tab, owner, &hash_item)) {
321
322 return;
323 }
324
Dave Airliee0be4282007-07-12 10:26:44 +1000325 owner_item = drm_hash_entry(hash_item, struct drm_owner_item, owner_hash);
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000326 drm_sman_do_owner_cleanup(sman, owner_item);
327}
328
329EXPORT_SYMBOL(drm_sman_owner_cleanup);
330
Dave Airlie9698b4d2007-07-12 10:21:05 +1000331void drm_sman_cleanup(struct drm_sman *sman)
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000332{
Dave Airliee0be4282007-07-12 10:26:44 +1000333 struct drm_owner_item *entry, *next;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000334 unsigned int i;
Dave Airlie9698b4d2007-07-12 10:21:05 +1000335 struct drm_sman_mm *sman_mm;
Thomas Hellstrom3a1bd922006-08-07 21:30:28 +1000336
337 list_for_each_entry_safe(entry, next, &sman->owner_items, sman_list) {
338 drm_sman_do_owner_cleanup(sman, entry);
339 }
340 if (sman->mm) {
341 for (i = 0; i < sman->num_managers; ++i) {
342 sman_mm = &sman->mm[i];
343 if (sman_mm->private) {
344 sman_mm->destroy(sman_mm->private);
345 sman_mm->private = NULL;
346 }
347 }
348 }
349}
350
351EXPORT_SYMBOL(drm_sman_cleanup);