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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
|
/* Copyright (c) 2008-2016, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/*
* KGSL command batch management
* A command batch is a single submission from userland. The cmdbatch
* encapsulates everything about the submission : command buffers, flags and
* sync points.
*
* Sync points are events that need to expire before the
* cmdbatch can be queued to the hardware. All synpoints are contained in an
* array of kgsl_cmdbatch_sync_event structs in the command batch. There can be
* multiple types of events both internal ones (GPU events) and external
* triggers. As the events expire bits are cleared in a pending bitmap stored
* in the command batch. The GPU will submit the command as soon as the bitmap
* goes to zero indicating no more pending events.
*/
#include <linux/uaccess.h>
#include <linux/list.h>
#include <linux/compat.h>
#include "kgsl.h"
#include "kgsl_device.h"
#include "kgsl_cmdbatch.h"
#include "kgsl_sync.h"
#include "kgsl_trace.h"
#include "kgsl_compat.h"
/*
* Define an kmem cache for the memobj structures since we allocate and free
* them so frequently
*/
static struct kmem_cache *memobjs_cache;
/**
* kgsl_cmdbatch_put() - Decrement the refcount for a command batch object
* @cmdbatch: Pointer to the command batch object
*/
static inline void kgsl_cmdbatch_put(struct kgsl_cmdbatch *cmdbatch)
{
if (cmdbatch)
kref_put(&cmdbatch->refcount, kgsl_cmdbatch_destroy_object);
}
void kgsl_dump_syncpoints(struct kgsl_device *device,
struct kgsl_cmdbatch *cmdbatch)
{
struct kgsl_cmdbatch_sync_event *event;
unsigned int i;
unsigned long flags;
for (i = 0; i < cmdbatch->numsyncs; i++) {
event = &cmdbatch->synclist[i];
if (!kgsl_cmdbatch_event_pending(cmdbatch, i))
continue;
switch (event->type) {
case KGSL_CMD_SYNCPOINT_TYPE_TIMESTAMP: {
unsigned int retired;
kgsl_readtimestamp(event->device,
event->context, KGSL_TIMESTAMP_RETIRED,
&retired);
dev_err(device->dev,
" [timestamp] context %d timestamp %d (retired %d)\n",
event->context->id, event->timestamp,
retired);
break;
}
case KGSL_CMD_SYNCPOINT_TYPE_FENCE:
spin_lock_irqsave(&event->handle_lock, flags);
if (event->handle)
dev_err(device->dev, " fence: [%pK] %s\n",
event->handle->fence,
event->handle->name);
else
dev_err(device->dev, " fence: invalid\n");
spin_unlock_irqrestore(&event->handle_lock, flags);
break;
}
}
}
static void _kgsl_cmdbatch_timer(unsigned long data)
{
struct kgsl_device *device;
struct kgsl_cmdbatch *cmdbatch = (struct kgsl_cmdbatch *) data;
struct kgsl_cmdbatch_sync_event *event;
unsigned int i;
unsigned long flags;
if (cmdbatch == NULL || cmdbatch->context == NULL)
return;
device = cmdbatch->context->device;
dev_err(device->dev,
"kgsl: possible gpu syncpoint deadlock for context %d timestamp %d\n",
cmdbatch->context->id, cmdbatch->timestamp);
set_bit(CMDBATCH_FLAG_FENCE_LOG, &cmdbatch->priv);
kgsl_context_dump(cmdbatch->context);
clear_bit(CMDBATCH_FLAG_FENCE_LOG, &cmdbatch->priv);
dev_err(device->dev, " pending events:\n");
for (i = 0; i < cmdbatch->numsyncs; i++) {
event = &cmdbatch->synclist[i];
if (!kgsl_cmdbatch_event_pending(cmdbatch, i))
continue;
switch (event->type) {
case KGSL_CMD_SYNCPOINT_TYPE_TIMESTAMP:
dev_err(device->dev, " [%d] TIMESTAMP %d:%d\n",
i, event->context->id, event->timestamp);
break;
case KGSL_CMD_SYNCPOINT_TYPE_FENCE:
spin_lock_irqsave(&event->handle_lock, flags);
if (event->handle != NULL) {
dev_err(device->dev, " [%d] FENCE %s\n",
i, event->handle->fence ?
event->handle->fence->name : "NULL");
kgsl_sync_fence_log(event->handle->fence);
}
spin_unlock_irqrestore(&event->handle_lock, flags);
break;
}
}
dev_err(device->dev, "--gpu syncpoint deadlock print end--\n");
}
/**
* kgsl_cmdbatch_destroy_object() - Destroy a cmdbatch object
* @kref: Pointer to the kref structure for this object
*
* Actually destroy a command batch object. Called from kgsl_cmdbatch_put
*/
void kgsl_cmdbatch_destroy_object(struct kref *kref)
{
struct kgsl_cmdbatch *cmdbatch = container_of(kref,
struct kgsl_cmdbatch, refcount);
kgsl_context_put(cmdbatch->context);
kfree(cmdbatch->synclist);
kfree(cmdbatch);
}
EXPORT_SYMBOL(kgsl_cmdbatch_destroy_object);
/*
* a generic function to retire a pending sync event and (possibly)
* kick the dispatcher
*/
static void kgsl_cmdbatch_sync_expire(struct kgsl_device *device,
struct kgsl_cmdbatch_sync_event *event)
{
/*
* Clear the event from the pending mask - if it is already clear, then
* leave without doing anything useful
*/
if (!test_and_clear_bit(event->id, &event->cmdbatch->pending))
return;
/*
* If no more pending events, delete the timer and schedule the command
* for dispatch
*/
if (!kgsl_cmdbatch_events_pending(event->cmdbatch)) {
del_timer_sync(&event->cmdbatch->timer);
if (device->ftbl->drawctxt_sched)
device->ftbl->drawctxt_sched(device,
event->cmdbatch->context);
}
}
/*
* This function is called by the GPU event when the sync event timestamp
* expires
*/
static void kgsl_cmdbatch_sync_func(struct kgsl_device *device,
struct kgsl_event_group *group, void *priv, int result)
{
struct kgsl_cmdbatch_sync_event *event = priv;
trace_syncpoint_timestamp_expire(event->cmdbatch,
event->context, event->timestamp);
kgsl_cmdbatch_sync_expire(device, event);
kgsl_context_put(event->context);
kgsl_cmdbatch_put(event->cmdbatch);
}
static inline void _free_memobj_list(struct list_head *list)
{
struct kgsl_memobj_node *mem, *tmpmem;
/* Free the cmd mem here */
list_for_each_entry_safe(mem, tmpmem, list, node) {
list_del_init(&mem->node);
kmem_cache_free(memobjs_cache, mem);
}
}
/**
* kgsl_cmdbatch_destroy() - Destroy a cmdbatch structure
* @cmdbatch: Pointer to the command batch object to destroy
*
* Start the process of destroying a command batch. Cancel any pending events
* and decrement the refcount. Asynchronous events can still signal after
* kgsl_cmdbatch_destroy has returned.
*/
void kgsl_cmdbatch_destroy(struct kgsl_cmdbatch *cmdbatch)
{
unsigned int i;
unsigned long pending, flags;
if (IS_ERR_OR_NULL(cmdbatch))
return;
/* Zap the canary timer */
del_timer_sync(&cmdbatch->timer);
/*
* Copy off the pending list and clear all pending events - this will
* render any subsequent asynchronous callback harmless
*/
bitmap_copy(&pending, &cmdbatch->pending, KGSL_MAX_SYNCPOINTS);
bitmap_zero(&cmdbatch->pending, KGSL_MAX_SYNCPOINTS);
/*
* Clear all pending events - this will render any subsequent async
* callbacks harmless
*/
for (i = 0; i < cmdbatch->numsyncs; i++) {
struct kgsl_cmdbatch_sync_event *event = &cmdbatch->synclist[i];
/* Don't do anything if the event has already expired */
if (!test_bit(i, &pending))
continue;
switch (event->type) {
case KGSL_CMD_SYNCPOINT_TYPE_TIMESTAMP:
kgsl_cancel_event(cmdbatch->device,
&event->context->events, event->timestamp,
kgsl_cmdbatch_sync_func, event);
break;
case KGSL_CMD_SYNCPOINT_TYPE_FENCE:
spin_lock_irqsave(&event->handle_lock, flags);
if (kgsl_sync_fence_async_cancel(event->handle)) {
event->handle = NULL;
spin_unlock_irqrestore(
&event->handle_lock, flags);
kgsl_cmdbatch_put(cmdbatch);
} else {
spin_unlock_irqrestore(
&event->handle_lock, flags);
}
break;
}
}
/*
* Release the the refcount on the mem entry associated with the
* cmdbatch profiling buffer
*/
if (cmdbatch->flags & KGSL_CMDBATCH_PROFILING)
kgsl_mem_entry_put(cmdbatch->profiling_buf_entry);
/* Destroy the cmdlist we created */
_free_memobj_list(&cmdbatch->cmdlist);
/* Destroy the memlist we created */
_free_memobj_list(&cmdbatch->memlist);
/*
* If we cancelled an event, there's a good chance that the context is
* on a dispatcher queue, so schedule to get it removed.
*/
if (!bitmap_empty(&pending, KGSL_MAX_SYNCPOINTS) &&
cmdbatch->device->ftbl->drawctxt_sched)
cmdbatch->device->ftbl->drawctxt_sched(cmdbatch->device,
cmdbatch->context);
kgsl_cmdbatch_put(cmdbatch);
}
EXPORT_SYMBOL(kgsl_cmdbatch_destroy);
/*
* A callback that gets registered with kgsl_sync_fence_async_wait and is fired
* when a fence is expired
*/
static void kgsl_cmdbatch_sync_fence_func(void *priv)
{
unsigned long flags;
struct kgsl_cmdbatch_sync_event *event = priv;
kgsl_cmdbatch_sync_expire(event->device, event);
trace_syncpoint_fence_expire(event->cmdbatch,
event->handle ? event->handle->name : "unknown");
spin_lock_irqsave(&event->handle_lock, flags);
/*
* Setting the event->handle to NULL here make sure that
* other function does not dereference a invalid pointer.
*/
event->handle = NULL;
spin_unlock_irqrestore(&event->handle_lock, flags);
kgsl_cmdbatch_put(event->cmdbatch);
}
/* kgsl_cmdbatch_add_sync_fence() - Add a new sync fence syncpoint
* @device: KGSL device
* @cmdbatch: KGSL cmdbatch to add the sync point to
* @priv: Private sructure passed by the user
*
* Add a new fence sync syncpoint to the cmdbatch.
*/
static int kgsl_cmdbatch_add_sync_fence(struct kgsl_device *device,
struct kgsl_cmdbatch *cmdbatch, void *priv)
{
struct kgsl_cmd_syncpoint_fence *sync = priv;
struct kgsl_cmdbatch_sync_event *event;
unsigned int id;
unsigned long flags;
kref_get(&cmdbatch->refcount);
id = cmdbatch->numsyncs++;
event = &cmdbatch->synclist[id];
event->id = id;
event->type = KGSL_CMD_SYNCPOINT_TYPE_FENCE;
event->cmdbatch = cmdbatch;
event->device = device;
event->context = NULL;
spin_lock_init(&event->handle_lock);
set_bit(event->id, &cmdbatch->pending);
spin_lock_irqsave(&event->handle_lock, flags);
event->handle = kgsl_sync_fence_async_wait(sync->fd,
kgsl_cmdbatch_sync_fence_func, event);
if (IS_ERR_OR_NULL(event->handle)) {
int ret = PTR_ERR(event->handle);
event->handle = NULL;
spin_unlock_irqrestore(&event->handle_lock, flags);
clear_bit(event->id, &cmdbatch->pending);
kgsl_cmdbatch_put(cmdbatch);
/*
* If ret == 0 the fence was already signaled - print a trace
* message so we can track that
*/
if (ret == 0)
trace_syncpoint_fence_expire(cmdbatch, "signaled");
return ret;
} else {
trace_syncpoint_fence(cmdbatch, event->handle->name);
spin_unlock_irqrestore(&event->handle_lock, flags);
}
return 0;
}
/* kgsl_cmdbatch_add_sync_timestamp() - Add a new sync point for a cmdbatch
* @device: KGSL device
* @cmdbatch: KGSL cmdbatch to add the sync point to
* @priv: Private sructure passed by the user
*
* Add a new sync point timestamp event to the cmdbatch.
*/
static int kgsl_cmdbatch_add_sync_timestamp(struct kgsl_device *device,
struct kgsl_cmdbatch *cmdbatch, void *priv)
{
struct kgsl_cmd_syncpoint_timestamp *sync = priv;
struct kgsl_context *context = kgsl_context_get(cmdbatch->device,
sync->context_id);
struct kgsl_cmdbatch_sync_event *event;
int ret = -EINVAL;
unsigned int id;
if (context == NULL)
return -EINVAL;
/*
* We allow somebody to create a sync point on their own context.
* This has the effect of delaying a command from submitting until the
* dependent command has cleared. That said we obviously can't let them
* create a sync point on a future timestamp.
*/
if (context == cmdbatch->context) {
unsigned int queued;
kgsl_readtimestamp(device, context, KGSL_TIMESTAMP_QUEUED,
&queued);
if (timestamp_cmp(sync->timestamp, queued) > 0) {
KGSL_DRV_ERR(device,
"Cannot create syncpoint for future timestamp %d (current %d)\n",
sync->timestamp, queued);
goto done;
}
}
kref_get(&cmdbatch->refcount);
id = cmdbatch->numsyncs++;
event = &cmdbatch->synclist[id];
event->id = id;
event->type = KGSL_CMD_SYNCPOINT_TYPE_TIMESTAMP;
event->cmdbatch = cmdbatch;
event->context = context;
event->timestamp = sync->timestamp;
event->device = device;
set_bit(event->id, &cmdbatch->pending);
ret = kgsl_add_event(device, &context->events, sync->timestamp,
kgsl_cmdbatch_sync_func, event);
if (ret) {
clear_bit(event->id, &cmdbatch->pending);
kgsl_cmdbatch_put(cmdbatch);
} else {
trace_syncpoint_timestamp(cmdbatch, context, sync->timestamp);
}
done:
if (ret)
kgsl_context_put(context);
return ret;
}
/**
* kgsl_cmdbatch_add_sync() - Add a sync point to a command batch
* @device: Pointer to the KGSL device struct for the GPU
* @cmdbatch: Pointer to the cmdbatch
* @sync: Pointer to the user-specified struct defining the syncpoint
*
* Create a new sync point in the cmdbatch based on the user specified
* parameters
*/
int kgsl_cmdbatch_add_sync(struct kgsl_device *device,
struct kgsl_cmdbatch *cmdbatch,
struct kgsl_cmd_syncpoint *sync)
{
void *priv;
int ret, psize;
int (*func)(struct kgsl_device *device, struct kgsl_cmdbatch *cmdbatch,
void *priv);
switch (sync->type) {
case KGSL_CMD_SYNCPOINT_TYPE_TIMESTAMP:
psize = sizeof(struct kgsl_cmd_syncpoint_timestamp);
func = kgsl_cmdbatch_add_sync_timestamp;
break;
case KGSL_CMD_SYNCPOINT_TYPE_FENCE:
psize = sizeof(struct kgsl_cmd_syncpoint_fence);
func = kgsl_cmdbatch_add_sync_fence;
break;
default:
KGSL_DRV_ERR(device,
"bad syncpoint type ctxt %d type 0x%x size %zu\n",
cmdbatch->context->id, sync->type, sync->size);
return -EINVAL;
}
if (sync->size != psize) {
KGSL_DRV_ERR(device,
"bad syncpoint size ctxt %d type 0x%x size %zu\n",
cmdbatch->context->id, sync->type, sync->size);
return -EINVAL;
}
priv = kzalloc(sync->size, GFP_KERNEL);
if (priv == NULL)
return -ENOMEM;
if (copy_from_user(priv, sync->priv, sync->size)) {
kfree(priv);
return -EFAULT;
}
ret = func(device, cmdbatch, priv);
kfree(priv);
return ret;
}
static void add_profiling_buffer(struct kgsl_device *device,
struct kgsl_cmdbatch *cmdbatch, uint64_t gpuaddr, uint64_t size,
unsigned int id, uint64_t offset)
{
struct kgsl_mem_entry *entry;
if (!(cmdbatch->flags & KGSL_CMDBATCH_PROFILING))
return;
/* Only the first buffer entry counts - ignore the rest */
if (cmdbatch->profiling_buf_entry != NULL)
return;
if (id != 0)
entry = kgsl_sharedmem_find_id(cmdbatch->context->proc_priv,
id);
else
entry = kgsl_sharedmem_find(cmdbatch->context->proc_priv,
gpuaddr);
if (entry != NULL) {
if (!kgsl_gpuaddr_in_memdesc(&entry->memdesc, gpuaddr, size)) {
kgsl_mem_entry_put(entry);
entry = NULL;
}
}
if (entry == NULL) {
KGSL_DRV_ERR(device,
"ignore bad profile buffer ctxt %d id %d offset %lld gpuaddr %llx size %lld\n",
cmdbatch->context->id, id, offset, gpuaddr, size);
return;
}
cmdbatch->profiling_buf_entry = entry;
if (id != 0)
cmdbatch->profiling_buffer_gpuaddr =
entry->memdesc.gpuaddr + offset;
else
cmdbatch->profiling_buffer_gpuaddr = gpuaddr;
}
/**
* kgsl_cmdbatch_add_ibdesc() - Add a legacy ibdesc to a command batch
* @cmdbatch: Pointer to the cmdbatch
* @ibdesc: Pointer to the user-specified struct defining the memory or IB
*
* Create a new memory entry in the cmdbatch based on the user specified
* parameters
*/
int kgsl_cmdbatch_add_ibdesc(struct kgsl_device *device,
struct kgsl_cmdbatch *cmdbatch, struct kgsl_ibdesc *ibdesc)
{
uint64_t gpuaddr = (uint64_t) ibdesc->gpuaddr;
uint64_t size = (uint64_t) ibdesc->sizedwords << 2;
struct kgsl_memobj_node *mem;
/* sanitize the ibdesc ctrl flags */
ibdesc->ctrl &= KGSL_IBDESC_MEMLIST | KGSL_IBDESC_PROFILING_BUFFER;
if (cmdbatch->flags & KGSL_CMDBATCH_MEMLIST &&
ibdesc->ctrl & KGSL_IBDESC_MEMLIST) {
if (ibdesc->ctrl & KGSL_IBDESC_PROFILING_BUFFER) {
add_profiling_buffer(device, cmdbatch,
gpuaddr, size, 0, 0);
return 0;
}
}
if (cmdbatch->flags & (KGSL_CMDBATCH_SYNC | KGSL_CMDBATCH_MARKER))
return 0;
mem = kmem_cache_alloc(memobjs_cache, GFP_KERNEL);
if (mem == NULL)
return -ENOMEM;
mem->gpuaddr = gpuaddr;
mem->size = size;
mem->priv = 0;
mem->id = 0;
mem->offset = 0;
mem->flags = 0;
if (cmdbatch->flags & KGSL_CMDBATCH_MEMLIST &&
ibdesc->ctrl & KGSL_IBDESC_MEMLIST) {
/* add to the memlist */
list_add_tail(&mem->node, &cmdbatch->memlist);
} else {
/* set the preamble flag if directed to */
if (cmdbatch->context->flags & KGSL_CONTEXT_PREAMBLE &&
list_empty(&cmdbatch->cmdlist))
mem->flags = KGSL_CMDLIST_CTXTSWITCH_PREAMBLE;
/* add to the cmd list */
list_add_tail(&mem->node, &cmdbatch->cmdlist);
}
return 0;
}
/**
* kgsl_cmdbatch_create() - Create a new cmdbatch structure
* @device: Pointer to a KGSL device struct
* @context: Pointer to a KGSL context struct
* @flags: Flags for the cmdbatch
*
* Allocate an new cmdbatch structure
*/
struct kgsl_cmdbatch *kgsl_cmdbatch_create(struct kgsl_device *device,
struct kgsl_context *context, unsigned int flags)
{
struct kgsl_cmdbatch *cmdbatch = kzalloc(sizeof(*cmdbatch), GFP_KERNEL);
if (cmdbatch == NULL)
return ERR_PTR(-ENOMEM);
/*
* Increase the reference count on the context so it doesn't disappear
* during the lifetime of this command batch
*/
if (!_kgsl_context_get(context)) {
kfree(cmdbatch);
return ERR_PTR(-ENOENT);
}
kref_init(&cmdbatch->refcount);
INIT_LIST_HEAD(&cmdbatch->cmdlist);
INIT_LIST_HEAD(&cmdbatch->memlist);
cmdbatch->device = device;
cmdbatch->context = context;
/* sanitize our flags for cmdbatches */
cmdbatch->flags = flags & (KGSL_CMDBATCH_CTX_SWITCH
| KGSL_CMDBATCH_MARKER
| KGSL_CMDBATCH_END_OF_FRAME
| KGSL_CMDBATCH_SYNC
| KGSL_CMDBATCH_PWR_CONSTRAINT
| KGSL_CMDBATCH_MEMLIST
| KGSL_CMDBATCH_PROFILING
| KGSL_CMDBATCH_PROFILING_KTIME);
/* Add a timer to help debug sync deadlocks */
setup_timer(&cmdbatch->timer, _kgsl_cmdbatch_timer,
(unsigned long) cmdbatch);
return cmdbatch;
}
#ifdef CONFIG_COMPAT
static int add_ibdesc_list_compat(struct kgsl_device *device,
struct kgsl_cmdbatch *cmdbatch, void __user *ptr, int count)
{
int i, ret = 0;
struct kgsl_ibdesc_compat ibdesc32;
struct kgsl_ibdesc ibdesc;
for (i = 0; i < count; i++) {
memset(&ibdesc32, 0, sizeof(ibdesc32));
if (copy_from_user(&ibdesc32, ptr, sizeof(ibdesc32))) {
ret = -EFAULT;
break;
}
ibdesc.gpuaddr = (unsigned long) ibdesc32.gpuaddr;
ibdesc.sizedwords = (size_t) ibdesc32.sizedwords;
ibdesc.ctrl = (unsigned int) ibdesc32.ctrl;
ret = kgsl_cmdbatch_add_ibdesc(device, cmdbatch, &ibdesc);
if (ret)
break;
ptr += sizeof(ibdesc32);
}
return ret;
}
static int add_syncpoints_compat(struct kgsl_device *device,
struct kgsl_cmdbatch *cmdbatch, void __user *ptr, int count)
{
struct kgsl_cmd_syncpoint_compat sync32;
struct kgsl_cmd_syncpoint sync;
int i, ret = 0;
for (i = 0; i < count; i++) {
memset(&sync32, 0, sizeof(sync32));
if (copy_from_user(&sync32, ptr, sizeof(sync32))) {
ret = -EFAULT;
break;
}
sync.type = sync32.type;
sync.priv = compat_ptr(sync32.priv);
sync.size = (size_t) sync32.size;
ret = kgsl_cmdbatch_add_sync(device, cmdbatch, &sync);
if (ret)
break;
ptr += sizeof(sync32);
}
return ret;
}
#else
static int add_ibdesc_list_compat(struct kgsl_device *device,
struct kgsl_cmdbatch *cmdbatch, void __user *ptr, int count)
{
return -EINVAL;
}
static int add_syncpoints_compat(struct kgsl_device *device,
struct kgsl_cmdbatch *cmdbatch, void __user *ptr, int count)
{
return -EINVAL;
}
#endif
int kgsl_cmdbatch_add_ibdesc_list(struct kgsl_device *device,
struct kgsl_cmdbatch *cmdbatch, void __user *ptr, int count)
{
struct kgsl_ibdesc ibdesc;
int i, ret;
if (is_compat_task())
return add_ibdesc_list_compat(device, cmdbatch, ptr, count);
for (i = 0; i < count; i++) {
memset(&ibdesc, 0, sizeof(ibdesc));
if (copy_from_user(&ibdesc, ptr, sizeof(ibdesc)))
return -EFAULT;
ret = kgsl_cmdbatch_add_ibdesc(device, cmdbatch, &ibdesc);
if (ret)
return ret;
ptr += sizeof(ibdesc);
}
return 0;
}
int kgsl_cmdbatch_add_syncpoints(struct kgsl_device *device,
struct kgsl_cmdbatch *cmdbatch, void __user *ptr, int count)
{
struct kgsl_cmd_syncpoint sync;
int i, ret;
if (count == 0)
return 0;
if (count > KGSL_MAX_SYNCPOINTS)
return -EINVAL;
cmdbatch->synclist = kcalloc(count,
sizeof(struct kgsl_cmdbatch_sync_event), GFP_KERNEL);
if (cmdbatch->synclist == NULL)
return -ENOMEM;
if (is_compat_task())
return add_syncpoints_compat(device, cmdbatch, ptr, count);
for (i = 0; i < count; i++) {
memset(&sync, 0, sizeof(sync));
if (copy_from_user(&sync, ptr, sizeof(sync)))
return -EFAULT;
ret = kgsl_cmdbatch_add_sync(device, cmdbatch, &sync);
if (ret)
return ret;
ptr += sizeof(sync);
}
return 0;
}
static int kgsl_cmdbatch_add_object(struct list_head *head,
struct kgsl_command_object *obj)
{
struct kgsl_memobj_node *mem;
mem = kmem_cache_alloc(memobjs_cache, GFP_KERNEL);
if (mem == NULL)
return -ENOMEM;
mem->gpuaddr = obj->gpuaddr;
mem->size = obj->size;
mem->id = obj->id;
mem->offset = obj->offset;
mem->flags = obj->flags;
mem->priv = 0;
list_add_tail(&mem->node, head);
return 0;
}
#define CMDLIST_FLAGS \
(KGSL_CMDLIST_IB | \
KGSL_CMDLIST_CTXTSWITCH_PREAMBLE | \
KGSL_CMDLIST_IB_PREAMBLE)
int kgsl_cmdbatch_add_cmdlist(struct kgsl_device *device,
struct kgsl_cmdbatch *cmdbatch, void __user *ptr,
unsigned int size, unsigned int count)
{
struct kgsl_command_object obj;
int i, ret = 0;
/* Return early if nothing going on */
if (count == 0 && ptr == NULL && size == 0)
return 0;
/* Sanity check inputs */
if (count == 0 || ptr == NULL || size == 0)
return -EINVAL;
/* Ignore all if SYNC or MARKER is specified */
if (cmdbatch->flags & (KGSL_CMDBATCH_SYNC | KGSL_CMDBATCH_MARKER))
return 0;
for (i = 0; i < count; i++) {
memset(&obj, 0, sizeof(obj));
ret = _copy_from_user(&obj, ptr, sizeof(obj), size);
if (ret)
return ret;
/* Sanity check the flags */
if (!(obj.flags & CMDLIST_FLAGS)) {
KGSL_DRV_ERR(device,
"invalid cmdobj ctxt %d flags %d id %d offset %lld addr %lld size %lld\n",
cmdbatch->context->id, obj.flags, obj.id,
obj.offset, obj.gpuaddr, obj.size);
return -EINVAL;
}
ret = kgsl_cmdbatch_add_object(&cmdbatch->cmdlist, &obj);
if (ret)
return ret;
ptr += sizeof(obj);
}
return 0;
}
int kgsl_cmdbatch_add_memlist(struct kgsl_device *device,
struct kgsl_cmdbatch *cmdbatch, void __user *ptr,
unsigned int size, unsigned int count)
{
struct kgsl_command_object obj;
int i, ret = 0;
/* Return early if nothing going on */
if (count == 0 && ptr == NULL && size == 0)
return 0;
/* Sanity check inputs */
if (count == 0 || ptr == NULL || size == 0)
return -EINVAL;
for (i = 0; i < count; i++) {
memset(&obj, 0, sizeof(obj));
ret = _copy_from_user(&obj, ptr, sizeof(obj), size);
if (ret)
return ret;
if (!(obj.flags & KGSL_OBJLIST_MEMOBJ)) {
KGSL_DRV_ERR(device,
"invalid memobj ctxt %d flags %d id %d offset %lld addr %lld size %lld\n",
cmdbatch->context->id, obj.flags, obj.id,
obj.offset, obj.gpuaddr, obj.size);
return -EINVAL;
}
if (obj.flags & KGSL_OBJLIST_PROFILE)
add_profiling_buffer(device, cmdbatch, obj.gpuaddr,
obj.size, obj.id, obj.offset);
else {
ret = kgsl_cmdbatch_add_object(&cmdbatch->memlist,
&obj);
if (ret)
return ret;
}
ptr += sizeof(obj);
}
return 0;
}
int kgsl_cmdbatch_add_synclist(struct kgsl_device *device,
struct kgsl_cmdbatch *cmdbatch, void __user *ptr,
unsigned int size, unsigned int count)
{
struct kgsl_command_syncpoint syncpoint;
struct kgsl_cmd_syncpoint sync;
int i, ret = 0;
/* Return early if nothing going on */
if (count == 0 && ptr == NULL && size == 0)
return 0;
/* Sanity check inputs */
if (count == 0 || ptr == NULL || size == 0)
return -EINVAL;
if (count > KGSL_MAX_SYNCPOINTS)
return -EINVAL;
cmdbatch->synclist = kcalloc(count,
sizeof(struct kgsl_cmdbatch_sync_event), GFP_KERNEL);
if (cmdbatch->synclist == NULL)
return -ENOMEM;
for (i = 0; i < count; i++) {
memset(&syncpoint, 0, sizeof(syncpoint));
ret = _copy_from_user(&syncpoint, ptr, sizeof(syncpoint), size);
if (ret)
return ret;
sync.type = syncpoint.type;
sync.priv = to_user_ptr(syncpoint.priv);
sync.size = syncpoint.size;
ret = kgsl_cmdbatch_add_sync(device, cmdbatch, &sync);
if (ret)
return ret;
ptr += sizeof(syncpoint);
}
return 0;
}
void kgsl_cmdbatch_exit(void)
{
if (memobjs_cache != NULL)
kmem_cache_destroy(memobjs_cache);
}
int kgsl_cmdbatch_init(void)
{
memobjs_cache = KMEM_CACHE(kgsl_memobj_node, 0);
if (memobjs_cache == NULL) {
KGSL_CORE_ERR("failed to create memobjs_cache");
return -ENOMEM;
}
return 0;
}
|