aboutsummaryrefslogtreecommitdiffstats
path: root/litmus/nvidia_info.c
blob: fd9b0a2a9bc643d1570147da7e9b9b9b8a5006f6 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
/* litmus/nvidia_info.c - routines for:
	1) Determining GPU device ID given NVIDIA GPL-layer tasklet data.
	2) Tracking GPU ownership by task_structs
	3) Managing klmirqd scheduling priority
	4) Handling nvidia tasklets and work_structs

	TODO: Refactor 1&2 and 3&4 into seperate files.
*/

#include <linux/module.h>
#include <linux/semaphore.h>
#include <linux/pci.h>
#include <linux/interrupt.h>
#include <linux/workqueue.h>

#include <litmus/sched_trace.h>
#include <litmus/nvidia_info.h>
#include <litmus/litmus.h>

#include <litmus/sched_plugin.h>

#include <litmus/binheap.h>

#if defined(CONFIG_NV_DRV_331_44)
#define NV_MAJOR_V 331
#define NV_MINOR_V 44
#elif defined(CONFIG_NV_DRV_331_13)
#define NV_MAJOR_V 331
#define NV_MINOR_V 13
#elif defined(CONFIG_NV_DRV_325_15)
#define NV_MAJOR_V 325
#define NV_MINOR_V 15
#elif defined(CONFIG_NV_DRV_319_37)
#define NV_MAJOR_V 319
#define NV_MINOR_V 37
#elif defined(CONFIG_NV_DRV_304_54)
#define NV_MAJOR_V 304
#define NV_MINOR_V 54
#elif defined(CONFIG_NV_DRV_295_40)
#define NV_MAJOR_V 295
#define NV_MINOR_V 40
#elif defined(CONFIG_NV_DRV_270_41)
#define NV_MAJOR_V 279
#define NV_MINOR_V 41
#else
#error "Unsupported NV Driver"
#endif

#if NV_MAJOR_V >= 319
#include <drm/drmP.h>
#endif

/* The following structures map to structers found in the GPL layer
   of the NVIDIA-disributed binary blob driver. Much of the code
   is cobbled together from various versions of the NV driver. We
   can factor this out into a separate tool that gives memory offsets
   to determine the device ID if distributing this code ever becomes
   a problem. */

typedef unsigned char	NvV8;	/* "void": enumerated or multiple fields*/
typedef unsigned short	NvV16;	/* "void": enumerated or multiple fields*/
typedef unsigned char	NvU8;	/* 0 to 255								*/
typedef unsigned short	NvU16;	/* 0 to 65535							*/
typedef signed char		NvS8; 	/* -128 to 127							*/
typedef signed short	NvS16;	/* -32768 to 32767						*/
typedef float			NvF32;	/* IEEE Single Precision (S1E8M23)		*/
typedef double			NvF64;	/* IEEE Double Precision (S1E11M52)		*/
typedef unsigned int	NvV32;	/* "void": enumerated or multiple fields*/
typedef unsigned int	NvU32;	/* 0 to 4294967295						*/
typedef unsigned long long NvU64; /* 0 to 18446744073709551615			*/
typedef NvU8			NvBool;
typedef union
{
	volatile NvV8 Reg008[1];
	volatile NvV16 Reg016[1];
	volatile NvV32 Reg032[1];
} litmus_nv_hwreg_t, * litmus_nv_phwreg_t;

typedef struct
{
	NvU64 address;
#if NV_MAJOR_V >= 295
	NvU64 strapped_size;
#endif
	NvU64 size;
	NvU32 offset;
	NvU32 *map;
	litmus_nv_phwreg_t map_u;
} litmus_nv_aperture_t;

#if NV_MAJOR_V >= 331
typedef struct
{
	NvU32	domain;
	NvU8	bus;
	NvU8	slot;
#if NV_MINOR_V == 44
	NvU8	function;
#endif
	NvU16	vendor_id;
	NvU16	device_id;
	NvBool	valid;
} litmus_pci_info_t;
#endif

typedef struct
{
	void  *priv;				/* private data */
	void  *os_state;			/* os-specific device state */

#if NV_MAJOR_V == 270
	int	   rmInitialized;
#endif
	int	flags;

#if NV_MAJOR_V < 331
	/* PCI config info */
	NvU32 domain;
	NvU16 bus;
	NvU16 slot;
	NvU16 vendor_id;
	NvU16 device_id;
#else
	litmus_pci_info_t pci_info;
#endif

	NvU16 subsystem_id;
	NvU32 gpu_id;
	void *handle;

#if NV_MAJOR_V < 325
	NvU32 pci_cfg_space[16];
#else
	NvU32 pci_cfg_space[64];
#endif

	/* physical characteristics */
	litmus_nv_aperture_t bars[3];
	litmus_nv_aperture_t *regs;
	litmus_nv_aperture_t *fb, ud;

#if NV_MAJOR_V < 325
	litmus_nv_aperture_t agp;
#endif

	NvU32  interrupt_line;

#if NV_MAJOR_V < 325
	NvU32 agp_config;
	NvU32 agp_status;
#endif

	NvU32 primary_vga;

	NvU32 sim_env;

	NvU32 rc_timer_enabled;

	/* list of events allocated for this device */
	void *event_list;

	void *kern_mappings;

} litmus_nv_state_t;

typedef struct work_struct litmus_nv_task_t;

typedef struct litmus_nv_work_s
{
	litmus_nv_task_t task;
	void *data;
} litmus_nv_work_t;

typedef struct litmus_nv_linux_state_s
{
	litmus_nv_state_t nv_state;
	atomic_t usage_count;

	struct pci_dev *dev;

#if NV_MAJOR_V < 325
	void *agp_bridge;
#endif

	void *alloc_queue;

	void *timer_sp;
	void *isr_sp;
	void *pci_cfgchk_sp;
	void *isr_bh_sp;
	char registry_keys[512];

	/* keep track of any pending bottom halfes */
	struct tasklet_struct tasklet;
	litmus_nv_work_t work;

	/* get a timer callback every second */
	struct timer_list rc_timer;

	/* lock for linux-specific data, not used by core rm */
#if !defined(CONFIG_NV_DRV_USES_MUTEX)
	struct semaphore ldata_lock;
#else
	struct mutex ldata_lock;
#endif

#if NV_MAJOR_V >= 331 && NV_MINOR_V >= 44
	struct proc_dir_entry *proc_dir;
#endif

	/* lock for linux-specific alloc queue */
#if !defined(CONFIG_NV_DRV_USES_MUTEX)
	struct semaphore at_lock;
#else
	struct mutex at_lock;
#endif

	/* !!! This field is all that we're after to determine
	   !!! the device number of the GPU that spawned a given
	   vvv tasklet or workqueue item. */
	NvU32 device_num;
	struct litmus_nv_linux_state_s *next;

#if NV_MAJOR_V >= 319
	struct drm_device *drm;
#endif
} litmus_nv_linux_state_t;


#ifdef CONFIG_SCHED_DEBUG_TRACE
static void __attribute__((unused))
dump_nvidia_info(const struct tasklet_struct *t)
{
	litmus_nv_state_t* nvstate = NULL;
	litmus_nv_linux_state_t* linuxstate =  NULL;
	struct pci_dev* pci = NULL;

	nvstate = (litmus_nv_state_t*)(t->data);

	if(nvstate) {
		TRACE("NV State:\n"
			  "\ttasklet ptr = %p\n"
			  "\tstate ptr = %p\n"
			  "\tprivate data ptr = %p\n"
			  "\tos state ptr = %p\n"
			  "\tdomain = %u\n"
			  "\tbus = %u\n"
			  "\tslot = %u\n"
			  "\tvender_id = %u\n"
			  "\tdevice_id = %u\n"
			  "\tsubsystem_id = %u\n"
			  "\tgpu_id = %u\n"
			  "\tinterrupt_line = %u\n",
			  t,
			  nvstate,
			  nvstate->priv,
			  nvstate->os_state,
#if NV_MAJOR_V < 331
			  nvstate->domain,
			  nvstate->bus,
			  nvstate->slot,
			  nvstate->vendor_id,
			  nvstate->device_id,
#else
			  nvstate->pci_info.domain,
			  nvstate->pci_info.bus,
			  nvstate->pci_info.slot,
			  nvstate->pci_info.vendor_id,
			  nvstate->pci_info.device_id,
#endif
			  nvstate->subsystem_id,
			  nvstate->gpu_id,
			  nvstate->interrupt_line);

		linuxstate = container_of(nvstate, litmus_nv_linux_state_t, nv_state);
	}
	else {
		TRACE("INVALID NVSTATE????\n");
	}

	if(linuxstate) {
		int ls_offset =
				(void*)(&(linuxstate->device_num)) -
				(void*)(linuxstate);
		int ns_offset_raw =
				(void*)(&(linuxstate->device_num)) -
				(void*)(&(linuxstate->nv_state));
		int ns_offset_desired =
				(void*)(&(linuxstate->device_num)) -
				(void*)(nvstate);

		TRACE("LINUX NV State:\n"
			  "\tlinux nv state ptr: %p\n"
			  "\taddress of tasklet: %p\n"
			  "\taddress of work: %p\n"
			  "\tusage_count: %d\n"
			  "\tdevice_num: %u\n"
			  "\ttasklet addr == this tasklet: %d\n"
			  "\tpci: %p\n",
			  linuxstate,
			  &(linuxstate->tasklet),
			  &(linuxstate->work),
			  atomic_read(&(linuxstate->usage_count)),
			  linuxstate->device_num,
			  (t == &(linuxstate->tasklet)),
			  linuxstate->dev);

		pci = linuxstate->dev;

		TRACE("Offsets:\n"
			  "\tOffset from LinuxState: %d, %x\n"
			  "\tOffset from NVState: %d, %x\n"
			  "\tOffset from parameter: %d, %x\n"
			  "\tdevice_num: %u\n",
			  ls_offset, ls_offset,
			  ns_offset_raw, ns_offset_raw,
			  ns_offset_desired, ns_offset_desired,
			  *((u32*)((void*)nvstate + ns_offset_desired)));
	}
	else {
		TRACE("INVALID LINUXNVSTATE?????\n");
	}
}
#endif


static struct module* nvidia_mod = NULL;

static int init_nv_device_reg(void);
static int shutdown_nv_device_reg(void);
void shutdown_nvidia_info(void);

static int nvidia_going_module_notify(struct notifier_block *self,
				unsigned long val, void *data)
{
	struct module *mod = data;

	if (nvidia_mod && (mod == nvidia_mod)) {
		switch (val) {
		case MODULE_STATE_GOING:
			/* just set our mod reference to null to avoid crash */
			nvidia_mod = NULL;
			mb();
			break;
		default:
			break;
		}
	}

	return 0;
}

static struct notifier_block nvidia_going =
{
	.notifier_call = nvidia_going_module_notify,
	.priority = 1,
};


struct init_nvinfo_wq_data
{
	struct work_struct work;
};

static void __init_nvidia_info(struct work_struct *w)
{
	struct init_nvinfo_wq_data *work =
			container_of(w, struct init_nvinfo_wq_data, work);
	struct module* mod;

	mutex_lock(&module_mutex);
	mod = find_module("nvidia");
	mutex_unlock(&module_mutex);

	if(mod != NULL) {
		TRACE("%s : Found NVIDIA module. Core Code: %p to %p\n", __FUNCTION__,
			  (void*)(mod->module_core),
			  (void*)(mod->module_core) + mod->core_size);

		init_nv_device_reg();
		nvidia_mod = mod; /* make module visible to others */
		register_module_notifier(&nvidia_going);
	}
	else {
		TRACE("%s : Could not find NVIDIA module!  Loaded?\n", __FUNCTION__);
		init_nv_device_reg();
	}

	kfree(work);
}

int init_nvidia_info(void)
{
	struct init_nvinfo_wq_data *wq_job =
		kmalloc(sizeof(struct init_nvinfo_wq_data), GFP_ATOMIC);
	INIT_WORK(&wq_job->work, __init_nvidia_info);
	schedule_work(&wq_job->work);
	return 0;
}

void shutdown_nvidia_info(void)
{
	if (nvidia_mod) {
		nvidia_mod = NULL;
		mb();

		unregister_module_notifier(&nvidia_going);
		shutdown_nv_device_reg();
	}
}

/* works with pointers to static data inside the module too. */
int is_nvidia_func(void* func_addr)
{
	int ret = 0;
	struct module* mod = nvidia_mod;

	if(mod)
		ret = within_module_core((long unsigned int)func_addr, mod);

	return(ret);
}
EXPORT_SYMBOL(is_nvidia_func);

int nv_schedule_work(struct work_struct *work)
{
#if defined(CONFIG_LITMUS_NVIDIA_WORKQ_ON) || \
	defined(CONFIG_LITMUS_NVIDIA_WORKQ_ON_DEDICATED)
	if(nv_schedule_work_klmirqd(work))
		return 1;
#endif
	/* default to linux handler */
	sched_trace_work_release(NULL, get_work_nv_device_num(work));
	return queue_work(system_wq, work);
}
EXPORT_SYMBOL(nv_schedule_work);

void nv_tasklet_schedule(struct tasklet_struct *t)
{
	/* assume t has already been identified as an nvidia tasklet */
#if defined(CONFIG_LITMUS_NVIDIA_NONSPLIT_INTERRUPTS)
	if (nv_tasklet_schedule_now(t))
		return;
#elif defined(CONFIG_LITMUS_SOFTIRQD)
	if (nv_tasklet_schedule_klmirqd(t, _litmus_tasklet_schedule))
		return;
#else
	sched_trace_tasklet_release(NULL, get_tasklet_nv_device_num(t));
#endif
	/* default to linux handler */
	___tasklet_schedule(t);
}
EXPORT_SYMBOL(nv_tasklet_schedule);

void nv_tasklet_hi_schedule(struct tasklet_struct *t)
{
	/* assume t has already been identified as an nvidia tasklet */
#if defined(CONFIG_LITMUS_NVIDIA_NONSPLIT_INTERRUPTS)
	if (nv_tasklet_schedule_now(t))
		return;
#elif defined(CONFIG_LITMUS_SOFTIRQD)
	if (nv_tasklet_schedule_klmirqd(t, _litmus_tasklet_hi_schedule))
		return;
#else
	sched_trace_tasklet_release(NULL, get_tasklet_nv_device_num(t));
#endif
	/* default to linux handler */
	___tasklet_hi_schedule(t);
}
EXPORT_SYMBOL(nv_tasklet_hi_schedule);

void nv_tasklet_hi_schedule_first(struct tasklet_struct *t)
{
	BUG_ON(!irqs_disabled());

	/* assume t has already been identified as an nvidia tasklet */
#if defined(CONFIG_LITMUS_NVIDIA_NONSPLIT_INTERRUPTS)
	if (nv_tasklet_schedule_now(t))
		return;
#elif defined(CONFIG_LITMUS_SOFTIRQD)
	if (nv_tasklet_schedule_klmirqd(t, _litmus_tasklet_hi_schedule_first))
		return;
#else
	sched_trace_tasklet_release(NULL, get_tasklet_nv_device_num(t));
#endif
	/* default to linux handler */
	___tasklet_hi_schedule_first(t);
}
EXPORT_SYMBOL(nv_tasklet_hi_schedule_first);

u32 get_tasklet_nv_device_num(const struct tasklet_struct *t)
{
	/* TODO: use hard-coded offsets instead of including structures
	   derived from NVIDIA's GPL layer data structures */
	litmus_nv_state_t* nvstate = (litmus_nv_state_t*)(t->data);
	litmus_nv_linux_state_t* linuxstate =
			container_of(nvstate, litmus_nv_linux_state_t, nv_state);

	BUG_ON(linuxstate->device_num >= NV_DEVICE_NUM);

	return(linuxstate->device_num);
}

u32 get_work_nv_device_num(const struct work_struct *t)
{
	const int DEVICE_NUM_OFFSET = sizeof(struct work_struct);
	void* state = (void*)(t);
	void** device_num_ptr = state + DEVICE_NUM_OFFSET;

	/* pray NVIDIA will aways set "data" to device ID pointer */
	u32 device_num = *((u32*)(*device_num_ptr));
	return(device_num);
}


typedef struct {
	raw_spinlock_t	lock;

	/* GPU owners are organized in a priority-ordered heap */
	struct binheap	owners;

#ifdef CONFIG_LITMUS_SOFTIRQD
	klmirqd_callback_t interrupt_callback;
	struct task_struct* interrupt_thread;

	/* TODO: make threads check for the ready flag */
	unsigned int interrupt_ready:1;

#ifdef CONFIG_LITMUS_NVIDIA_WORKQ_ON_DEDICATED
	klmirqd_callback_t workq_callback;
	struct task_struct* workq_thread;
	unsigned int workq_ready:1;
#endif
#endif

#ifdef CONFIG_LITMUS_NV_KLMIRQD_DEBUG
	struct tasklet_struct nv_klmirqd_dbg_tasklet;
#endif
}nv_device_registry_t;


/* global registry table for GPU device ownership */
static nv_device_registry_t NV_DEVICE_REG[NV_DEVICE_NUM];



#ifdef CONFIG_LITMUS_SOFTIRQD
/* launches a klmirqd thread for a given device */
static int nvidia_launch_interrupt_cb(void *arg)
{
	unsigned long flags;
	int reg_device_id = (int)(long long)(arg);
	nv_device_registry_t *reg = &NV_DEVICE_REG[reg_device_id];

	TRACE("nvklmirqd callback for GPU %d\n", reg_device_id);

	raw_spin_lock_irqsave(&reg->lock, flags);
	reg->interrupt_thread = current;
	reg->interrupt_ready = 1;
	raw_spin_unlock_irqrestore(&reg->lock, flags);

	return 0;
}

#ifdef CONFIG_LITMUS_NVIDIA_WORKQ_ON_DEDICATED
/* launches a klmirqd thread for a given device
   (exclusively handles deferred workqueue work) */
static int nvidia_launch_workq_cb(void *arg)
{
	unsigned long flags;
	int reg_device_id = (int)(long long)(arg);
	nv_device_registry_t *reg = &NV_DEVICE_REG[reg_device_id];

	TRACE("nvklmworkerd callback for GPU %d\n", reg_device_id);

	raw_spin_lock_irqsave(&reg->lock, flags);
	reg->workq_thread = current;
	reg->workq_ready = 1;
	raw_spin_unlock_irqrestore(&reg->lock, flags);

	return 0;
}
#endif /* end LITMUS_NVIDIA_WORKQ_ON_DEDICATED */
#endif /* end LITMUS_SOFTIRQD */

#ifdef CONFIG_LITMUS_NV_KLMIRQD_DEBUG
struct nv_klmirqd_dbg_timer_struct
{
	struct hrtimer timer;
};

static struct nv_klmirqd_dbg_timer_struct nv_klmirqd_dbg_timer;

static void nv_klmirqd_arm_dbg_timer(lt_t relative_time)
{
	lt_t when_to_fire = litmus_clock() + relative_time;

	TRACE("next nv tasklet in %d ns\n", relative_time);

	__hrtimer_start_range_ns(&nv_klmirqd_dbg_timer.timer,
					ns_to_ktime(when_to_fire),
					0,
					HRTIMER_MODE_ABS_PINNED,
					0);
}

static void nv_klmirqd_dbg_tasklet_func(unsigned long arg)
{
	lt_t now = litmus_clock();
	nv_device_registry_t *reg = (nv_device_registry_t*)arg;
	int gpunum = reg - &NV_DEVICE_REG[0];

	TRACE("nv klmirqd routine invoked for GPU %d!\n", gpunum);

	/* set up the next timer -- within next 10ms */
	nv_klmirqd_arm_dbg_timer(now % (NSEC_PER_MSEC * 10));
}


static enum hrtimer_restart nvklmirqd_timer_func(struct hrtimer *timer)
{
	lt_t now = litmus_clock();
	int gpu = (int)(now % num_online_gpus());
	nv_device_registry_t *reg;

	TRACE("nvklmirqd_timer invoked!\n");

	reg = &NV_DEVICE_REG[gpu];

	if (reg->interrupt_thread && reg->interrupt_ready) {
		TRACE("Adding a tasklet for GPU %d\n", gpu);
		litmus_tasklet_schedule(&reg->nv_klmirqd_dbg_tasklet, reg->interrupt_thread);
	}
	else {
		TRACE("nv klmirqd is not ready!\n");
		/* set up the next timer -- within next 10ms */
		nv_klmirqd_arm_dbg_timer(now % (NSEC_PER_MSEC * 10));
	}

	return HRTIMER_NORESTART;
}
#endif


static int gpu_owner_max_priority_order(const struct binheap_node *a,
				const struct binheap_node *b)
{
	struct task_struct *d_a =
			container_of(
				binheap_entry(a, struct rt_param, gpu_owner_node),
				struct task_struct, rt_param);
	struct task_struct *d_b =
			container_of(
				binheap_entry(b, struct rt_param, gpu_owner_node),
				struct task_struct, rt_param);

	BUG_ON(!d_a);
	BUG_ON(!d_b);

	return litmus->compare(d_a, d_b);
}

#ifdef CONFIG_LITMUS_SOFTIRQD
static int create_threads(nv_device_registry_t* reg, int device)
{
	int ret = 0;
	char name[MAX_KLMIRQD_NAME_LEN+1];
	int default_cpu = litmus->map_gpu_to_cpu(device);
	int status;

	/* spawn the interrupt thread */
	snprintf(name, MAX_KLMIRQD_NAME_LEN, "nvklmirqd%d", device);
	reg->interrupt_callback.func = nvidia_launch_interrupt_cb;
	reg->interrupt_callback.arg = (void*)(long long)(device);
	mb();
	status = launch_klmirqd_thread(name, default_cpu,
								   &reg->interrupt_callback);
	if(status != 0) {
		TRACE("Failed to create nvklmirqd thread for GPU %d\n", device);
		--ret;
	}

#ifdef CONFIG_LITMUS_NVIDIA_WORKQ_ON_DEDICATED
	/* spawn the workqueue thread */
	snprintf(name, MAX_KLMIRQD_NAME_LEN, "nvklmworker%d", device);
	reg->workq_callback.func = nvidia_launch_workq_cb;
	reg->workq_callback.arg = (void*)(long long)(device);
	mb();
	status = launch_klmirqd_thread(name, default_cpu,
								   &reg->workq_callback);
	if(status != 0) {
		TRACE("Failed to create nvklmworkqd thread for GPU %d\n", device);
		--ret;
	}
#endif /* end LITMUS_NVIDIA_WORKQ_ON_DEDICATED */

	return ret;
}

static int destroy_threads(nv_device_registry_t* reg, int device)
{
	int ret = 0;
	unsigned long flags;

	if ((reg->interrupt_thread && reg->interrupt_ready)
#ifdef CONFIG_LITMUS_NVIDIA_WORKQ_ON_DEDICATED
		|| (reg->workq_thread && reg->workq_ready)
#endif
		) {
		raw_spin_lock_irqsave(&reg->lock, flags);
		if (reg->interrupt_thread && reg->interrupt_ready) {
			struct task_struct* th = reg->interrupt_thread;
			reg->interrupt_thread = NULL;
			mb();
			reg->interrupt_ready = 0;
			mb();
			raw_spin_unlock_irqrestore(&reg->lock, flags);
			kill_klmirqd_thread(th);
		}
		else
			raw_spin_unlock_irqrestore(&reg->lock, flags);

#ifdef CONFIG_LITMUS_NVIDIA_WORKQ_ON_DEDICATED
		raw_spin_lock_irqsave(&reg->lock, flags);
		if (reg->workq_thread && reg->workq_ready) {
			struct task_struct* th = reg->workq_thread;
			reg->workq_thread = NULL;
			mb();
			reg->workq_ready = 0;
			mb();

			raw_spin_unlock_irqrestore(&reg->lock, flags);
			kill_klmirqd_thread(th);
		}
		else
			raw_spin_unlock_irqrestore(&reg->lock, flags);
#endif /* end LITMUS_NVIDIA_WORKQ_ON_DEDICATED */
	}

	return ret;
}

#if defined(CONFIG_LITMUS_NVIDIA_WORKQ_ON) || \
	defined(CONFIG_LITMUS_NVIDIA_WORKQ_ON_DEDICATED)
/* called by Linux's schedule_work() to hand GPU work_structs to klmirqd */
int nv_schedule_work_klmirqd(struct work_struct *work)
{
	unsigned long flags;
	u32 nvidia_device = get_work_nv_device_num(work);
	struct task_struct* klmirqd_th;
	int ret = 0;

	if (nvidia_device >= NV_DEVICE_NUM) {
		TRACE("Could not determine GPU source of work item: %u\n", nvidia_device);
		goto out;
	}

	klmirqd_th = get_and_lock_nvklmworkqd_thread(nvidia_device, &flags);
	if (unlikely(!klmirqd_th)) {
		TRACE("Could not find klmirqd thread for GPU %u\n", nvidia_device);
		goto out;
	}

	TRACE("Handling NVIDIA workq for device %u (klmirqd: %s/%d) at %llu\n",
		nvidia_device,
		klmirqd_th->comm,
		klmirqd_th->pid,
		litmus_clock());

	sched_trace_work_release(effective_priority(klmirqd_th),
					nvidia_device);

	if (likely(litmus_schedule_work(work, klmirqd_th))) {
		unlock_nvklmworkqd_thread(nvidia_device, &flags);
		ret = 1; /* success */
	}

out:
	return ret;
}
#endif /* end LITMUS_NVIDIA_WORKQ_ON || WORKQ_ON_DEDICATED */



#endif /* end LITMUS_SOFTIRQD */

static int init_nv_device_reg(void)
{
	int i;

#ifdef CONFIG_LITMUS_SOFTIRQD
	if (!klmirqd_is_ready()) {
		TRACE("klmirqd is not ready!\n");
		return 0;
	}
#endif

	memset(NV_DEVICE_REG, 0, sizeof(NV_DEVICE_REG));
	mb();

	for(i = 0; i < num_online_gpus(); ++i) {
		raw_spin_lock_init(&NV_DEVICE_REG[i].lock);
		INIT_BINHEAP_HANDLE(&NV_DEVICE_REG[i].owners,
						gpu_owner_max_priority_order);

#ifdef CONFIG_LITMUS_SOFTIRQD
		(void)create_threads(&NV_DEVICE_REG[i], i);
#endif
	}

#ifdef CONFIG_LITMUS_NV_KLMIRQD_DEBUG
	for(i = 0; i < num_online_gpus(); ++i) {
		tasklet_init(&NV_DEVICE_REG[i].nv_klmirqd_dbg_tasklet,
					 nv_klmirqd_dbg_tasklet_func,
					 (unsigned long)&NV_DEVICE_REG[i]);
	}
	hrtimer_init(&nv_klmirqd_dbg_timer.timer, CLOCK_MONOTONIC,
					HRTIMER_MODE_ABS);
	nv_klmirqd_dbg_timer.timer.function = nvklmirqd_timer_func;
	nv_klmirqd_arm_dbg_timer(NSEC_PER_MSEC * 1000);
#endif

	return(1);
}


/* The following code is full of nasty race conditions... */
/* spawning of klimirqd threads can race with init_nv_device_reg()!!!! */
static int shutdown_nv_device_reg(void)
{
	int i;

	TRACE("Shutting down nv device registration.\n");

	for (i = 0; i < num_online_gpus(); ++i) {

		TRACE("Shutting down GPU %d.\n", i);

#ifdef CONFIG_LITMUS_SOFTIRQD
		(void)destroy_threads(&NV_DEVICE_REG[i], i);
#endif
		while (!binheap_empty(&NV_DEVICE_REG[i].owners)) {
			binheap_delete_root(&NV_DEVICE_REG[i].owners,
							struct rt_param, gpu_owner_node);
		}
	}

	return(1);
}


/* use to get the owner of nv_device_id. */
struct task_struct* get_nv_max_device_owner(u32 target_device_id)
{
	struct task_struct *owner = NULL;
	nv_device_registry_t *reg;

	BUG_ON(target_device_id >= NV_DEVICE_NUM);

	reg = &NV_DEVICE_REG[target_device_id];

	if (!binheap_empty(&reg->owners)) {
		struct task_struct *hp = container_of(
			binheap_top_entry(&reg->owners, struct rt_param, gpu_owner_node),
			struct task_struct, rt_param);

		TRACE_CUR("hp: %s/%d\n", hp->comm, hp->pid);

		owner = hp;
	}

	return(owner);
}

#ifdef CONFIG_LITMUS_SOFTIRQD
typedef enum
{
	INTERRUPT_TH,
	WORKQ_TH
} nvklmtype_t;

static struct task_struct* __get_klm_thread(nv_device_registry_t* reg,
				nvklmtype_t type)
{
	struct task_struct *klmirqd = NULL;

	switch(type) {
	case INTERRUPT_TH:
#ifdef CONFIG_LITMUS_NVIDIA_WORKQ_ON
	case WORKQ_TH:
#endif
		if(likely(reg->interrupt_ready))
			klmirqd = reg->interrupt_thread;
		break;
#ifdef CONFIG_LITMUS_NVIDIA_WORKQ_ON_DEDICATED
	case WORKQ_TH:
		if(likely(reg->workq_ready))
			klmirqd = reg->workq_thread;
		break;
#endif
	default:
		break;
	}

	return klmirqd;
}

static struct task_struct* __get_and_lock_klm_thread(nv_device_registry_t* reg,
				unsigned long* flags, nvklmtype_t type)
{
	struct task_struct *klmirqd;

	raw_spin_lock_irqsave(&reg->lock, *flags);
	klmirqd = __get_klm_thread(reg, type);

	if (!klmirqd) {
		/* unlock if thread does not exist or is not ready */
		raw_spin_unlock_irqrestore(&reg->lock, *flags);
	}

	return klmirqd;
}

static void __unlock_klm_thread(nv_device_registry_t* reg,
				unsigned long* flags, nvklmtype_t type)
{
	/* workq and interrupts share a lock per GPU */
	raw_spin_unlock_irqrestore(&reg->lock, *flags);
}

struct task_struct* get_and_lock_nvklmirqd_thread(u32 target_device_id,
				unsigned long* flags)
{
	nv_device_registry_t *reg;
	struct task_struct *th;
	BUG_ON(target_device_id >= NV_DEVICE_NUM);

	if (unlikely(nvidia_mod == NULL))
		return NULL;

	reg = &NV_DEVICE_REG[target_device_id];
	th = __get_and_lock_klm_thread(reg, flags, INTERRUPT_TH);

	barrier();
	if (unlikely(nvidia_mod == NULL)) {
		th = NULL;
		__unlock_klm_thread(reg, flags, INTERRUPT_TH);
	}

	return th;
}

void unlock_nvklmirqd_thread(u32 target_device_id, unsigned long* flags)
{
	nv_device_registry_t *reg;
	BUG_ON(target_device_id >= NV_DEVICE_NUM);
	reg = &NV_DEVICE_REG[target_device_id];
	__unlock_klm_thread(reg, flags, INTERRUPT_TH);
}

struct task_struct* get_nvklmirqd_thread(u32 target_device_id)
{
	/* should this function be allowed?
	   who will use klmirqd thread without thread safety? */

	unsigned long flags;
	struct task_struct *klmirqd;
	klmirqd = get_and_lock_nvklmirqd_thread(target_device_id, &flags);
	if(klmirqd)
		unlock_nvklmirqd_thread(target_device_id, &flags);
	return klmirqd;
}

#if defined(CONFIG_LITMUS_NVIDIA_WORKQ_ON) || \
	defined(CONFIG_LITMUS_NVIDIA_WORKQ_ON_DEDICATED)
struct task_struct* get_and_lock_nvklmworkqd_thread(u32 target_device_id,
				unsigned long* flags)
{
	nv_device_registry_t *reg;
	struct task_struct *th;
	BUG_ON(target_device_id >= NV_DEVICE_NUM);

	if (unlikely(nvidia_mod == NULL))
		return NULL;

	reg = &NV_DEVICE_REG[target_device_id];
	th = __get_and_lock_klm_thread(reg, flags, WORKQ_TH);

	barrier();
	if (unlikely(nvidia_mod == NULL)) {
		th = NULL;
		__unlock_klm_thread(reg, flags, WORKQ_TH);
	}

	return th;
}

void unlock_nvklmworkqd_thread(u32 target_device_id, unsigned long* flags)
{
	nv_device_registry_t *reg;
	BUG_ON(target_device_id >= NV_DEVICE_NUM);
	reg = &NV_DEVICE_REG[target_device_id];
	__unlock_klm_thread(reg, flags, WORKQ_TH);
}


struct task_struct* get_nvklmworkqd_thread(u32 target_device_id)
{
	unsigned long flags;
	struct task_struct *klmirqd;
	klmirqd = get_and_lock_nvklmworkqd_thread(target_device_id, &flags);
	if(klmirqd)
		unlock_nvklmworkqd_thread(target_device_id, &flags);
	return klmirqd;
}
#endif /* end LITMUS_NVIDIA_WORKQ_ON && LITMUS_NVIDIA_WORKQ_ON_DEDICATED */


static int gpu_klmirqd_increase_priority(struct task_struct *klmirqd,
				struct task_struct *hp)
{
	int retval = 0;
	/* the klmirqd thread should never attempt to hold a litmus-level real-time
	   so nested support is not required */
	retval = litmus->__increase_prio(klmirqd, hp);
	return retval;
}

static int gpu_klmirqd_decrease_priority(struct task_struct *klmirqd,
				struct task_struct *hp, int budget_triggered)
{
	int retval = 0;
	/* the klmirqd thread should never attempt to hold a litmus-level real-time
	   so nested support is not required */
	retval = litmus->__decrease_prio(klmirqd, hp, budget_triggered);
	return retval;
}

int nv_tasklet_schedule_klmirqd(struct tasklet_struct *t,
				klmirqd_tasklet_sched_t klmirqd_func)
{
	unsigned long flags;
	u32 nvidia_device;
	struct task_struct* klmirqd_th;

	nvidia_device = get_tasklet_nv_device_num(t);
	klmirqd_th = get_and_lock_nvklmirqd_thread(nvidia_device, &flags);

	if (likely(klmirqd_th)) {
		TRACE("Handling NVIDIA tasklet for device %u "
			"(klmirqd: %s/%d) at %llu\n",
			nvidia_device,
			klmirqd_th->comm,
			klmirqd_th->pid,
			litmus_clock());

		sched_trace_tasklet_release(effective_priority(klmirqd_th),
						nvidia_device);

		if(likely(klmirqd_func(t, klmirqd_th))) {
			unlock_nvklmirqd_thread(nvidia_device, &flags);
			return 1; /* success */
		}
	}
	return 0;
}
#endif  /* end LITMUS_SOFTIRQD */

#ifdef CONFIG_LITMUS_NVIDIA_NONSPLIT_INTERRUPTS
int nv_tasklet_schedule_now(struct tasklet_struct *t)
{
	int success = 1;

	sched_trace_tasklet_release(NULL, get_tasklet_nv_device_num(t));

	TRACE("Handling NVIDIA tasklet.\n");

	if(likely(tasklet_trylock(t))) {
		if(likely(!atomic_read(&t->count))) {
			if(!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
				BUG();
			sched_trace_tasklet_begin(NULL);
			t->func(t->data);
			tasklet_unlock(t);
			sched_trace_tasklet_end(NULL, 0ul);
		}
		else {
			success = 0;
		}

		tasklet_unlock(t);
	}
	else {
		success = 0;
	}

	return success;
}
#endif


/* call when an gpu owner becomes real-time */
long enable_gpu_owner(struct task_struct *t)
{
	long retval = 0;
	int gpu;
	nv_device_registry_t *reg;

#ifdef CONFIG_LITMUS_SOFTIRQD
	struct task_struct *hp;
#endif

	if (!tsk_rt(t)->held_gpus)
		return -1;

	BUG_ON(!is_realtime(t));

	gpu = find_first_bit(&tsk_rt(t)->held_gpus,
					BITS_PER_BYTE*sizeof(tsk_rt(t)->held_gpus));

	if (binheap_is_in_heap(&tsk_rt(t)->gpu_owner_node)) {
		TRACE_CUR("task %s/%d is already active on GPU %d\n",
						t->comm, t->pid, gpu);
		goto out;
	}

	/* update the registration (and maybe klmirqd) */
	reg = &NV_DEVICE_REG[gpu];

	binheap_add(&tsk_rt(t)->gpu_owner_node, &reg->owners,
				struct rt_param, gpu_owner_node);


#ifdef CONFIG_LITMUS_SOFTIRQD
	hp = container_of(
			binheap_top_entry(&reg->owners, struct rt_param, gpu_owner_node),
			struct task_struct, rt_param);

	if (hp == t) {
		int success;

		/* we're the new hp */
		success = gpu_klmirqd_increase_priority(
						reg->interrupt_thread, effective_priority(t));
		retval = success;

#ifdef CONFIG_LITMUS_NVIDIA_WORKQ_ON_DEDICATED
		success = gpu_klmirqd_increase_priority(
						reg->workq_thread, effective_priority(t));
		if (success != 1)
			retval = success;
#endif /* end LITMUS_NVIDIA_WORKQ_ON_DEDICATED */
	}
#endif /* end LITMUS_SOFTIRQD */

out:
	return retval;
}

/* call when an gpu owner exits real-time */
long disable_gpu_owner(struct task_struct *t)
{
	long retval = 0;
	int gpu;
	nv_device_registry_t *reg;

#ifdef CONFIG_LITMUS_SOFTIRQD
	struct task_struct *hp;
	struct task_struct *new_hp = NULL;
#endif

	if (!tsk_rt(t)->held_gpus) {
		TRACE_CUR("task %s/%d does not hold any GPUs\n", t->comm, t->pid);
		return -1;
	}

	BUG_ON(!is_realtime(t));

	gpu = find_first_bit(&tsk_rt(t)->held_gpus,
					BITS_PER_BYTE*sizeof(tsk_rt(t)->held_gpus));

	if (!binheap_is_in_heap(&tsk_rt(t)->gpu_owner_node))
		goto out;

	reg = &NV_DEVICE_REG[gpu];

#ifdef CONFIG_LITMUS_SOFTIRQD
	hp = container_of(
			binheap_top_entry(&reg->owners, struct rt_param, gpu_owner_node),
			struct task_struct, rt_param);

	binheap_delete(&tsk_rt(t)->gpu_owner_node, &reg->owners);

	if (!binheap_empty(&reg->owners)) {
		new_hp = container_of(
				binheap_top_entry(&reg->owners, struct rt_param, gpu_owner_node),
				struct task_struct, rt_param);
	}

	if (hp == t && new_hp != t) {
		int success;
		struct task_struct *to_inh = (new_hp) ?
			effective_priority(new_hp) : NULL;
		success = gpu_klmirqd_decrease_priority(reg->interrupt_thread,
												to_inh, 0);
		retval = success;

#ifdef CONFIG_LITMUS_NVIDIA_WORKQ_ON_DEDICATED
		success = gpu_klmirqd_decrease_priority(reg->workq_thread,
						to_inh, 0);
		if(success != 1)
			retval = success;
#endif /* end LITMUS_NVIDIA_WORKQ_ON_DEDICATED */
	}
#else
	binheap_delete(&tsk_rt(t)->gpu_owner_node, &reg->owners);
#endif /* end !LITMUS_SOFTIRQD */

out:
	return retval;
}


long recheck_gpu_owner(struct task_struct* t)
{
	/* TODO: blend implementation of disable/enable */
	int retval = disable_gpu_owner(t);
	if (!retval)
		retval = enable_gpu_owner(t);
	return retval;
}

/* call to notify the GPU mgmt framework that the priority
   has increased for the given owner (causes re-evaluation
   of GPU tasklet/workqueue scheduling priority) */
int gpu_owner_increase_priority(struct task_struct *t)
{
	int retval = 0;
	int gpu;
	nv_device_registry_t *reg;

	struct task_struct *hp = NULL;
	struct task_struct *hp_eff = NULL;

#ifdef CONFIG_LITMUS_SOFTIRQD
	int increase_klmirqd = 0;
#endif

	BUG_ON(!is_realtime(t));
	BUG_ON(!tsk_rt(t)->held_gpus);

	gpu = find_first_bit(&tsk_rt(t)->held_gpus,
					BITS_PER_BYTE*sizeof(tsk_rt(t)->held_gpus));

	if (!binheap_is_in_heap(&tsk_rt(t)->gpu_owner_node)) {
		TRACE_CUR("nv klmirqd may not inherit from %s/%d on GPU %d\n",
				  t->comm, t->pid, gpu);
		goto out;
	}

	TRACE_CUR("task %s/%d on GPU %d increasing priority.\n",
					t->comm, t->pid, gpu);

	reg = &NV_DEVICE_REG[gpu];

	hp = container_of(
			binheap_top_entry(&reg->owners, struct rt_param, gpu_owner_node),
			struct task_struct, rt_param);
	hp_eff = effective_priority(hp);

	if (hp != t) {
		/* our position in the heap may have changed.
		   hp is already at the root. */
		binheap_decrease(&tsk_rt(t)->gpu_owner_node, &reg->owners);
	}
#ifdef CONFIG_LITMUS_SOFTIRQD
	else {
		/* unconditionally propagate - t already has the updated eff and is
		   at the root, so we can't detect a change in inheritance, but we
		   know that priority has indeed increased/changed. */
		increase_klmirqd = 1;
	}

	hp = container_of(
			binheap_top_entry(&reg->owners, struct rt_param, gpu_owner_node),
			struct task_struct, rt_param);

	/* check if the eff. prio. of hp has changed */
	if (increase_klmirqd || (effective_priority(hp) != hp_eff)) {
		int success;

		hp_eff = effective_priority(hp);
		TRACE_CUR("%s/%d (eff_prio = %s/%d) is new hp on GPU %d.\n",
						t->comm, t->pid,
						hp_eff->comm, hp_eff->pid,
						gpu);

		success = gpu_klmirqd_increase_priority(reg->interrupt_thread, hp_eff);
		retval = success;

#ifdef CONFIG_LITMUS_NVIDIA_WORKQ_ON_DEDICATED
		success = gpu_klmirqd_increase_priority(reg->workq_thread, hp_eff);
		if (success != 1)
			retval = success;
#endif /* end LITMUS_NVIDIA_WORKQ_ON_DEDICATED */
	}
#endif /* end LITMUS_SOFTIRQD */

out:
	return retval;
}

/* call to notify the GPU mgmt framework that the priority
   has decreased for the given owner (causes re-evaluation
   of GPU tasklet/workqueue scheduling priority) */
int gpu_owner_decrease_priority(struct task_struct *t)
{
	int retval = 0;
	int gpu;
	nv_device_registry_t *reg;

	struct task_struct *hp = NULL;
	struct task_struct *hp_eff = NULL;

	BUG_ON(!is_realtime(t));
	BUG_ON(!tsk_rt(t)->held_gpus);

	gpu = find_first_bit(&tsk_rt(t)->held_gpus,
					BITS_PER_BYTE*sizeof(tsk_rt(t)->held_gpus));

	if (!binheap_is_in_heap(&tsk_rt(t)->gpu_owner_node)) {
		TRACE_CUR("nv klmirqd may not inherit from %s/%d on GPU %d\n",
				  t->comm, t->pid, gpu);
		goto out;
	}

	TRACE_CUR("task %s/%d on GPU %d decresing priority.\n",
					t->comm, t->pid, gpu);
	reg = &NV_DEVICE_REG[gpu];

	hp = container_of(
			binheap_top_entry(&reg->owners, struct rt_param, gpu_owner_node),
			struct task_struct, rt_param);
	hp_eff = effective_priority(hp);
	binheap_delete(&tsk_rt(t)->gpu_owner_node, &reg->owners);
	binheap_add(&tsk_rt(t)->gpu_owner_node, &reg->owners,
				struct rt_param, gpu_owner_node);

#ifdef CONFIG_LITMUS_SOFTIRQD
	if (hp == t) { /* t was originally the hp */
		struct task_struct *new_hp =
			container_of(binheap_top_entry(&reg->owners,
									struct rt_param, gpu_owner_node),
					 struct task_struct, rt_param);
		/* if the new_hp is still t, or if the effective priority has
		   changed */
		if ((new_hp == t) || (effective_priority(new_hp) != hp_eff)) {
			int success;
			hp_eff = effective_priority(new_hp);
			TRACE_CUR("%s/%d is no longer hp on GPU %d.\n",
							t->comm, t->pid, gpu);
			success = gpu_klmirqd_decrease_priority(reg->interrupt_thread,
							hp_eff, 1);
			retval = success;

#ifdef CONFIG_LITMUS_NVIDIA_WORKQ_ON_DEDICATED

			success = gpu_klmirqd_decrease_priority(reg->workq_thread,
							hp_eff, 1);
			if(success != 1)
				retval = success;
#endif /* end LITMUS_NVIDIA_WORKQ_ON_DEDICATED */
		}
	}
#endif /* end LITMUS_SOFTIRQD */

out:
	return retval;
}

static int __reg_nv_device(int reg_device_id, struct task_struct *t)
{
	__set_bit(reg_device_id, &tsk_rt(t)->held_gpus);
	return 0;
}

static int __clear_reg_nv_device(int de_reg_device_id, struct task_struct *t)
{
	__clear_bit(de_reg_device_id, &tsk_rt(t)->held_gpus);
	return 0;
}

int reg_nv_device(int reg_device_id, int reg_action, struct task_struct *t)
{
	int ret;

	if((reg_device_id < num_online_gpus()) && (reg_device_id >= 0))
	{
		if(reg_action)
			ret = __reg_nv_device(reg_device_id, t);
		else
			ret = __clear_reg_nv_device(reg_device_id, t);
	}
	else
	{
		ret = -ENODEV;
	}

	return ret;
}