aboutsummaryrefslogtreecommitdiffstats
path: root/nvdebug.h
blob: 3860c2ec8bc5da64d286f20ff45cf713d1c1ee57 (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
/* Copyright 2021 Joshua Bakita
 * SPDX-License-Identifier: MIT
 *
 * File outline:
 * - Runlist, preemption, and channel control (FIFO)
 * - Basic GPU information (MC)
 * - Detailed GPU information (PTOP, FUSE, and CE)
 * - PRAMIN, BAR1/2, and page table status
 * - Helper functions for nvdebug
 */

#include <linux/device.h>   // For dev_get_drvdata()
#include <linux/seq_file.h> // For struct seq_file
#include <linux/proc_fs.h>  // For PDE_DATA() macro
#include <linux/version.h>  // For KERNEL_VERSION and LINUX_VERSION_CODE
#include <asm/io.h>

// Fully defined in include/nvgpu/gk20a.h. We only pass around pointers to
// this, so declare as incomplete type to avoid pulling in the nvgpu headers.
struct gk20a;

/* Runlist Channel
  A timeslice group (TSG) is composed of channels. Each channel is a FIFO queue
  of GPU commands. These commands are typically queued from userspace.

  Prior to Volta, channels could also exist independent of a TSG. These are
  called "bare channels" in the Jetson nvgpu driver.

  `INST_PTR` points to a GPU Instance Block which contains FIFO states, virtual
  address space configuration for this context, and a pointer to the page
  tables. All channels in a TSG point to the same GPU Instance Block (?).

  "RUNQUEUE_SELECTOR determines to which runqueue the channel belongs, and
  thereby which PBDMA will run the channel.  Increasing values select
  increasingly numbered PBDMA IDs serving the runlist.  If the selector value
  exceeds the number of PBDMAs on the runlist, the hardware will silently
  reassign the channel to run on the first PBDMA as though RUNQUEUE_SELECTOR had
  been set to 0.  (In current hardware, this is used by SCG on the graphics
  runlist only to determine which FE pipe should service a given channel.  A
  value of 0 targets the first FE pipe, which can process all FE driven engines:
  Graphics, Compute, Inline2Memory, and TwoD.  A value of 1 targets the second
  FE pipe, which can only process Compute work.  Note that GRCE work is allowed
  on either runqueue." (NVIDIA) Note that it appears runqueue 1 is the default
  for CUDA work on the Jetson Xavier.

  ENTRY_TYPE (T)        : type of this entry: ENTRY_TYPE_CHAN
  CHID (ID)             : identifier of the channel to run (overlays ENTRY_ID)
  RUNQUEUE_SELECTOR (Q) : selects which PBDMA should run this channel if
                          more than one PBDMA is supported by the runlist,
                          additionally, "A value of 0 targets the first FE
                          pipe, which can process all FE driven engines:
                          Graphics, Compute, Inline2Memory, and TwoD.  A value
                          of 1 targets the second FE pipe, which can only
                          process Compute work.  Note that GRCE work is allowed
                          on either runqueue.)"

  INST_PTR_LO           : lower 20 bits of the 4k-aligned instance block pointer
  INST_PTR_HI           : upper 32 bit of instance block pointer
  INST_TARGET (TGI)     : aperture of the instance block

  USERD_PTR_LO          : upper 24 bits of the low 32 bits, of the 512-byte-aligned USERD pointer
  USERD_PTR_HI          : upper 32 bits of USERD pointer
  USERD_TARGET (TGU)    : aperture of the USERD data structure

  Channels were around since at least Fermi, but were rearranged with Volta to
  add a USERD pointer, a longer INST pointer, and a runqueue selector flag.
*/
enum ENTRY_TYPE {ENTRY_TYPE_CHAN = 0, ENTRY_TYPE_TSG = 1};
enum INST_TARGET {TARGET_VID_MEM = 0, TARGET_SYS_MEM_COHERENT = 2, TARGET_SYS_MEM_NONCOHERENT = 3};
static inline char* target_to_text(enum INST_TARGET t) {
	switch (t) {
		case TARGET_VID_MEM:
			return "VID_MEM";
		case TARGET_SYS_MEM_COHERENT:
			return "SYS_MEM_COHERENT";
		case TARGET_SYS_MEM_NONCOHERENT:
			return "SYS_MEM_NONCOHERENT";
		default:
			printk(KERN_WARNING "[nvdebug] Invalid aperture!\n");
			return "INVALID";
	}
}

// Support: Volta, Ampere, Turing
struct gv100_runlist_chan {
// 0:63
	enum ENTRY_TYPE entry_type:1;
	uint32_t runqueue_selector:1;
	 uint32_t padding:2;
	enum INST_TARGET inst_target:2;
	 uint32_t padding2:2;
	uint32_t userd_ptr_lo:24;
	uint32_t userd_ptr_hi:32;
// 64:128
	uint32_t chid:12;
	uint32_t inst_ptr_lo:20;
	uint32_t inst_ptr_hi:32;
} __attribute__((packed));

// Support: Fermi, Kepler*, Maxwell, Pascal
// *In Kepler, inst fields may be unpopulated?
struct gm107_runlist_chan {
	uint32_t chid:12;
	 uint32_t padding0:1;
	enum ENTRY_TYPE entry_type:1;
	 uint32_t padding1:18;
	uint32_t inst_ptr_lo:20;
	enum INST_TARGET inst_target:2;  // Totally guessing on this
	 uint32_t padding2:10;
} __attribute__((packed));

#define gk110_runlist_chan gm107_runlist_chan

/* Runlist TSG (TimeSlice Group)
  The runlist is composed of timeslice groups (TSG). Each TSG corresponds
  to a single virtual address space on the GPU and contains `TSG_LENGTH`
  channels. These channels and virtual address space are accessible to the GPU
  host unit for use until the timeslice expires or a TSG switch is forcibly
  initiated via a write to `NV_PFIFO_PREEMPT`.

  timeslice = (TSG_TIMESLICE_TIMEOUT << TSG_TIMESLICE_SCALE) * 1024 nanoseconds

  ENTRY_TYPE (T)      : type of this entry: ENTRY_TYPE_TSG
  TIMESLICE_SCALE     : scale factor for the TSG's timeslice
  TIMESLICE_TIMEOUT   : timeout amount for the TSG's timeslice
  TSG_LENGTH          : number of channels that are part of this timeslice group
  TSGID               : identifier of the Timeslice group (overlays ENTRY_ID)

  TSGs appear to have been introduced with Kepler and stayed the same until
  they were rearranged at the time of channel rearrangement to support longer
  GPU instance addresses with Volta.
*/

// Support: Volta, Ampere*, Turing*
// *These treat the top 8 bits of TSGID as GFID (unused)
struct gv100_runlist_tsg {
// 0:63
	enum ENTRY_TYPE entry_type:1;
	 uint64_t padding:15;
	uint32_t timeslice_scale:4;
	 uint64_t padding2:4;
	uint32_t timeslice_timeout:8;
	uint32_t tsg_length:8;
	 uint32_t padding3:24;
// 64:128
	uint32_t tsgid:12;
	 uint64_t padding4:52;
} __attribute__((packed));
#define MAX_TSGID (1 << 12)

// Support: Kepler (v2?), Maxwell, Pascal
// Same fields as Volta except tsg_length is 6 bits rather than 8
// Last 32 bits appear to contain an undocumented inst ptr
struct gk110_runlist_tsg {
	uint32_t tsgid:12;
	 uint32_t padding0:1;
	enum ENTRY_TYPE entry_type:1;
	uint32_t timeslice_scale:4;
	uint32_t timeslice_timeout:8;
	uint32_t tsg_length:6;
	 uint32_t padding1:32;
} __attribute__((packed));


enum PREEMPT_TYPE {PREEMPT_TYPE_CHANNEL = 0, PREEMPT_TYPE_TSG = 1};

/* Preempt a TSG or Channel by ID
  ID/CHID     : Id of TSG or channel to preempt
  IS_PENDING  : Is a context switch pending?
  TYPE        : PREEMPT_TYPE_CHANNEL or PREEMPT_TYPE_TSG

  Support: Kepler, Maxwell, Pascal, Volta, Turing
*/
#define NV_PFIFO_PREEMPT 0x00002634
typedef union {
	struct {
		uint32_t id:12;
		 uint32_t padding:8;
		bool is_pending:1;
		 uint32_t padding2:3;
		enum PREEMPT_TYPE type:2;
		 uint32_t padding3:6;
	} __attribute__((packed));
	uint32_t raw;
} pfifo_preempt_t;

/*
  "Initiate a preempt of the engine by writing the bit associated with its
  runlist to NV_PFIFO_RUNLIST_PREEMPT...  Do not poll NV_PFIFO_RUNLIST_PREEMPT
  for the preempt to complete."

  Useful for preempting multiple runlists at once.

  Appears to trigger an interrupt or some other side-effect on the Jetson
  Xavier, as the built-in nvgpu driver seems to be disturbed by writing to this.

  To select the runlist dynamically, use the BIT(nr) kernel macro.
  Example:
    runlist_preempt_t rl_preempt;
    rl_preempt.raw = nvdebug_readl(g, NV_PFIFO_RUNLIST_PREEMPT);
    rl_preempt.raw |= BIT(nr);
    nvdebug_writel(g, NV_PFIFO_RUNLIST_PREEMPT, rl_preempt.raw);

  Support: Volta
*/
#define NV_PFIFO_RUNLIST_PREEMPT 0x00002638
typedef union {
	struct {
		bool runlist_0:1;
		bool runlist_1:1;
		bool runlist_2:1;
		bool runlist_3:1;
		bool runlist_4:1;
		bool runlist_5:1;
		bool runlist_6:1;
		bool runlist_7:1;
		bool runlist_8:1;
		bool runlist_9:1;
		bool runlist_10:1;
		bool runlist_11:1;
		bool runlist_12:1;
		bool runlist_13:1;
		 uint32_t padding:18;
	} __attribute__((packed));
	uint32_t raw;
} runlist_preempt_t;

/* Additional information on preempting from NVIDIA's driver (commit b1d0d8ece)
 * "From h/w team
 * Engine save can be blocked by eng  stalling interrupts.
 * FIFO interrupts shouldn’t block an engine save from
 * finishing, but could block FIFO from reporting preempt done.
 * No immediate reason to reset the engine if FIFO interrupt is
 * pending.
 * The hub, priv_ring, and ltc interrupts could block context
 * switch (or memory), but doesn’t necessarily have to.
 * For Hub interrupts they just report access counters and page
 * faults. Neither of these necessarily block context switch
 * or preemption, but they could.
 * For example a page fault for graphics would prevent graphics
 * from saving out. An access counter interrupt is a
 * notification and has no effect.
 * SW should handle page faults though for preempt to complete.
 * PRI interrupt (due to a failed PRI transaction) will result
 * in ctxsw failure reported to HOST.
 * LTC interrupts are generally ECC related and if so,
 * certainly don’t block preemption/ctxsw but they could.
 * Bus interrupts shouldn’t have anything to do with preemption
 * state as they are part of the Host EXT pipe, though they may
 * exhibit a symptom that indicates that GPU is in a bad state.
 * To be completely fair, when an engine is preempting SW
 * really should just handle other interrupts as they come in.
 * It’s generally bad to just poll and wait on a preempt
 * to complete since there are many things in the GPU which may
 * cause a system to hang/stop responding."
 */

// Note: This is different with Turing
// Support: Fermi, Kepler, Maxwell, Pascal, Volta
#define NV_PFIFO_RUNLIST_BASE 0x00002270
#define NV_PFIFO_ENG_RUNLIST_BASE(i) (0x00002280+(i)*8)
typedef union {
	struct {
		uint32_t ptr:28;
		enum INST_TARGET target:2;
		 uint32_t padding:2;
	} __attribute__((packed));
	uint32_t raw;
} runlist_base_t;

// Support: Kepler, Maxwell, Pascal, Volta
// Works on Fermi, but id is one bit longer and is b11111
#define NV_PFIFO_RUNLIST 0x00002274
#define NV_PFIFO_ENG_RUNLIST(i) (0x00002284+(i)*8)
typedef union {
	// RUNLIST fields
	struct {
		uint32_t len:16;
		 uint32_t padding:4;
		uint32_t id:4; // Runlist ID (each engine may have a seperate runlist)
		 uint32_t padding2:8;
	} __attribute__((packed));
	// ENG_RUNLIST fields that differ
	struct {
		 uint32_t padding3:20;
		bool is_pending:1; // Is runlist not yet committed?
		 uint32_t padding4:11;
	} __attribute__((packed));
	uint32_t raw;
} runlist_info_t;

enum CHANNEL_STATUS {
	CHANNEL_STATUS_IDLE = 0,
	CHANNEL_STATUS_PENDING = 1,
	CHANNEL_STATUS_PENDING_CTX_RELOAD = 2,
	CHANNEL_STATUS_PENDING_ACQUIRE = 3,
	CHANNEL_STATUS_PENDING_ACQ_CTX_RELOAD = 4,
	CHANNEL_STATUS_ON_PBDMA = 5,
	CHANNEL_STATUS_ON_PBDMA_AND_ENG = 6,
	CHANNEL_STATUS_ON_ENG = 7,
	CHANNEL_STATUS_ON_ENG_PENDING_ACQUIRE = 8,
	CHANNEL_STATUS_ON_ENG_PENDING = 9,
	CHANNEL_STATUS_ON_PBDMA_CTX_RELOAD = 10,
	CHANNEL_STATUS_ON_PBDMA_AND_ENG_CTX_RELOAD = 11,
	CHANNEL_STATUS_ON_ENG_CTX_RELOAD = 12,
	CHANNEL_STATUS_ON_ENG_PENDING_CTX_RELOAD = 13,
	CHANNEL_STATUS_ON_ENG_PENDING_ACQ_CTX_RELOAD = 14,
};

#define NV_PCCSR_CHANNEL_INST(i) (0x00800000+(i)*8)
// There are a total of 512 possible channels
#define MAX_CHID 512
typedef union {
	struct {
// 0:31
		uint32_t inst_ptr:28;
		enum INST_TARGET inst_target:2;
		 uint32_t padding0:1;
		bool inst_bind:1;
// 32:64
		bool enable:1;
		bool next:1;
		 uint32_t padding:6;
		bool force_ctx_reload:1;
		 uint32_t padding2:1;
		bool enable_set:1;
		bool enable_clear:1;
		 uint32_t padding3:10;
		bool pbdma_faulted:1;
		bool eng_faulted:1;
		enum CHANNEL_STATUS status:4;
		bool busy:1;
		 uint32_t padding4:3;
	} __attribute__((packed));
	uint64_t raw;
} channel_ctrl_t;

/* Control word for runlist enable/disable.

  RUNLIST_N           : Is runlist n disabled? (1 == disabled, 0 == enabled)

  To select the runlist dynamically, use the BIT(nr) kernel macro.
  Disabling example:
    runlist_disable_t rl_disable;
    rl_disable.raw = nvdebug_readl(g, NV_PFIFO_SCHED_DISABLE);
    rl_disable.raw |= BIT(nr);
    nvdebug_writel(g, NV_PFIFO_SCHED_DISABLE, rl_disable.raw);
  Enabling example:
    runlist_disable_t rl_disable;
    rl_disable.raw = nvdebug_readl(g, NV_PFIFO_SCHED_DISABLE);
    rl_disable.raw &= ~BIT(nr);
    nvdebug_writel(g, NV_PFIFO_SCHED_DISABLE, rl_disable.raw);

  Support: Fermi, Kepler, Maxwell, Pascal, Volta, Turing
*/
#define NV_PFIFO_SCHED_DISABLE 0x00002630
typedef union {
	struct {
		bool runlist_0:1;
		bool runlist_1:1;
		bool runlist_2:1;
		bool runlist_3:1;
		bool runlist_4:1;
		bool runlist_5:1;
		bool runlist_6:1;
		bool runlist_7:1;
		bool runlist_8:1;
		bool runlist_9:1;
		bool runlist_10:1;
		 uint32_t padding:21;
	} __attribute__((packed));
	uint32_t raw;
} runlist_disable_t;

/* Read GPU descriptors from the Master Controller (MC)

  MINOR_REVISION  : Legacy (only used with Celvin in Nouveau)
  MAJOR_REVISION  : Legacy (only used with Celvin in Nouveau)
  IMPLEMENTATION  : Which implementation of the GPU architecture
  ARCHITECTURE    : Which GPU architecture

  CHIP_ID = IMPLEMENTATION + ARCHITECTURE << 4
  CHIP_ID         : Unique ID of all chips since Kelvin

  Support: Kelvin, Rankline, Curie, Tesla, Fermi, Kepler, Maxwell, Pascal,
           Volta, Turing, Ampere
*/
#define NV_MC_BOOT_0 0x00000000
#define NV_CHIP_ID_GP106 0x136 // Discrete GeForce GTX 1060
#define NV_CHIP_ID_GV11B 0x15B // Jetson Xavier embedded GPU
#define NV_CHIP_ID_KEPLER 0x0E0
#define NV_CHIP_ID_VOLTA 0x140

inline static const char* ARCH2NAME(uint32_t arch) {
	switch (arch) {
	case 0x01:
		return "Celsius";
	case 0x02:
		return "Kelvin";
	case 0x03:
		return "Rankline";
	case 0x04:
	case 0x06: // 0x06 is (nForce 6XX integrated only)
		return "Curie";
	// 0x07 is unused/skipped
	case 0x05: // First Tesla card was released before the nForce 6XX
	case 0x08:
	case 0x09:
	case 0x0A:
		return "Tesla";
	// 0x0B is unused/skipped
	case 0x0C:
	case 0x0D:
		return "Fermi";
	case 0x0E:
	case 0x0F:
	case 0x11:
		return "Kepler";
	case 0x12:
		return "Maxwell";
	case 0x13:
		return "Pascal";
	case 0x14:
	case 0x15: // Volta integrated
		return "Volta";
	case 0x16:
		return "Turing";
	case 0x17:
		return "Ampere";
	case 0x18:
		return "Hopper";
	case 0x19:
		return "Ada Lovelace";
	case 0x20:
		return "Blackwell (?)";
	default:
		if (arch < 0x19)
			return "[unknown historical architecture]";
		else
			return "[future]";
	}
}

typedef union {
	// Fields as defined in the NVIDIA reference
	struct {
		uint32_t minor_revision:4;
		uint32_t major_revision:4;
		 uint32_t reserved:4;
		 uint32_t padding0:8;
		uint32_t implementation:4;
		uint32_t architecture:5;
		 uint32_t padding1:3;
	} __attribute__((packed));
	uint32_t raw;
	// Arch << 4 + impl is also often used
	struct {
		 uint32_t padding2:20;
		uint32_t chip_id:9;
		 uint32_t padding3:3;
	} __attribute__((packed));
} mc_boot_0_t;


/* GPU engine information and control register offsets
  Each engine is described by one or more entries (terminated by an entry with
  the `has_next_entry` flag unset) in the fixed-size PTOP_DEVICE_INFO table. A
  typical device, such as the graphics/compute engine and any copy engines, are
  described by three entries, one of each type.

  The PTOP_DEVICE_INFO table is sparsely populated (entries of type
  INFO_TYPE_NOT_VALID may be intermingled with valid entries), so any traversal
  code should check all NV_PTOP_DEVICE_INFO__SIZE_1 entries and not terminate
  upon reaching the first entry of INFO_TYPE_NOT_VALID.

  INFO_TYPE          : Is this a DATA, ENUM, or ENGINE_TYPE table entry?
  HAS_NEXT_ENTRY     : Does the following entry refer to the same engine?

  == INFO_TYPE_DATA fields ==
  PRI_BASE           : BAR0 base = (PRI_BASE << 12) aka 4k aligned.
  INST_ID            : "Note that some instanced [engines] (such as logical copy
                       engines aka LCE) share a PRI_BASE across all [engines] of
                       the same engine type; such [engines] require an additional
                       offset: instanced base = BAR0 base + stride * INST_ID.
  FAULT_ID_IS_VALID  : Does this engine have its own bind point and fault ID
                       with the MMU?
  FAULT_ID           : "The MMU fault id used by this [engine]. These IDs
                       correspond to the NV_PFAULT_MMU_ENG_ID define list."

  == INFO_TYPE_ENUM fields ==
  ENGINE_IS_VALID    : Is this engine a host engine?
  ENGINE_ENUM        : "[T]he host engine ID for the current [engine] if it is
                       a host engine, meaning Host can send methods to the
                       engine. This id is used to index into any register array
                       whose __SIZE_1 is equal to NV_HOST_NUM_ENGINES.  A given
                       ENGINE_ENUM can be present for at most one device in the
                       table.  Devices corresponding to all ENGINE_ENUM ids 0
                       through NV_HOST_NUM_ENGINES - 1 must be present in the
                       device info table."
  RUNLIST_IS_VALID   : Is this engine a host engine with a runlist?
  RUNLIST_ENUM       : "[T]he Host runlist ID on which methods for the current
                       [engine] should be submitted... The runlist id is used to
                       index into any register array whose __SIZE_1 is equal to
                       NV_HOST_NUM_RUNLISTS. [Engines] corresponding to all
                       RUNLIST_ENUM ids 0 through NV_HOST_NUM_RUNLISTS - 1 must
                       be present in the device info table."
  INTR_IS_VALID      : Does this device have an interrupt?
  INTR_ENUM          : Interrupt ID for use with "the NV_PMC_INTR_*_DEVICE
                       register bitfields."
  RESET_IS_VALID     : Does this engine have a reset ID?
  RESET_ENUM         : Reset ID for use indexing the "NV_PMC_ENABLE_DEVICE(i)
                       and NV_PMC_ELPG_ENABLE_DEVICE(i) register bitfields."

  == INFO_TYPE_ENGINE_TYPE fields ==
  ENGINE_TYPE        : What type of engine is this? (see ENGINE_TYPES_NAMES) 

  Support: Kepler, Maxwell, Pascal, Volta, Ampere
  See dev_top.ref.txt of NVIDIA's open-gpu-doc for more info.
*/
#define NV_PTOP_DEVICE_INFO(i) (0x00022700+(i)*4)
#define NV_PTOP_DEVICE_INFO__SIZE_1 64
enum DEVICE_INFO_TYPE {INFO_TYPE_NOT_VALID = 0, INFO_TYPE_DATA = 1, INFO_TYPE_ENUM = 2, INFO_TYPE_ENGINE_TYPE = 3};
enum ENGINE_TYPES {
	ENGINE_GRAPHICS = 0, // GRAPHICS [/compute]
	ENGINE_COPY0 = 1, // [raw/physical] COPY #0
	ENGINE_COPY1 = 2, // [raw/physical] COPY #1
	ENGINE_COPY2 = 3, // [raw/physical] COPY #2

	ENGINE_MSPDEC = 8, // Picture DECoder
	ENGINE_MSPPP = 9, // [Video] Post Processing
	ENGINE_MSVLD = 10, // [Video] Variable Length Decoder
	ENGINE_MSENC = 11, // [Video] ENCoding
	ENGINE_VIC = 12, // Video Image Compositor
	ENGINE_SEC = 13, // SEquenCer [?]
	ENGINE_NVENC0 = 14, // Nvidia Video ENCoder #0
	ENGINE_NVENC1 = 15, // Nvidia Video ENCoder #1
	ENGINE_NVDEC = 16, // Nvidia Video DECoder

	ENGINE_IOCTRL = 18, // I/O ConTRoLler [of NVLINK at least]
	ENGINE_LCE = 19, // Logical Copy Engine
	ENGINE_GSP = 20, // Gpu System Processor
	ENGINE_NVJPG = 21, // NVidia JPeG [Decoder] (Ampere+)
};
#define ENGINE_TYPES_LEN 22
static const char* const ENGINE_TYPES_NAMES[ENGINE_TYPES_LEN] = {
	"Graphics/Compute",
	"COPY0",
	"COPY1",
	"COPY2",
	"Unknown Engine ID#4",
	"Unknown Engine ID#5",
	"Unknown Engine ID#6",
	"Unknown Engine ID#7",
	"MSPDEC: Picture Decoder",
	"MSPPP: Post Processing",
	"MSVLD: Variable Length Decoder",
	"MSENC: Encoder",
	"VIC: Video Image Compositor",
	"SEC: Sequencer",
	"NVENC0: NVIDIA Video Encoder #0",
	"NVENC1: NVIDIA Video Encoder #1",
	"NVDEC: NVIDIA Video Decoder",
	"Unknown Engine ID#17",
	"IOCTRL: I/O Controller",
	"LCE: Logical Copy Engine",
	"GSP: GPU System Processor",
	"NVJPG: NVIDIA JPEG Decoder",
};

typedef union {
	// DATA type fields
	struct {
		enum DEVICE_INFO_TYPE info_type:2;
		bool fault_id_is_valid:1;
		uint32_t fault_id:7;
		 uint32_t padding0:2;
		uint32_t pri_base:12;
		 uint32_t padding1:2;
		uint32_t inst_id:4;
		uint32_t is_not_enum2:1;
		bool has_next_entry:1;
	} __attribute__((packed));
	// ENUM type fields
	struct {
		 uint32_t padding2:2;
		bool reset_is_valid:1;
		bool intr_is_valid:1;
		bool runlist_is_valid:1;
		bool engine_is_valid:1;
		 uint32_t padding3:3;
		uint32_t reset_enum:5;
		 uint32_t padding4:1;
		uint32_t intr_enum:5;
		 uint32_t padding5:1;
		uint32_t runlist_enum:4;
		 uint32_t padding6:1;
		uint32_t engine_enum:4;
		 uint32_t padding7:2;
	} __attribute__((packed));
	// ENGINE_TYPE type fields
	struct {
		 uint32_t padding8:2;
		enum ENGINE_TYPES engine_type:29;
		 uint32_t padding9:1;
	} __attribute__((packed));
	uint32_t raw;
} ptop_device_info_t;

/* Graphics Processing Cluster (GPC) information
  The GPU's Compute/Graphics engine is subdivided into Graphics Processing
  Clusters (also known as GPU Processing Clusters, starting with Ampere).

  Each GPC is subdivided into Texture Processing Clusters (TPCs) which contain
  Streaming Multiprocessors (SMs).


*/
// Support: Fermi through Blackwell
// Get the number of GPCs **on die**
#define NV_PTOP_SCAL_NUM_GPCS 0x00022430
// Get the number of TPCs per GPC **on die**
#define NV_PTOP_SCAL_NUM_TPC_PER_GPC 0x00022434
// GPC and TPC masks
// Support: Maxwell, Pascal, Volta, Turing
// Bitmask of which GPC **are enabled** of the max on die
#define NV_FUSE_GPC 0x00021c1c
// Bitmask of which TPCs **are enabled** on each GPC
#define NV_FUSE_TPC_FOR_GPC(i) (0x00021c38+(i)*4)
// Support: Ampere, Ada, Hopper, Blackwell
//#define NV_FUSE_GPC 0x00820c1c
//#define NV_FUSE_TPC_FOR_GPC(i) (0x00820c38+(i)*4)

/* Logical Copy Engine (LCE) Information
  Every GPU has some number of copy engines which can process transfers to,
  from, or within a GPU. Up until Maxwell, the hardware engines were directly
  accessible, and this register exposes how many there are.

  Starting with Pascal, an additional layer of indirection was added---logical
  copy engines. Only logical copy engines can be directly dispatched to, and
  there are normally more logical copy engines than there are physical ones. On
  Pascal+ this register stores the number of logical copy engines.

  SCAL_NUM_CES : Number of externally accessible copy engines

  Support: Kepler through (at least) Blackwell
  Also see dev_ce.ref.txt of NVIDIA's open-gpu-doc for info.
*/
#define NV_PTOP_SCAL_NUM_CES 0x00022444

/* Physical Copy Engine (PCE) information
  On Pascal GPUs or newer, this register complements the above information by
  exposing which, and how many, physical copy engines are enabled on the GPU.

  CE_PCE_MAP : A bitmask, where a set bit indicates that the PCE for that index
               is enabled (not floorswept) on this GPU. Count the number of set
               bits to get the number of PCEs.

  Support: Kepler through (at least) Blackwell
  Also see dev_ce.ref.txt of NVIDIA's open-gpu-doc for info.
*/
#define NV_CE_PCE_MAP 0x00104028


/* Location of the 1Kb instance block with page tables for BAR1 and BAR2.
  Support: Fermi+ (?), Pascal
*/
#define NV_PBUS_BAR1_BLOCK 0x00001704
#define NV_PBUS_BAR2_BLOCK 0x00001714
typedef union {
	struct {
		uint32_t ptr:28;
		enum INST_TARGET target:2;
		 uint32_t padding0:1;
		bool is_virtual:1;
	} __attribute__((packed));
	uint32_t raw;
	struct {
		uint32_t map:30;
		 uint32_t padding1:2;
	} __attribute__((packed));
} bar_config_block_t;

/* BAR0 PRAMIN (Private RAM Instance) window configuration

  BASE    : Base of window >> 16 in [TARGET] virtual address space
  TARGET  : Which address space BASE points into

  Note: This seems to be set to 0x0bff00000 - 0x0c0000000 at least sometimes

  Support: Tesla 2.0, Fermi, Kepler, Maxwell, Pascal, Turing, Ampere
*/
#define NV_PBUS_BAR0_WINDOW 0x00001700
#define NV_PRAMIN 0x00700000  // Goes until 0x00800000 (1MB window)
#define NV_PRAMIN_LEN 0x00100000
typedef union {
	struct {
		uint32_t base:24;
		enum INST_TARGET target:2;
		 uint32_t padding0:6;
	} __attribute__((packed));
	uint32_t raw;
} bar0_window_t;

// Support: Tesla 2.0, Fermi, Kepler, Maxwell, Pascal, Turing, Ampere
#define NV_PRAMIN_PDB_CONFIG_OFF 0x200
typedef union {
	struct {
		uint32_t target:2;
		uint32_t vol:1;
		 uint32_t padding0:1;
		uint32_t fault_replay_tex:1;
		uint32_t fault_replay_gcc:1;
		 uint32_t padding1:4;
		bool is_ver2:1;
		bool is_64k_big_page:1;  // 128Kb otherwise
		uint32_t page_dir_lo:20;
		uint32_t page_dir_hi:32;
	} __attribute__((packed));
	uint64_t raw;
} page_dir_config_t;

/* Page directory entry

  Note: Format changed with Pascal (how?)

  Support: Pascal, Volta, Turing, Ampere, Ada
*/
// FIXME: PDE/PTEs are actually 64 bits =S
// Important: Aperture keys are different with PDEs
enum PD_TARGET {
	PD_AND_TARGET_INVALID = 0,  // b000
	PD_AND_TARGET_VID_MEM = 2,  // b010
	PD_AND_TARGET_SYS_MEM_COHERENT = 4,  // b100
	PD_AND_TARGET_SYS_MEM_NONCOHERENT = 6,  // b110
	PTE_AND_TARGET_VID_MEM = 1,  // b001
	PTE_AND_TARGET_PEER = 3,  // b011
	PTE_AND_TARGET_SYS_MEM_COHERENT = 5,  // b101
	PTE_AND_TARGET_SYS_MEM_NONCOHERENT = 7,  // b111
};
static inline char* pd_target_to_text(enum PD_TARGET t) {
	switch (t) {
		case PD_AND_TARGET_INVALID:
			return "INVALID";
		case PD_AND_TARGET_VID_MEM:
		case PTE_AND_TARGET_VID_MEM:
			return "VID_MEM";
		case PTE_AND_TARGET_PEER:
			return "PEER";
		case PD_AND_TARGET_SYS_MEM_COHERENT:
		case PTE_AND_TARGET_SYS_MEM_COHERENT:
			return "SYS_MEM_COHERENT";
		case PD_AND_TARGET_SYS_MEM_NONCOHERENT:
		case PTE_AND_TARGET_SYS_MEM_NONCOHERENT:
			return "SYS_MEM_NONCOHERENT";
		default:
			printk(KERN_WARNING "[nvdebug] Invalid aperture!\n");
			return NULL;
	}
}

// PDE/PTE V2 type
// Note: As the meaning of target (bits 2:1) changes depending on if the entry
//       is a PTE or not, this combines them into a single target field to
//       simplify comparisons.
// Support: Pascal, Volta, Turing, Ampere, Ada
//
// V3 introduced with Hopper, but Hopper and Blackwell also support V2
typedef union {
	// Page Directory Entry (PDE)
	struct {
		bool is_pte:1;
		 uint32_t __target:2;
		bool is_volatile:1;
		 uint32_t padding1:4;
		uint32_t addr:24;
	} __attribute__((packed));
	// Page Table Entry (PTE)
	struct {
		enum PD_TARGET target:3;
		 uint32_t __is_volatile:1;
		bool is_encrypted:1;
		bool is_privileged:1;
		bool is_readonly:1;
		bool atomics_disabled:1;
		 uint32_t __addr:24;
	} __attribute__((packed));
	uint32_t raw;
} page_dir_entry_t;

// PDE/PTE V1 types
// Support: Fermi, Kepler, Maxwell
enum V1_PD_TARGET {
	PD_TARGET_INVALID = 0,
	PD_TARGET_VID_MEM = 1,
	PD_TARGET_SYS_MEM_COHERENT = 2,
	PD_TARGET_SYS_MEM_NONCOHERENT = 3,
};
// Page Directory Entry (PDE)
typedef union {
// Large page fields
	struct {
// 0:32
		enum V1_PD_TARGET target:2;
		 uint32_t padding0:2;
		uint64_t addr:28;  // May be wider?
// 32:63
		 uint32_t padding2:3;
		uint32_t is_volatile:1; // Might have counted wrong?
		 uint32_t padding3:28;
	} __attribute__((packed));
// Small page fields
	struct {
// 0:32
		 uint32_t padding00:32;
// 32:63
		enum V1_PD_TARGET alt_target:2;
		uint32_t alt_is_volatile:1; // Might have counted wrong?
		 uint32_t padding03:1;
		uint64_t alt_addr:28;
	} __attribute__((packed));
	uint64_t raw;
} page_dir_entry_v1_t;
// Page Table Entry (PTE)
// Reconstructed from info in Jetson nvgpu driver
typedef union {
	struct {
// 0:32
		bool is_present:1;
		bool is_privileged:1;
		bool is_readonly:1;
		 uint32_t padding0:1;
		uint64_t addr:28;
// 32:63
		bool is_volatile:1;
		enum INST_TARGET:2;
		 uint32_t padding1:1;
		uint32_t kind:8;
		uint32_t comptag:17;
		 uint32_t padding2:1;
		bool is_read_disabled:1;
		bool is_write_disabled:1;
	} __attribute__((packed));
	uint64_t raw;
} page_tbl_entry_v1_t;
//enum V0_PDE_TYPE {NOT_PRESENT = 0, PAGE_64K = 1, PAGE_16K = 2, PAGE_4K = 3};
//enum V0_PDE_SIZE {PDE_SZ_128K = 0, PDE_SZ_32K = 1, PDE_SZ_16K = 2, PDE_SZ_8K = 3};
//static const int V0_PDE_SIZE2NUM[4] = {128*1024, 32*1024, 16*1024, 8*1024};
/* PDE V0 (nv50/Tesla)
typedef union {
	struct {
		enum V1_PDE_TYPE type:2;
		enum INST_TARGET target:2;
		 uint32_t padding0:1;
		enum V1_PDE_SIZE sublevel_size:2;
		 uint32_t padding1:5;
		uint32_t addr:28;
		 uint32_t padding2:24;
	} __attribute__((packed));
	uint64_t raw;
} page_dir_entry_v1_t;*/
/* PTE V0 (nv50)
typedef union {
	struct {
		bool is_present:1;
		 uint32_t padding3:2;
		bool is_readonly:1;
		enum INST_TARGET target:2;
		bool is_privileged:1;
		uint32_t contig_blk_sz:3;
		 uint32_t padding4:2;
		uint32_t addr:28;
		uint32_t storage_type:7;  // ???
		uint32_t compression_mode:2;  // ???
		uint32_t compression_tag:12;  // ???
		bool is_long_partition_cycle:1;  // ???
		bool is_encrypted:1;
		 uint32_t padding5:1;
	} __attribute__((packed));
	uint64_t raw;
} page_tbl_entry_v1_t;*/

// TODO(jbakita): Maybe put the above GPU types in a different file.

#define NV_PCI_VENDOR 0x10de
struct nvdebug_state {
	// Pointer to the mapped base address of the GPU control registers (obtained
	// via ioremap() originally). For embedded GPUs, we extract this from their
	// struct nvgpu_os_linux. For discrete GPUs, we create our own mapping of
	// BAR0 with pci_iomap(). Access via nvgpu_readl/writel functions.
	void __iomem *regs;
	// Depending on the architecture, BAR2 or BAR3 are used to access PRAMIN
	union {
		void __iomem *bar2;
		void __iomem *bar3;
	};
	int chip_id;
	// Additional state from the built-in driver. Only set iff
	// chip_id == NV_CHIP_ID_GV11B
	struct gk20a *g;
	// Pointer to PCI device needed for pci_iounmap
	struct pci_dev *pcid;
};

/*const struct runlist_funcs {
	u8 size;
	enum ENTRY_TYPE (*entry_type)(struct nvdebug_state *, void *);
	uint32_t (*chid)(struct nvdebug_state *, void *);
	uint32_t (*inst_ptr_lo)(struct nvdebug_state *, void *);
	enum INST_TARGET (*inst_target)(struct nvdebug_state *, void *):
	uint32_t (*tsgid)(struct nvdebug_state *, void *);
	uint32_t (*timeslice_scale)(struct nvdebug_state *, void *);
	uint32_t (*timeslice_timeout)(struct nvdebug_state *, void *);
	uint32_t (*tsg_length)(struct nvdebug_state *, void *);
};*/

// This disgusting macro is a crutch to work around the fact that runlists were
// different prior to Volta.
#define VERSIONED_RL_ACCESSOR(_ENTRY_TYPE, type, prop) \
	__attribute__((unused)) \
	static type (prop)(const struct nvdebug_state *g, const void *raw) { \
		if (g->chip_id > NV_CHIP_ID_VOLTA) { \
			const struct gv100_runlist_ ## _ENTRY_TYPE *entry = (struct gv100_runlist_ ## _ENTRY_TYPE*)raw; \
			return entry->prop; \
		} else if (g->chip_id > NV_CHIP_ID_KEPLER) { \
			const struct gk110_runlist_ ## _ENTRY_TYPE *entry = (struct gk110_runlist_ ## _ENTRY_TYPE*)raw; \
			return entry->prop; \
		} else { \
			printk(KERN_WARNING "[nvdebug] " #prop " unavailable on GPU ID %x, which is older than Kepler.\n", g->chip_id); \
			return (type)0; \
		} \
	}

VERSIONED_RL_ACCESSOR(chan, uint32_t, chid);
VERSIONED_RL_ACCESSOR(chan, uint32_t, inst_ptr_lo);
VERSIONED_RL_ACCESSOR(chan, enum INST_TARGET, inst_target);
VERSIONED_RL_ACCESSOR(tsg, uint32_t, tsgid);
VERSIONED_RL_ACCESSOR(tsg, enum ENTRY_TYPE, entry_type);
VERSIONED_RL_ACCESSOR(tsg, uint32_t, timeslice_scale);
VERSIONED_RL_ACCESSOR(tsg, uint32_t, timeslice_timeout);
VERSIONED_RL_ACCESSOR(tsg, uint32_t, tsg_length);


#define NV_RL_ENTRY_SIZE(g) \
	 ((g)->chip_id >= NV_CHIP_ID_VOLTA ? sizeof(struct gv100_runlist_tsg) : sizeof(struct gk110_runlist_tsg))

#define for_chan_in_tsg(g, chan, tsg) \
        for (chan = (typeof(chan))(((u8*)tsg) + NV_RL_ENTRY_SIZE(g)); \
             (u8*)chan < ((u8*)tsg) + (1 + tsg_length(g, tsg)) * NV_RL_ENTRY_SIZE(g); \
             chan = (typeof(chan))(((u8*)chan) + NV_RL_ENTRY_SIZE(g)))

#define next_tsg(g, tsg) \
        (typeof(tsg))((u8*)(tsg) + NV_RL_ENTRY_SIZE(g) * (tsg_length(g, tsg) + 1))

struct runlist_iter {
	// Pointer to either a TSG or channel entry (they're the same size)
	void *curr_entry;
	// This should be set to tsg_length when a TSG is reached, and
	// decremented as each subsequent channel is printed. This allows us to
	// track which channel are and are not part of the TSG.
	int channels_left_in_tsg;
	// Total runlist length, etc
	runlist_info_t rl_info;
};

#define NVDEBUG_MAX_DEVICES 8
extern struct nvdebug_state g_nvdebug_state[NVDEBUG_MAX_DEVICES];

// Defined in runlist.c
int get_runlist_iter(struct nvdebug_state *g, int rl_id, struct runlist_iter *rl_iter);
int preempt_tsg(struct nvdebug_state *g, uint32_t tsg_id);

// Defined in mmu.c
uint32_t vram2PRAMIN(struct nvdebug_state *g, uint64_t addr);
void __iomem *phy2PRAMIN(struct nvdebug_state* g, uint64_t phy);
uint64_t search_page_directory(
	struct nvdebug_state *g,
	void __iomem *pde_offset,
	void __iomem *(*off2addr)(struct nvdebug_state*, uint64_t),
	uint64_t addr_to_find);
uint64_t search_v1_page_directory(
	struct nvdebug_state *g,
	void __iomem *pde_offset,
	void __iomem *(*off2addr)(struct nvdebug_state*, uint64_t),
	uint64_t addr_to_find);


static inline struct gk20a *get_gk20a(struct device *dev) {
        // XXX: Only works because gk20a* is the first member of gk20a_platform
        return *((struct gk20a**)dev_get_drvdata(dev));
}

// We us the data field of the proc_dir_entry ("PDE" in this function) to store
// our index into the g_nvdebug_state array
static inline int seq2gpuidx(struct seq_file *s) {
        const struct file *f = s->file;
        return (uintptr_t)PDE_DATA(file_inode(f));
}
static inline int file2gpuidx(const struct file *f) {
        return (uintptr_t)PDE_DATA(file_inode(f));
}
static inline int file2parentgpuidx(const struct file *f) {
	// Should be safe to call on ProcFS entries, as our parent should (?)
	// still exist if we're called. If not, there are worse races in this
	// module.
	return (uintptr_t)PDE_DATA(file_dentry(f)->d_parent->d_inode);
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)
// Commit 643eb158a3 in nvgpu moved the mapped registers to the second entry
// of the gk20a struct (after a function pointer). This change was made as L4T
// was upgraded from Linux 4.9 to 5.10 (r32 -> r34+)
// Note that this is wrong if nvgpu was built without CONFIG_NVGPU_NON_FUSA
// i.e. if FUSA was enabled, this is wrong.
#define gk20a_regs(gk20a) (*(void**)((void*)gk20a + sizeof(void(*)(void))))
#else
#include <os/linux/os_linux.h>  // For struct nvgpu_os_linux, which holds regs
#define gk20a_regs(gk20a) (container_of(gk20a, struct nvgpu_os_linux, g)->regs)
#endif

// Similar to nvgpu_readl()
// (except we don't try to resolve situations where regs is NULL)
static inline u32 nvdebug_readl(struct nvdebug_state *s, u32 r) {
	u32 ret;
	if (unlikely(!s->regs || (s->g && !gk20a_regs(s->g)))) {
		printk(KERN_ERR "[nvdebug] nvdebug_readl: Unable to read; registers unavailable. Is GPU on?\n");
		return -1;
	}
	ret = readl(s->regs + r);
	// It seems like the GPU returns this as a flag value for bad addresses
	if (ret == 0xbadf5040) {
		printk(KERN_ERR "[nvdebug] nvdebug_readl: Unable to read from register offset %#x; bad data\n", r);
		return -1;
	}
	return ret;
}

// quadword version of nvdebug_readl()
static inline u64 nvdebug_readq(struct nvdebug_state *s, u32 r) {
	u64 ret;
	if (unlikely(!s->regs || (s->g && !gk20a_regs(s->g)))) {
		printk(KERN_ERR "[nvdebug] nvdebug_readq: Unable to read; registers unavailable. Is GPU on?\n");
		return -1;
	}
	// readq seems to always return the uppermost 32 bits as 0, so workaround with readl
	ret = readl(s->regs + r);
	ret |= ((u64)readl(s->regs + r + 4)) << 32;
	// It seems like the GPU returns this as a flag value for bad addresses
	if ((ret & 0xffffffffull) == 0xbadf5040ull) {
		printk(KERN_ERR "[nvdebug] nvdebug_readq: Unable to read from register offset %#x; bad data\n", r);
		return -1;
	}
	return ret;
}

// Similar to nvgpu_writel()
static inline void nvdebug_writel(struct nvdebug_state *s, u32 r, u32 v) {
	if (unlikely(!s->regs || (s->g && !gk20a_regs(s->g)))) {
		printk(KERN_ERR "[nvdebug] nvdebug_writel: Unable to write; registers unavailable. Is GPU on?\n");
		return;
	}
	writel_relaxed(v, s->regs + r);
	wmb();
}

// quadword version of nvdebug_writel()
// XXX: This probably doesn't work XXX: Untested
static inline void nvdebug_writeq(struct nvdebug_state *s, u32 r, u64 v) {
	if (unlikely(!s->regs || (s->g && !gk20a_regs(s->g)))) {
		printk(KERN_ERR "[nvdebug] nvdebug_writeq: Unable to write; registers unavailable. Is GPU on?\n");
		return;
	}
	writeq_relaxed(v, s->regs + r);
	wmb();
}