forked from aws/aws-fpga
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxocl_exec.c
1426 lines (1263 loc) · 37 KB
/
xocl_exec.c
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
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2017-2018 Xilinx, Inc
*
* Authors:
* Soren Soe <[email protected]>
*
* A GEM style device manager for PCIe based OpenCL accelerators.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* 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.
*/
#include <linux/bitmap.h>
#include <linux/list.h>
#include <linux/eventfd.h>
#include <linux/kthread.h>
#include "ert.h"
#include "xocl_drv.h"
#include "xocl_exec.h"
#include "xocl_xdma.h"
//#define SCHED_VERBOSE
#define SCHED_THREAD_ENABLE
#if 0
static unsigned long zero = 0;
static unsigned long time_ns(void)
{
struct timeval now;
do_gettimeofday(&now);
if (!zero)
zero = timeval_to_ns(&now);
return timeval_to_ns(&now) - zero;
}
#endif
#define sched_error_on(xdev,expr,msg) \
({ \
unsigned int ret = 0; \
if ((expr)) { \
DRM_INFO("Assertion failed: %s:%d:%s:%s %s\n" \
,__FILE__,__LINE__,__FUNCTION__,#expr,msg); \
xdev->exec.scheduler->error=1; \
ret = 1; \
} \
(ret); \
})
#ifdef SCHED_VERBOSE
# define SCHED_DEBUG(msg) printk(msg)
# define SCHED_DEBUGF(format,...) printk(format, ##__VA_ARGS__)
#else
# define SCHED_DEBUG(msg)
# define SCHED_DEBUGF(format,...)
#endif
#define XOCL_U32_MASK 0xFFFFFFFF
/**
* struct xocl_sched: scheduler for xocl_cmd objects
*
* @scheduler_thread: thread associated with this scheduler
* @use_count: use count for this scheduler
* @wait_queue: conditional wait queue for scheduler thread
* @error: set to 1 to indicate scheduler error
* @command_queue: list of command objects managed by scheduler
* @intc: boolean flag set when there is a pending interrupt for command completion
* @poll: number of running commands in polling mode
*/
struct xocl_sched
{
struct task_struct *scheduler_thread;
unsigned int use_count;
wait_queue_head_t wait_queue;
unsigned int error;
struct list_head command_queue;
atomic_t intc; /* pending interrupt */
atomic_t poll; /* number of cmds to poll */
};
static struct xocl_sched global_scheduler0;
/**
* Command data used by scheduler
*
* @list: command object moves from list to list
* @bo: underlying drm buffer object
* @xdev: device handle
* @xs: scehduler processing this commands
* @state: state of command object per scheduling
* @cu_idx: index of CU executing this cmd object; used in penguin mode only
* @slot_idx: command queue index of this command object
* @packet: mapped ert packet object from user space
*/
struct xocl_cmd
{
struct list_head list;
struct drm_xocl_bo *bo;
struct drm_xocl_dev *xdev;
struct xocl_sched *xs;
enum ert_cmd_state state;
int cu_idx;
int slot_idx;
struct ert_packet *packet;
};
/**
* set_cmd_int_state() - Set internal command state used by scheduler only
*
* @xcmd: command to change internal state on
* @state: new command state per ert.h
*/
inline void
set_cmd_int_state(struct xocl_cmd* xcmd, enum ert_cmd_state state)
{
SCHED_DEBUGF("->set_cmd_int_state(,%d)\n",state);
xcmd->state = state;
SCHED_DEBUG("<-set_cmd_int_state\n");
}
/**
* set_cmd_state() - Set both internal and external state of a command
*
* The state is reflected externally through the command packet
* as well as being captured in internal state variable
*
* @xcmd: command object
* @state: new state
*/
inline void
set_cmd_state(struct xocl_cmd* xcmd, enum ert_cmd_state state)
{
SCHED_DEBUGF("->set_cmd_state(,%d)\n",state);
xcmd->state = state;
xcmd->packet->state = state;
SCHED_DEBUG("<-set_cmd_state\n");
}
/**
* List of free xocl_cmd objects.
*
* @free_cmds: populated with recycled xocl_cmd objects
* @cmd_mutex: mutex lock for cmd_list
*
* Command objects are recycled for later use and only freed when kernel
* module is unloaded.
*/
static LIST_HEAD(free_cmds);
static DEFINE_MUTEX(free_cmds_mutex);
/**
* List of new pending xocl_cmd objects
*
* @pending_cmds: populated from user space with new commands for buffer objects
* @num_pending: number of pending commands
*
* Scheduler copies pending commands to its private queue when necessary
*/
static LIST_HEAD(pending_cmds);
static DEFINE_MUTEX(pending_cmds_mutex);
static atomic_t num_pending = ATOMIC_INIT(0);
/**
* get_free_xocl_cmd() - Get a free command object
*
* Get from free/recycled list or allocate a new command if necessary.
*
* Return: Free command object
*/
static struct xocl_cmd*
get_free_xocl_cmd(void)
{
struct xocl_cmd* cmd;
SCHED_DEBUG("-> get_free_xocl_cmd\n");
mutex_lock(&free_cmds_mutex);
cmd=list_first_entry_or_null(&free_cmds,struct xocl_cmd,list);
if (cmd)
list_del(&cmd->list);
mutex_unlock(&free_cmds_mutex);
if (!cmd)
cmd = kmalloc(sizeof(struct xocl_cmd),GFP_KERNEL);
if (!cmd)
return ERR_PTR(-ENOMEM);
SCHED_DEBUGF("<- get_free_xocl_cmd %p\n",cmd);
return cmd;
}
/**
* add_cmd() - Add a new command to pending list
*
* @xdev: device owning adding the buffer object
* @bo: buffer objects from user space from which new command is created
*
* Scheduler copies pending commands to its internal command queue.
*
* Return: 0 on success, -errno on failure
*/
static int
add_cmd(struct drm_xocl_dev *xdev, struct drm_xocl_bo* bo)
{
struct xocl_cmd *xcmd = get_free_xocl_cmd();
SCHED_DEBUG("-> add_cmd\n");
xcmd->bo=bo;
xcmd->xdev=xdev;
xcmd->cu_idx=-1;
xcmd->slot_idx=-1;
xcmd->packet = (struct ert_packet*)bo->vmapping;
xcmd->xs = xdev->exec.scheduler;
set_cmd_state(xcmd,ERT_CMD_STATE_NEW);
mutex_lock(&pending_cmds_mutex);
list_add_tail(&xcmd->list,&pending_cmds);
mutex_unlock(&pending_cmds_mutex);
/* wake scheduler */
atomic_inc(&num_pending);
wake_up_interruptible(&xcmd->xs->wait_queue);
SCHED_DEBUG("<- add_cmd\n");
return 0;
}
/**
* recycle_cmd() - recycle a command objects
*
* @xcmd: command object to recycle
*
* Command object is added to the freelist
*
* Return: 0
*/
static int
recycle_cmd(struct xocl_cmd* xcmd)
{
SCHED_DEBUGF("recycle %p\n",xcmd);
mutex_lock(&free_cmds_mutex);
list_del(&xcmd->list);
list_add_tail(&xcmd->list,&free_cmds);
mutex_unlock(&free_cmds_mutex);
return 0;
}
/**
* delete_cmd_list() - reclaim memory for all allocated command objects
*/
static void
delete_cmd_list(void)
{
struct xocl_cmd *xcmd;
struct list_head *pos, *next;
mutex_lock(&free_cmds_mutex);
list_for_each_safe(pos, next, &free_cmds) {
xcmd = list_entry(pos, struct xocl_cmd, list);
list_del(pos);
kfree(xcmd);
}
mutex_unlock(&free_cmds_mutex);
}
/**
* struct xocl_sched_ops: scheduler specific operations
*
* Scheduler can operate in MicroBlaze mode (mb/ert) or in penguin mode. This
* struct differentiates specific operations. The struct is per device node,
* meaning that one device can operate in ert mode while another can operate in
* penguin mode.
*/
struct xocl_sched_ops
{
int (*submit) (struct xocl_cmd *xcmd);
void (*query) (struct xocl_cmd *xcmd);
};
static struct xocl_sched_ops mb_ops;
static struct xocl_sched_ops penguin_ops;
/**
* is_ert() - Check if running in embedded (ert) mode.
*
* Return: %true of ert mode, %false otherwise
*/
inline unsigned int
is_ert(struct drm_xocl_dev *xdev)
{
return xdev->exec.ops == &mb_ops;
}
/**
* ffs_or_neg_one() - Find first set bit in a 32 bit mask.
*
* @mask: mask to check
*
* First LSBit is at position 0.
*
* Return: Position of first set bit, or -1 if none
*/
inline int
ffs_or_neg_one(u32 mask)
{
if (!mask)
return -1;
return ffs(mask)-1;
}
/**
* ffz_or_neg_one() - First first zero bit in bit mask
*
* @mask: mask to check
* Return: Position of first zero bit, or -1 if none
*/
inline int
ffz_or_neg_one(u32 mask)
{
if (mask==XOCL_U32_MASK)
return -1;
return ffz(mask);
}
/**
* slot_size() - slot size per device configuration
*
* Return: Command queue slot size
*/
inline unsigned int
slot_size(struct drm_xocl_dev *xdev)
{
return ERT_CQ_SIZE / xdev->exec.num_slots;
}
/**
* cu_mask_idx() - CU mask index for a given cu index
*
* @cu_idx: Global [0..127] index of a CU
* Return: Index of the CU mask containing the CU with cu_idx
*/
inline unsigned int
cu_mask_idx(unsigned int cu_idx)
{
return cu_idx >> 5; /* 32 cus per mask */
}
/**
* cu_idx_in_mask() - CU idx within its mask
*
* @cu_idx: Global [0..127] index of a CU
* Return: Index of the CU within the mask that contains it
*/
inline unsigned int
cu_idx_in_mask(unsigned int cu_idx)
{
return cu_idx - (cu_mask_idx(cu_idx) << 5);
}
/**
* cu_idx_from_mask() - Given CU idx within a mask return its global idx [0..127]
*
* @cu_idx: Index of CU with mask identified by mask_idx
* @mask_idx: Mask index of the has CU with cu_idx
* Return: Global cu_idx [0..127]
*/
inline unsigned int
cu_idx_from_mask(unsigned int cu_idx, unsigned int mask_idx)
{
return cu_idx + (mask_idx << 5);
}
/**
* slot_mask_idx() - Slot mask idx index for a given slot_idx
*
* @slot_idx: Global [0..127] index of a CQ slot
* Return: Index of the slot mask containing the slot_idx
*/
inline unsigned int
slot_mask_idx(unsigned int slot_idx)
{
return slot_idx >> 5;
}
/**
* slot_idx_in_mask() - Index of command queue slot within the mask that contains it
*
* @slot_idx: Global [0..127] index of a CQ slot
* Return: Index of slot within the mask that contains it
*/
inline unsigned int
slot_idx_in_mask(unsigned int slot_idx)
{
return slot_idx - (slot_mask_idx(slot_idx) << 5);
}
/**
* slot_idx_from_mask_idx() - Given slot idx within a mask, return its global idx [0..127]
*
* @slot_idx: Index of slot with mask identified by mask_idx
* @mask_idx: Mask index of the mask hat has slot with slot_idx
* Return: Global slot_idx [0..127]
*/
inline unsigned int
slot_idx_from_mask_idx(unsigned int slot_idx,unsigned int mask_idx)
{
return slot_idx + (mask_idx << 5);
}
/**
* opcode() - Command opcode
*
* @cmd: Command object
* Return: Opcode per command packet
*/
inline u32
opcode(struct xocl_cmd* xcmd)
{
return xcmd->packet->opcode;
}
/**
* payload_size() - Command payload size
*
* @xcmd: Command object
* Return: Size in number of words of command packet payload
*/
inline u32
payload_size(struct xocl_cmd *xcmd)
{
return xcmd->packet->count;
}
/**
* packet_size() - Command packet size
*
* @xcmd: Command object
* Return: Size in number of words of command packet
*/
inline u32
packet_size(struct xocl_cmd *xcmd)
{
return payload_size(xcmd) + 1;
}
/**
* cu_masks() - Number of command packet cu_masks
*
* @xcmd: Command object
* Return: Total number of CU masks in command packet
*/
inline u32
cu_masks(struct xocl_cmd *xcmd)
{
struct ert_start_kernel_cmd *sk;
if (opcode(xcmd)!=ERT_START_KERNEL)
return 0;
sk = (struct ert_start_kernel_cmd *)xcmd->packet;
return 1 + sk->extra_cu_masks;
}
/**
* regmap_size() - Size of regmap is payload size (n) minus the number of cu_masks
*
* @xcmd: Command object
* Return: Size of register map in number of words
*/
inline u32
regmap_size(struct xocl_cmd* xcmd)
{
return payload_size(xcmd) - cu_masks(xcmd);
}
/**
* cu_idx_to_addr() - Convert CU idx into it relative bar address.
*
* @xdev: Device handle
* @cu_idx: Global CU idx
* Return: Address of CU relative to bar
*/
inline u32
cu_idx_to_addr(struct drm_xocl_dev *xdev,unsigned int cu_idx)
{
return (cu_idx << xdev->exec.cu_shift_offset) + xdev->exec.cu_base_addr;
}
/**
* cu_idx_to_bitmask() - Compute the cu bitmask for cu_idx
*
* Subtract 32 * lower bitmasks prior to bitmask repsenting
* this index. For example, f.x cu_idx=67
* 1 << (67 - (67>>5)<<5) =
* 1 << (67 - (2<<5)) =
* 1 << (67 - 64) =
* 1 << 3 =
* 0b1000 for position 4 in third bitmask
*
* @xdev: Device handle
* @cu_idx: Global index [0..127] of CU
*
* This function computes the bitmask for cu_idx in the mask that stores cu_idx
*
* Return: Bitmask with bit set for corresponding CU
*/
inline u32
cu_idx_to_bitmask(struct drm_xocl_dev *xdev, u32 cu_idx)
{
return 1 << (cu_idx - (cu_mask_idx(cu_idx)<<5));
}
/**
* configure() - Configure the scheduler
*
* Process the configure command sent from user space. Only one process can
* configure the scheduler, so if scheduler is already configured, the
* function should verify that another process doesn't expect different
* configuration.
*
* Future may need ability to query current configuration so as to keep
* multiple processes in sync.
*
* Return: 0 on success, 1 on failure
*/
static int
configure(struct xocl_cmd *xcmd)
{
struct drm_xocl_dev *xdev=xcmd->xdev;
struct ert_configure_cmd *cfg;
if (sched_error_on(xdev,opcode(xcmd)!=ERT_CONFIGURE,"expected configure command"))
return 1;
cfg = (struct ert_configure_cmd *)(xcmd->packet);
if (xdev->exec.configured==0) {
SCHED_DEBUG("configuring scheduler\n");
xdev->exec.num_slots = ERT_CQ_SIZE / cfg->slot_size;
xdev->exec.num_cus = cfg->num_cus;
xdev->exec.cu_shift_offset = cfg->cu_shift;
xdev->exec.cu_base_addr = cfg->cu_base_addr;
xdev->exec.num_cu_masks = ((xdev->exec.num_cus-1)>>5) + 1;
if (cfg->ert) {
SCHED_DEBUG("++ configuring embedded scheduler mode\n");
xdev->exec.ops = &mb_ops;
xdev->exec.polling_mode = cfg->polling;
xdev->exec.cq_interrupt = cfg->cq_int;
}
else {
SCHED_DEBUG("++ configuring penguin scheduler mode\n");
xdev->exec.ops = &penguin_ops;
xdev->exec.polling_mode = 1;
}
DRM_INFO("scheduler config ert(%d) slots(%d), cus(%d), cu_shift(%d), cu_base(0x%x), cu_masks(%d)\n"
,is_ert(xdev)
,xdev->exec.num_slots
,xdev->exec.num_cus
,xdev->exec.cu_shift_offset
,xdev->exec.cu_base_addr
,xdev->exec.num_cu_masks);
return 0;
}
DRM_INFO("reconfiguration of scheduler not supported\n");
return 1;
}
/**
* acquire_slot_idx() - Acquire a slot index if available. Update slot status to busy
* so it cannot be reacquired.
*
* This function is called from scheduler thread
*
* Return: Command queue slot index, or -1 if none avaiable
*/
static int
acquire_slot_idx(struct drm_xocl_dev *xdev)
{
unsigned int mask_idx=0, slot_idx=-1;
u32 mask;
SCHED_DEBUG("-> acquire_slot_idx\n");
for (mask_idx=0; mask_idx<xdev->exec.num_slot_masks; ++mask_idx) {
mask = xdev->exec.slot_status[mask_idx];
slot_idx = ffz_or_neg_one(mask);
if (slot_idx==-1 || slot_idx_from_mask_idx(slot_idx,mask_idx)>=xdev->exec.num_slots)
continue;
xdev->exec.slot_status[mask_idx] ^= (1<<slot_idx);
SCHED_DEBUGF("<- acquire_slot_idx returns %d\n",slot_idx_from_mask_idx(slot_idx,mask_idx));
return slot_idx_from_mask_idx(slot_idx,mask_idx);
}
SCHED_DEBUGF("<- acquire_slot_idx returns -1\n");
return -1;
}
/**
* release_slot_idx() - Release a slot index
*
* Update slot status mask for slot index. Notify scheduler in case
* release is via ISR
*
* @xdev: scheduler
* @slot_idx: the slot index to release
*/
static void
release_slot_idx(struct drm_xocl_dev *xdev, unsigned int slot_idx)
{
unsigned int mask_idx = slot_mask_idx(slot_idx);
unsigned int pos = slot_idx_in_mask(slot_idx);
SCHED_DEBUGF("<-> release_slot_idx slot_status[%d]=0x%x, pos=%d\n"
,mask_idx,xdev->exec.slot_status[mask_idx],pos);
xdev->exec.slot_status[mask_idx] ^= (1<<pos);
}
/**
* get_cu_idx() - Get index of CU executing command at idx
*
* This function is called in polling mode only and
* the command at cmd_idx is guaranteed to have been
* started on a CU
*
* Return: Index of CU, or -1 on error
*/
inline unsigned int
get_cu_idx(struct drm_xocl_dev *xdev, unsigned int cmd_idx)
{
struct xocl_cmd *xcmd = xdev->exec.submitted_cmds[cmd_idx];
if (sched_error_on(xdev,!xcmd,"no submtted cmd"))
return -1;
return xcmd->cu_idx;
}
/**
* cu_done() - Check status of CU
*
* @cu_idx: Index of cu to check
*
* This function is called in polling mode only. The cu_idx
* is guaranteed to have been started
*
* Return: %true if cu done, %false otherwise
*/
inline int
cu_done(struct drm_xocl_dev *xdev, unsigned int cu_idx)
{
u32 cu_addr = cu_idx_to_addr(xdev,cu_idx);
SCHED_DEBUGF("-> cu_done(,%d) checks cu at address 0x%x\n",cu_idx,cu_addr);
/* done is indicated by AP_DONE(2) alone or by AP_DONE(2) | AP_IDLE(4)
* but not by AP_IDLE itself. Since 0x10 | (0x10 | 0x100) = 0x110
* checking for 0x10 is sufficient. */
if (ioread32(xdev->user_bar + cu_addr) & 2) {
unsigned int mask_idx = cu_mask_idx(cu_idx);
unsigned int pos = cu_idx_in_mask(cu_idx);
xdev->exec.cu_status[mask_idx] ^= 1<<pos;
SCHED_DEBUG("<- cu_done returns 1\n");
return true;
}
SCHED_DEBUG("<- cu_done returns 0\n");
return false;
}
/**
* cmd_done() - Check of a command is done
*
* @cmd_idx: Slot index of command to check
*
* This function is called in polling mode only. The command
* at cmd_idx is guaranteed to have been started on a CU.
*
* Return: %true if command is done, %false otherwise
*/
inline int
cmd_done(struct drm_xocl_dev *xdev, unsigned int cmd_idx)
{
struct xocl_cmd *xcmd = xdev->exec.submitted_cmds[cmd_idx];
u32 opc = 0;
SCHED_DEBUGF("-> cmd_done(,%d)\n",cmd_idx);
if (sched_error_on(xdev,!xcmd || xcmd->slot_idx!=cmd_idx,"no command or missing slot index"))
return false;
opc = opcode(xcmd);
if (opc==ERT_START_CU) {
int val = cu_done(xdev,get_cu_idx(xdev,cmd_idx));
SCHED_DEBUGF("<- cmd_done (cu_done) returns %d\n",val);
return val;
}
if (opc==ERT_CONFIGURE) {
SCHED_DEBUG("<- cmd_done (configure) returns 1\n");
return true;
}
SCHED_DEBUG("<- cmd_done returns 0\n");
return false;
}
/**
* notify_host() - Notify user space that a command is complete.
*/
static void
notify_host(struct xocl_cmd *xcmd)
{
struct list_head *ptr;
struct drm_xocl_client_ctx *entry;
struct drm_xocl_dev *xdev = xcmd->xdev;
unsigned long flags = 0;
SCHED_DEBUG("-> notify_host\n");
/* now for each client update the trigger counter in the context */
spin_lock_irqsave(&xdev->exec.ctx_list_lock, flags);
list_for_each(ptr, &xdev->exec.ctx_list) {
entry = list_entry(ptr, struct drm_xocl_client_ctx, link);
atomic_inc(&entry->trigger);
}
spin_unlock_irqrestore(&xdev->exec.ctx_list_lock, flags);
/* wake up all the clients */
wake_up_interruptible(&xdev->exec.poll_wait_queue);
SCHED_DEBUG("<- notify_host\n");
}
/**
* mark_cmd_complete() - Move a command to complete state
*
* Commands are marked complete in two ways
* 1. Through polling of CUs or polling of MB status register
* 2. Through interrupts from MB
* In both cases, the completed commands are residing in the completed_cmds
* list and the number of completed commands is reflected in num_completed.
*
* @xcmd: Command to mark complete
*
* The command is removed from the slot it occupies in the device command
* queue. The slot is released so new commands can be submitted. The host
* is notified that some command has completed.
*/
static void
mark_cmd_complete(struct xocl_cmd *xcmd)
{
SCHED_DEBUGF("-> mark_cmd_complete(,%d)\n",xcmd->slot_idx);
xcmd->xdev->exec.submitted_cmds[xcmd->slot_idx] = NULL;
set_cmd_state(xcmd,ERT_CMD_STATE_COMPLETED);
if (xcmd->xdev->exec.polling_mode)
atomic_dec(&xcmd->xs->poll);
release_slot_idx(xcmd->xdev,xcmd->slot_idx);
notify_host(xcmd);
SCHED_DEBUGF("<- mark_cmd_complete\n");
}
/**
* mark_mask_complete() - Move all commands in mask to complete state
*
* @mask: Bitmask with queried statuses of commands
* @mask_idx: Index of the command mask. Used to offset the actual cmd slot index
*/
static void
mark_mask_complete(struct drm_xocl_dev *xdev, u32 mask, unsigned int mask_idx)
{
int bit_idx=0,cmd_idx=0;
SCHED_DEBUGF("-> mark_mask_complete(,0x%x,%d)\n",mask,mask_idx);
if (!mask)
return;
for (bit_idx=0, cmd_idx=mask_idx<<5; bit_idx<32; mask>>=1,++bit_idx,++cmd_idx)
if (mask & 0x1)
mark_cmd_complete(xdev->exec.submitted_cmds[cmd_idx]);
SCHED_DEBUG("<- mark_mask_complete\n");
}
/**
* queued_to_running() - Move a command from queued to running state if possible
*
* @xcmd: Command to start
*
* Upon success, the command is not necessarily running. In ert mode the
* command will have been submitted to the embedded scheduler, whereas in
* penguin mode the command has been started on a CU.
*
* Return: %true if command was submitted to device, %false otherwise
*/
static int
queued_to_running(struct xocl_cmd *xcmd)
{
int retval = false;
SCHED_DEBUG("-> queued_to_running\n");
if (opcode(xcmd)==ERT_CONFIGURE)
configure(xcmd);
if (xcmd->xdev->exec.ops->submit(xcmd)) {
set_cmd_int_state(xcmd,ERT_CMD_STATE_RUNNING);
if (xcmd->xdev->exec.polling_mode)
atomic_inc(&xcmd->xs->poll);
xcmd->xdev->exec.submitted_cmds[xcmd->slot_idx] = xcmd;
retval = true;
}
SCHED_DEBUGF("<- queued_to_running returns %d\n",retval);
return retval;
}
/**
* running_to_complete() - Check status of running commands
*
* @xcmd: Command is in running state
*
* If a command is found to be complete, it marked complete prior to return
* from this function.
*/
static void
running_to_complete(struct xocl_cmd *xcmd)
{
SCHED_DEBUG("-> running_to_complete\n");
xcmd->xdev->exec.ops->query(xcmd);
SCHED_DEBUG("<- running_to_complete\n");
}
/**
* complete_to_free() - Recycle a complete command objects
*
* @xcmd: Command is in complete state
*/
static void
complete_to_free(struct xocl_cmd *xcmd)
{
SCHED_DEBUG("-> complete_to_free\n");
drm_gem_object_unreference_unlocked(&xcmd->bo->base);
recycle_cmd(xcmd);
SCHED_DEBUG("<- complete_to_free\n");
}
/**
* scheduler_queue_cmds() - Queue any pending commands
*
* The scheduler copies pending commands to its internal command queue where
* is is now in queued state.
*/
static void
scheduler_queue_cmds(struct xocl_sched *xs)
{
struct xocl_cmd *xcmd;
SCHED_DEBUG("-> scheduler_queue_cmds\n");
mutex_lock(&pending_cmds_mutex);
while (!list_empty(&pending_cmds)) {
xcmd = list_first_entry(&pending_cmds,struct xocl_cmd,list);
if (xcmd->xs != xs)
continue;
list_del(&xcmd->list);
list_add_tail(&xcmd->list,&xs->command_queue);
set_cmd_int_state(xcmd,ERT_CMD_STATE_QUEUED);
atomic_dec(&num_pending);
}
mutex_unlock(&pending_cmds_mutex);
SCHED_DEBUG("<- scheduler_queue_cmds\n");
}
/**
* scheduler_iterator_cmds() - Iterate all commands in scheduler command queue
*/
static void
scheduler_iterate_cmds(struct xocl_sched *xs)
{
struct xocl_cmd *xcmd;
struct list_head *pos, *next;
SCHED_DEBUG("-> scheduler_iterate_cmds\n");
list_for_each_safe(pos, next, &xs->command_queue) {
xcmd = list_entry(pos, struct xocl_cmd, list);
if (xcmd->state == ERT_CMD_STATE_QUEUED)
queued_to_running(xcmd);
if (xcmd->state == ERT_CMD_STATE_RUNNING)
running_to_complete(xcmd);
if (xcmd->state == ERT_CMD_STATE_COMPLETED)
complete_to_free(xcmd);
}
SCHED_DEBUG("<- scheduler_iterate_cmds\n");
}
/**
* scheduler_wait_condition() - Check status of scheduler wait condition
*
* Scheduler must wait (sleep) if
* 1. there are no pending commands
* 2. no pending interrupt from embedded scheduler
* 3. no pending complete commands in polling mode
*
* Return: 1 if scheduler must wait, 0 othewise
*/
static int
scheduler_wait_condition(struct xocl_sched *xs)
{
if (kthread_should_stop() || xs->error) {
SCHED_DEBUG("scheduler wakes kthread_should_stop\n");
return 0;
}
if (atomic_read(&num_pending)) {
SCHED_DEBUG("scheduler wakes to copy new pending commands\n");
return 0;
}
if (atomic_read(&xs->intc)) {
SCHED_DEBUG("scheduler wakes on interrupt\n");
atomic_set(&xs->intc,0);
return 0;
}
if (atomic_read(&xs->poll)) {
SCHED_DEBUG("scheduler wakes to poll\n");
return 0;
}
SCHED_DEBUG("scheduler waits ...\n");
return 1;
}
/**
* scheduler_wait() - check if scheduler should wait
*
* See scheduler_wait_condition().
*/
static void
scheduler_wait(struct xocl_sched *xs)
{
wait_event_interruptible(xs->wait_queue,scheduler_wait_condition(xs)==0);
}
/**
* scheduler_loop() - Run one loop of the scheduler
*/
static void
scheduler_loop(struct xocl_sched *xs)
{
SCHED_DEBUG("scheduler_loop\n");
scheduler_wait(xs);
if (xs->error) {
DRM_INFO("scheduler encountered unexpected error and exits\n");
return;
}
/* queue new pending commands */
scheduler_queue_cmds(xs);
/* iterate all commands */
scheduler_iterate_cmds(xs);
}
/**
* scheduler() - Command scheduler thread routine
*/
#if defined(__GNUC__) && !defined(SCHED_THREAD_ENABLE)
__attribute__((unused))
#endif
static int
scheduler(void* data)
{
struct xocl_sched *xs = (struct xocl_sched *)data;
while (!kthread_should_stop() && !xs->error)
scheduler_loop(xs);
DRM_INFO("%s:%d scheduler thread exits with value %d\n",__FILE__,__LINE__,xs->error);
return xs->error;
}
/**
* init_scheduler_thread() - Initialize scheduler thread if necessary
*
* Return: 0 on success, -errno otherwise
*/
static int
init_scheduler_thread(void)
{
#ifdef SCHED_THREAD_ENABLE
SCHED_DEBUGF("init_scheduler_thread use_count=%d\n",global_scheduler0.use_count);
if (global_scheduler0.use_count++)
return 0;
init_waitqueue_head(&global_scheduler0.wait_queue);
global_scheduler0.error = 0;
INIT_LIST_HEAD(&global_scheduler0.command_queue);
atomic_set(&global_scheduler0.intc,0);
atomic_set(&global_scheduler0.poll,0);