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
36
37
38
39
40
41
42
43#include "drmP.h"
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59void drm_ctxbitmap_free(struct drm_device * dev, int ctx_handle)
60{
61 mutex_lock(&dev->struct_mutex);
62 idr_remove(&dev->ctx_idr, ctx_handle);
63 mutex_unlock(&dev->struct_mutex);
64}
65
66
67
68
69
70
71
72
73
74
75static int drm_ctxbitmap_next(struct drm_device * dev)
76{
77 int new_id;
78 int ret;
79
80again:
81 if (idr_pre_get(&dev->ctx_idr, GFP_KERNEL) == 0) {
82 DRM_ERROR("Out of memory expanding drawable idr\n");
83 return -ENOMEM;
84 }
85 mutex_lock(&dev->struct_mutex);
86 ret = idr_get_new_above(&dev->ctx_idr, NULL,
87 DRM_RESERVED_CONTEXTS, &new_id);
88 if (ret == -EAGAIN) {
89 mutex_unlock(&dev->struct_mutex);
90 goto again;
91 }
92 mutex_unlock(&dev->struct_mutex);
93 return new_id;
94}
95
96
97
98
99
100
101
102
103int drm_ctxbitmap_init(struct drm_device * dev)
104{
105 idr_init(&dev->ctx_idr);
106 return 0;
107}
108
109
110
111
112
113
114
115
116
117void drm_ctxbitmap_cleanup(struct drm_device * dev)
118{
119 mutex_lock(&dev->struct_mutex);
120 idr_remove_all(&dev->ctx_idr);
121 mutex_unlock(&dev->struct_mutex);
122}
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142int drm_getsareactx(struct drm_device *dev, void *data,
143 struct drm_file *file_priv)
144{
145 struct drm_ctx_priv_map *request = data;
146 struct drm_local_map *map;
147 struct drm_map_list *_entry;
148
149 mutex_lock(&dev->struct_mutex);
150
151 map = idr_find(&dev->ctx_idr, request->ctx_id);
152 if (!map) {
153 mutex_unlock(&dev->struct_mutex);
154 return -EINVAL;
155 }
156
157 request->handle = NULL;
158 list_for_each_entry(_entry, &dev->maplist, head) {
159 if (_entry->map == map) {
160 request->handle =
161 (void *)(unsigned long)_entry->user_token;
162 break;
163 }
164 }
165
166 mutex_unlock(&dev->struct_mutex);
167
168 if (request->handle == NULL)
169 return -EINVAL;
170
171 return 0;
172}
173
174
175
176
177
178
179
180
181
182
183
184
185
186int drm_setsareactx(struct drm_device *dev, void *data,
187 struct drm_file *file_priv)
188{
189 struct drm_ctx_priv_map *request = data;
190 struct drm_local_map *map = NULL;
191 struct drm_map_list *r_list = NULL;
192
193 mutex_lock(&dev->struct_mutex);
194 list_for_each_entry(r_list, &dev->maplist, head) {
195 if (r_list->map
196 && r_list->user_token == (unsigned long) request->handle)
197 goto found;
198 }
199 bad:
200 mutex_unlock(&dev->struct_mutex);
201 return -EINVAL;
202
203 found:
204 map = r_list->map;
205 if (!map)
206 goto bad;
207
208 if (IS_ERR(idr_replace(&dev->ctx_idr, map, request->ctx_id)))
209 goto bad;
210
211 mutex_unlock(&dev->struct_mutex);
212
213 return 0;
214}
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232static int drm_context_switch(struct drm_device * dev, int old, int new)
233{
234 if (test_and_set_bit(0, &dev->context_flag)) {
235 DRM_ERROR("Reentering -- FIXME\n");
236 return -EBUSY;
237 }
238
239 DRM_DEBUG("Context switch from %d to %d\n", old, new);
240
241 if (new == dev->last_context) {
242 clear_bit(0, &dev->context_flag);
243 return 0;
244 }
245
246 return 0;
247}
248
249
250
251
252
253
254
255
256
257
258
259
260static int drm_context_switch_complete(struct drm_device *dev,
261 struct drm_file *file_priv, int new)
262{
263 dev->last_context = new;
264 dev->last_switch = jiffies;
265
266 if (!_DRM_LOCK_IS_HELD(file_priv->master->lock.hw_lock->lock)) {
267 DRM_ERROR("Lock isn't held after context switch\n");
268 }
269
270
271
272
273 clear_bit(0, &dev->context_flag);
274 wake_up(&dev->context_wait);
275
276 return 0;
277}
278
279
280
281
282
283
284
285
286
287
288int drm_resctx(struct drm_device *dev, void *data,
289 struct drm_file *file_priv)
290{
291 struct drm_ctx_res *res = data;
292 struct drm_ctx ctx;
293 int i;
294
295 if (res->count >= DRM_RESERVED_CONTEXTS) {
296 memset(&ctx, 0, sizeof(ctx));
297 for (i = 0; i < DRM_RESERVED_CONTEXTS; i++) {
298 ctx.handle = i;
299 if (copy_to_user(&res->contexts[i], &ctx, sizeof(ctx)))
300 return -EFAULT;
301 }
302 }
303 res->count = DRM_RESERVED_CONTEXTS;
304
305 return 0;
306}
307
308
309
310
311
312
313
314
315
316
317
318
319int drm_addctx(struct drm_device *dev, void *data,
320 struct drm_file *file_priv)
321{
322 struct drm_ctx_list *ctx_entry;
323 struct drm_ctx *ctx = data;
324
325 ctx->handle = drm_ctxbitmap_next(dev);
326 if (ctx->handle == DRM_KERNEL_CONTEXT) {
327
328 ctx->handle = drm_ctxbitmap_next(dev);
329 }
330 DRM_DEBUG("%d\n", ctx->handle);
331 if (ctx->handle == -1) {
332 DRM_DEBUG("Not enough free contexts.\n");
333
334 return -ENOMEM;
335 }
336
337 ctx_entry = kmalloc(sizeof(*ctx_entry), GFP_KERNEL);
338 if (!ctx_entry) {
339 DRM_DEBUG("out of memory\n");
340 return -ENOMEM;
341 }
342
343 INIT_LIST_HEAD(&ctx_entry->head);
344 ctx_entry->handle = ctx->handle;
345 ctx_entry->tag = file_priv;
346
347 mutex_lock(&dev->ctxlist_mutex);
348 list_add(&ctx_entry->head, &dev->ctxlist);
349 ++dev->ctx_count;
350 mutex_unlock(&dev->ctxlist_mutex);
351
352 return 0;
353}
354
355int drm_modctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
356{
357
358 return 0;
359}
360
361
362
363
364
365
366
367
368
369
370int drm_getctx(struct drm_device *dev, void *data, struct drm_file *file_priv)
371{
372 struct drm_ctx *ctx = data;
373
374
375 ctx->flags = 0;
376
377 return 0;
378}
379
380
381
382
383
384
385
386
387
388
389
390
391int drm_switchctx(struct drm_device *dev, void *data,
392 struct drm_file *file_priv)
393{
394 struct drm_ctx *ctx = data;
395
396 DRM_DEBUG("%d\n", ctx->handle);
397 return drm_context_switch(dev, dev->last_context, ctx->handle);
398}
399
400
401
402
403
404
405
406
407
408
409
410
411int drm_newctx(struct drm_device *dev, void *data,
412 struct drm_file *file_priv)
413{
414 struct drm_ctx *ctx = data;
415
416 DRM_DEBUG("%d\n", ctx->handle);
417 drm_context_switch_complete(dev, file_priv, ctx->handle);
418
419 return 0;
420}
421
422
423
424
425
426
427
428
429
430
431
432
433int drm_rmctx(struct drm_device *dev, void *data,
434 struct drm_file *file_priv)
435{
436 struct drm_ctx *ctx = data;
437
438 DRM_DEBUG("%d\n", ctx->handle);
439 if (ctx->handle != DRM_KERNEL_CONTEXT) {
440 if (dev->driver->context_dtor)
441 dev->driver->context_dtor(dev, ctx->handle);
442 drm_ctxbitmap_free(dev, ctx->handle);
443 }
444
445 mutex_lock(&dev->ctxlist_mutex);
446 if (!list_empty(&dev->ctxlist)) {
447 struct drm_ctx_list *pos, *n;
448
449 list_for_each_entry_safe(pos, n, &dev->ctxlist, head) {
450 if (pos->handle == ctx->handle) {
451 list_del(&pos->head);
452 kfree(pos);
453 --dev->ctx_count;
454 }
455 }
456 }
457 mutex_unlock(&dev->ctxlist_mutex);
458
459 return 0;
460}
461
462
463