summaryrefslogtreecommitdiff
path: root/drivers/edgetpu/edgetpu-usage-stats.c
blob: ba93d497ec391104afcb85e4d33936755b1b5587 (plain)
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
// SPDX-License-Identifier: GPL-2.0
/*
 * EdgeTPU usage stats
 *
 * Copyright (C) 2020 Google, Inc.
 */

#include <linux/slab.h>
#include <linux/sysfs.h>

#include "edgetpu-config.h"
#include "edgetpu-internal.h"
#include "edgetpu-kci.h"
#include "edgetpu-usage-stats.h"

/* Max number of frequencies to support */
#define EDGETPU_MAX_STATES	10

struct uid_entry {
	int32_t uid;
	uint64_t time_in_state[EDGETPU_MAX_STATES];
	struct hlist_node node;
};

static int tpu_state_map(struct edgetpu_dev *etdev, uint32_t state)
{
	int i, idx = 0;

	mutex_lock(&etdev->freq_lock);
	/* Use frequency table if f/w already reported via usage_stats */
	if (etdev->freq_table) {
		for (i = etdev->freq_count - 1; i >= 0; i--) {
			if (state == etdev->freq_table[i])
				idx = i;
		}
		mutex_unlock(&etdev->freq_lock);
		return idx;
	}

	mutex_unlock(&etdev->freq_lock);

	/*
	 * use predefined state table in case of no f/w reported supported
	 * frequencies.
	 */
	for (i = (EDGETPU_NUM_STATES - 1); i >= 0; i--) {
		if (state >= edgetpu_active_states[i])
			return i;
	}

	return 0;
}

/* Caller must hold usage_stats lock */
static struct uid_entry *
find_uid_entry_locked(int32_t uid, struct edgetpu_usage_stats *ustats)
{
	struct uid_entry *uid_entry;

	hash_for_each_possible(ustats->uid_hash_table, uid_entry, node, uid) {
		if (uid_entry->uid == uid)
			return uid_entry;
	}

	return NULL;
}

int edgetpu_usage_add(struct edgetpu_dev *etdev, struct tpu_usage *tpu_usage)
{
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;
	struct uid_entry *uid_entry;
	int state = tpu_state_map(etdev, tpu_usage->power_state);

	if (!ustats)
		return 0;

	etdev_dbg(etdev, "%s: uid=%u state=%u dur=%u", __func__,
		  tpu_usage->uid, tpu_usage->power_state,
		  tpu_usage->duration_us);
	mutex_lock(&ustats->usage_stats_lock);

	/* Find the uid in uid_hash_table first */
	uid_entry = find_uid_entry_locked(tpu_usage->uid, ustats);
	if (uid_entry) {
		uid_entry->time_in_state[state] += tpu_usage->duration_us;
		mutex_unlock(&ustats->usage_stats_lock);
		return 0;
	}

	/* Allocate memory for this uid */
	uid_entry = kzalloc(sizeof(*uid_entry), GFP_KERNEL);
	if (!uid_entry) {
		mutex_unlock(&ustats->usage_stats_lock);
		return -ENOMEM;
	}

	uid_entry->uid = tpu_usage->uid;
	uid_entry->time_in_state[state] += tpu_usage->duration_us;

	/* Add uid_entry to the uid_hash_table */
	hash_add(ustats->uid_hash_table, &uid_entry->node, tpu_usage->uid);

	mutex_unlock(&ustats->usage_stats_lock);

	return 0;
}

static void edgetpu_utilization_update(
	struct edgetpu_dev *etdev,
	struct edgetpu_component_activity *activity)
{
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;

	if (!ustats)
		return;

	etdev_dbg(etdev, "%s: comp=%d utilized %d%%\n", __func__,
		  activity->component, activity->utilization);

	mutex_lock(&ustats->usage_stats_lock);
	if (activity->utilization && activity->component >= 0 &&
	    activity->component < EDGETPU_USAGE_COMPONENT_COUNT)
		ustats->component_utilization[activity->component] =
			activity->utilization;
	mutex_unlock(&ustats->usage_stats_lock);
}

static void edgetpu_counter_update(
	struct edgetpu_dev *etdev,
	struct edgetpu_usage_counter *counter)
{
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;

	if (!ustats)
		return;

	etdev_dbg(etdev, "%s: type=%d value=%llu\n", __func__,
		  counter->type, counter->value);

	mutex_lock(&ustats->usage_stats_lock);
	if (counter->type >= 0 && counter->type < EDGETPU_COUNTER_COUNT)
		ustats->counter[counter->type] += counter->value;
	mutex_unlock(&ustats->usage_stats_lock);
}

static void edgetpu_counter_clear(
	struct edgetpu_dev *etdev,
	enum edgetpu_usage_counter_type counter_type)
{
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;

	if (!ustats)
		return;
	if (counter_type >= EDGETPU_COUNTER_COUNT)
		return;

	mutex_lock(&ustats->usage_stats_lock);
	ustats->counter[counter_type] = 0;
	mutex_unlock(&ustats->usage_stats_lock);
}

static void edgetpu_max_watermark_update(
	struct edgetpu_dev *etdev,
	struct edgetpu_usage_max_watermark *max_watermark)
{
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;

	if (!ustats)
		return;

	etdev_dbg(etdev, "%s: type=%d value=%llu\n", __func__,
		  max_watermark->type, max_watermark->value);

	if (max_watermark->type < 0 ||
	    max_watermark->type >= EDGETPU_MAX_WATERMARK_TYPE_COUNT)
		return;

	mutex_lock(&ustats->usage_stats_lock);
	if (max_watermark->value > ustats->max_watermark[max_watermark->type])
		ustats->max_watermark[max_watermark->type] =
			max_watermark->value;
	mutex_unlock(&ustats->usage_stats_lock);
}

static void edgetpu_thread_stats_update(
	struct edgetpu_dev *etdev,
	struct edgetpu_thread_stats *thread_stats)
{
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;

	if (!ustats)
		return;

	etdev_dbg(etdev, "%s: id=%d stackmax=%u\n", __func__,
		  thread_stats->thread_id, thread_stats->max_stack_usage_bytes);

	if (thread_stats->thread_id < 0 ||
	    thread_stats->thread_id >= EDGETPU_FW_THREAD_COUNT)
		return;

	mutex_lock(&ustats->usage_stats_lock);
	if (thread_stats->max_stack_usage_bytes >
	    ustats->thread_stack_max[thread_stats->thread_id])
		ustats->thread_stack_max[thread_stats->thread_id] =
			thread_stats->max_stack_usage_bytes;
	mutex_unlock(&ustats->usage_stats_lock);
}

/* Record new supported frequencies if reported by firmware */
static void edgetpu_dvfs_frequency_update(struct edgetpu_dev *etdev, uint32_t frequency)
{
	uint32_t *freq_table, i;

	mutex_lock(&etdev->freq_lock);
	if (!etdev->freq_table) {
		freq_table = kvmalloc(EDGETPU_MAX_STATES * sizeof(uint32_t), GFP_KERNEL);
		if (!freq_table) {
			etdev_warn(etdev, "Unable to create supported frequencies table");
			goto out;
		}
		etdev->freq_count = 0;
		etdev->freq_table = freq_table;
	}

	freq_table = etdev->freq_table;

	for (i = 0; i < etdev->freq_count; i++) {
		if (freq_table[i] == frequency)
			goto out;
	}

	if (etdev->freq_count >= EDGETPU_MAX_STATES) {
		etdev_warn(etdev, "Unable to record supported frequencies");
		goto out;
	}

	freq_table[etdev->freq_count++] = frequency;
out:
	mutex_unlock(&etdev->freq_lock);
}

void edgetpu_usage_stats_process_buffer(struct edgetpu_dev *etdev, void *buf)
{
	struct edgetpu_usage_header *header = buf;
	struct edgetpu_usage_metric *metric =
		(struct edgetpu_usage_metric *)(header + 1);
	int i;

	etdev_dbg(etdev, "%s: n=%u sz=%u", __func__,
		  header->num_metrics, header->metric_size);
	if (header->metric_size != sizeof(struct edgetpu_usage_metric)) {
		etdev_dbg(etdev, "%s: expected sz=%zu, discard", __func__,
			  sizeof(struct edgetpu_usage_metric));
		return;
	}

	for (i = 0; i < header->num_metrics; i++) {
		switch (metric->type) {
		case EDGETPU_METRIC_TYPE_TPU_USAGE:
			edgetpu_usage_add(etdev, &metric->tpu_usage);
			break;
		case EDGETPU_METRIC_TYPE_COMPONENT_ACTIVITY:
			edgetpu_utilization_update(
				etdev, &metric->component_activity);
			break;
		case EDGETPU_METRIC_TYPE_COUNTER:
			edgetpu_counter_update(etdev, &metric->counter);
			break;
		case EDGETPU_METRIC_TYPE_MAX_WATERMARK:
			edgetpu_max_watermark_update(
				etdev, &metric->max_watermark);
			break;
		case EDGETPU_METRIC_TYPE_THREAD_STATS:
			edgetpu_thread_stats_update(
				etdev, &metric->thread_stats);
			break;
		case EDGETPU_METRIC_TYPE_DVFS_FREQUENCY_INFO:
			edgetpu_dvfs_frequency_update(
				etdev, metric->dvfs_frequency_info);
			break;
		default:
			etdev_dbg(etdev, "%s: %d: skip unknown type=%u",
				  __func__, i, metric->type);
			break;
		}

		metric++;
	}
}

int edgetpu_usage_get_utilization(struct edgetpu_dev *etdev,
				  enum edgetpu_usage_component component)
{
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;
	int32_t val;

	if (component >= EDGETPU_USAGE_COMPONENT_COUNT)
		return -1;
	edgetpu_kci_update_usage(etdev);
	mutex_lock(&ustats->usage_stats_lock);
	val = ustats->component_utilization[component];
	ustats->component_utilization[component] = 0;
	mutex_unlock(&ustats->usage_stats_lock);
	return val;
}

static int64_t edgetpu_usage_get_counter(
	struct edgetpu_dev *etdev,
	enum edgetpu_usage_counter_type counter_type)
{
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;
	int64_t val;

	if (counter_type >= EDGETPU_COUNTER_COUNT)
		return -1;
	edgetpu_kci_update_usage(etdev);
	mutex_lock(&ustats->usage_stats_lock);
	val = ustats->counter[counter_type];
	mutex_unlock(&ustats->usage_stats_lock);
	return val;
}

static int64_t edgetpu_usage_get_max_watermark(
	struct edgetpu_dev *etdev,
	enum edgetpu_usage_max_watermark_type max_watermark_type)
{
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;
	int64_t val;

	if (max_watermark_type >= EDGETPU_MAX_WATERMARK_TYPE_COUNT)
		return -1;
	edgetpu_kci_update_usage(etdev);
	mutex_lock(&ustats->usage_stats_lock);
	val = ustats->max_watermark[max_watermark_type];
	mutex_unlock(&ustats->usage_stats_lock);
	return val;
}

static ssize_t tpu_usage_show(struct device *dev,
			      struct device_attribute *attr,
			      char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;
	int i;
	int ret = 0;
	unsigned int bkt;
	struct uid_entry *uid_entry;

	edgetpu_kci_update_usage(etdev);
	/* uid: state0speed state1speed ... */
	ret += scnprintf(buf, PAGE_SIZE, "uid:");

	mutex_lock(&etdev->freq_lock);
	if (!etdev->freq_table) {
		mutex_unlock(&etdev->freq_lock);
		for (i = 0; i < EDGETPU_NUM_STATES; i++)
			ret += scnprintf(buf + ret, PAGE_SIZE - ret, " %d",
					 edgetpu_states_display[i]);
	} else {
		for (i = 0; i < etdev->freq_count; i++)
			ret += scnprintf(buf + ret, PAGE_SIZE - ret, " %d",
					 etdev->freq_table[i]);
		mutex_unlock(&etdev->freq_lock);
	}

	ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");

	mutex_lock(&ustats->usage_stats_lock);

	hash_for_each(ustats->uid_hash_table, bkt, uid_entry, node) {
		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%d:",
				 uid_entry->uid);

		for (i = 0; i < EDGETPU_NUM_STATES; i++)
			ret += scnprintf(buf + ret, PAGE_SIZE - ret, " %lld",
					 uid_entry->time_in_state[i]);

		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
	}

	mutex_unlock(&ustats->usage_stats_lock);

	return ret;
}

static void usage_stats_remove_uids(struct edgetpu_usage_stats *ustats)
{
	unsigned int bkt;
	struct uid_entry *uid_entry;
	struct hlist_node *tmp;

	mutex_lock(&ustats->usage_stats_lock);

	hash_for_each_safe(ustats->uid_hash_table, bkt, tmp, uid_entry, node) {
		hash_del(&uid_entry->node);
		kfree(uid_entry);
	}

	mutex_unlock(&ustats->usage_stats_lock);
}

/* Write to clear all entries in uid_hash_table */
static ssize_t tpu_usage_clear(struct device *dev,
			       struct device_attribute *attr,
			       const char *buf,
			       size_t count)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;

	usage_stats_remove_uids(ustats);

	return count;
}

static DEVICE_ATTR(tpu_usage, 0664, tpu_usage_show, tpu_usage_clear);

static ssize_t device_utilization_show(struct device *dev,
				       struct device_attribute *attr,
				       char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	int32_t val;

	val = edgetpu_usage_get_utilization(
			etdev, EDGETPU_USAGE_COMPONENT_DEVICE);
	return scnprintf(buf, PAGE_SIZE, "%d\n", val);
}
static DEVICE_ATTR_RO(device_utilization);

static ssize_t tpu_utilization_show(struct device *dev,
				    struct device_attribute *attr,
				    char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	int32_t val;

	val = edgetpu_usage_get_utilization(
			etdev, EDGETPU_USAGE_COMPONENT_TPU);
	return scnprintf(buf, PAGE_SIZE, "%d\n", val);
}
static DEVICE_ATTR_RO(tpu_utilization);

static ssize_t tpu_active_cycle_count_show(struct device *dev,
					   struct device_attribute *attr,
					   char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	int64_t val;

	val = edgetpu_usage_get_counter(etdev,
					EDGETPU_COUNTER_TPU_ACTIVE_CYCLES);
	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
}

static ssize_t tpu_active_cycle_count_store(struct device *dev,
					    struct device_attribute *attr,
					    const char *buf,
					    size_t count)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);

	edgetpu_counter_clear(etdev, EDGETPU_COUNTER_TPU_ACTIVE_CYCLES);
	return count;
}
static DEVICE_ATTR(tpu_active_cycle_count, 0664, tpu_active_cycle_count_show,
		   tpu_active_cycle_count_store);

static ssize_t tpu_throttle_stall_count_show(struct device *dev,
					     struct device_attribute *attr,
					     char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	int64_t val;

	val = edgetpu_usage_get_counter(etdev,
					EDGETPU_COUNTER_TPU_THROTTLE_STALLS);
	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
}

static ssize_t tpu_throttle_stall_count_store(struct device *dev,
					      struct device_attribute *attr,
					      const char *buf,
					      size_t count)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);

	edgetpu_counter_clear(etdev, EDGETPU_COUNTER_TPU_THROTTLE_STALLS);
	return count;
}
static DEVICE_ATTR(tpu_throttle_stall_count, 0664,
		   tpu_throttle_stall_count_show,
		   tpu_throttle_stall_count_store);

static ssize_t inference_count_show(struct device *dev,
				    struct device_attribute *attr, char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	int64_t val;

	val = edgetpu_usage_get_counter(etdev,
					EDGETPU_COUNTER_INFERENCES);
	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
}

static ssize_t inference_count_store(struct device *dev,
				     struct device_attribute *attr,
				     const char *buf,
				     size_t count)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);

	edgetpu_counter_clear(etdev, EDGETPU_COUNTER_INFERENCES);
	return count;
}
static DEVICE_ATTR(inference_count, 0664, inference_count_show,
		   inference_count_store);

static ssize_t tpu_op_count_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	int64_t val;

	val = edgetpu_usage_get_counter(etdev,
					EDGETPU_COUNTER_TPU_OPS);
	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
}

static ssize_t tpu_op_count_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf,
				  size_t count)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);

	edgetpu_counter_clear(etdev, EDGETPU_COUNTER_TPU_OPS);
	return count;
}
static DEVICE_ATTR(tpu_op_count, 0664, tpu_op_count_show, tpu_op_count_store);

static ssize_t param_cache_hit_count_show(struct device *dev,
					  struct device_attribute *attr,
					  char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	int64_t val;

	val = edgetpu_usage_get_counter(etdev,
					EDGETPU_COUNTER_PARAM_CACHE_HITS);
	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
}

static ssize_t param_cache_hit_count_store(struct device *dev,
					   struct device_attribute *attr,
					   const char *buf,
					   size_t count)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);

	edgetpu_counter_clear(etdev, EDGETPU_COUNTER_PARAM_CACHE_HITS);
	return count;
}
static DEVICE_ATTR(param_cache_hit_count, 0664, param_cache_hit_count_show,
		   param_cache_hit_count_store);

static ssize_t param_cache_miss_count_show(struct device *dev,
					   struct device_attribute *attr,
					   char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	int64_t val;

	val = edgetpu_usage_get_counter(etdev,
					EDGETPU_COUNTER_PARAM_CACHE_MISSES);
	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
}

static ssize_t param_cache_miss_count_store(struct device *dev,
					    struct device_attribute *attr,
					    const char *buf,
					    size_t count)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);

	edgetpu_counter_clear(etdev, EDGETPU_COUNTER_PARAM_CACHE_MISSES);
	return count;
}
static DEVICE_ATTR(param_cache_miss_count, 0664, param_cache_miss_count_show,
		   param_cache_miss_count_store);

static ssize_t context_preempt_count_show(struct device *dev,
					  struct device_attribute *attr,
					  char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	int64_t val;

	val = edgetpu_usage_get_counter(etdev,
					EDGETPU_COUNTER_CONTEXT_PREEMPTS);
	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
}

static ssize_t context_preempt_count_store(struct device *dev,
					   struct device_attribute *attr,
					   const char *buf,
					   size_t count)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);

	edgetpu_counter_clear(etdev, EDGETPU_COUNTER_CONTEXT_PREEMPTS);
	return count;
}
static DEVICE_ATTR(context_preempt_count, 0664, context_preempt_count_show,
		   context_preempt_count_store);

static ssize_t hardware_preempt_count_show(struct device *dev, struct device_attribute *attr,
					   char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	int64_t val;

	val = edgetpu_usage_get_counter(etdev, EDGETPU_COUNTER_HARDWARE_PREEMPTS);
	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
}

static ssize_t hardware_preempt_count_store(struct device *dev, struct device_attribute *attr,
					    const char *buf, size_t count)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);

	edgetpu_counter_clear(etdev, EDGETPU_COUNTER_HARDWARE_PREEMPTS);
	return count;
}
static DEVICE_ATTR(hardware_preempt_count, 0664, hardware_preempt_count_show,
		   hardware_preempt_count_store);

static ssize_t hardware_ctx_save_time_show(struct device *dev, struct device_attribute *attr,
					   char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	int64_t val;

	val = edgetpu_usage_get_counter(etdev, EDGETPU_COUNTER_HARDWARE_CTX_SAVE_TIME_US);
	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
}

static ssize_t hardware_ctx_save_time_store(struct device *dev, struct device_attribute *attr,
					    const char *buf, size_t count)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);

	edgetpu_counter_clear(etdev, EDGETPU_COUNTER_HARDWARE_CTX_SAVE_TIME_US);
	return count;
}
static DEVICE_ATTR(hardware_ctx_save_time, 0664, hardware_ctx_save_time_show,
		   hardware_ctx_save_time_store);

static ssize_t scalar_fence_wait_time_show(struct device *dev, struct device_attribute *attr,
					   char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	int64_t val;

	val = edgetpu_usage_get_counter(etdev, EDGETPU_COUNTER_SCALAR_FENCE_WAIT_TIME_US);
	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
}

static ssize_t scalar_fence_wait_time_store(struct device *dev, struct device_attribute *attr,
					    const char *buf, size_t count)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);

	edgetpu_counter_clear(etdev, EDGETPU_COUNTER_SCALAR_FENCE_WAIT_TIME_US);
	return count;
}
static DEVICE_ATTR(scalar_fence_wait_time, 0664, scalar_fence_wait_time_show,
		   scalar_fence_wait_time_store);

static ssize_t long_suspend_count_show(struct device *dev, struct device_attribute *attr,
					   char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	int64_t val;

	val = edgetpu_usage_get_counter(etdev, EDGETPU_COUNTER_LONG_SUSPEND);
	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
}

static ssize_t long_suspend_count_store(struct device *dev, struct device_attribute *attr,
					    const char *buf, size_t count)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);

	edgetpu_counter_clear(etdev, EDGETPU_COUNTER_LONG_SUSPEND);
	return count;
}
static DEVICE_ATTR(long_suspend_count, 0664, long_suspend_count_show,
		   long_suspend_count_store);

static ssize_t outstanding_commands_max_show(
	struct device *dev, struct device_attribute *attr, char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	int64_t val;

	val = edgetpu_usage_get_max_watermark(
			etdev, EDGETPU_MAX_WATERMARK_OUT_CMDS);
	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
}

static ssize_t outstanding_commands_max_store(
	struct device *dev, struct device_attribute *attr,
	const char *buf, size_t count)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;

	if (ustats) {
		mutex_lock(&ustats->usage_stats_lock);
		ustats->max_watermark[EDGETPU_MAX_WATERMARK_OUT_CMDS] = 0;
		mutex_unlock(&ustats->usage_stats_lock);
	}

	return count;
}
static DEVICE_ATTR(outstanding_commands_max, 0664,
		   outstanding_commands_max_show,
		   outstanding_commands_max_store);

static ssize_t preempt_depth_max_show(
	struct device *dev, struct device_attribute *attr, char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	int64_t val;

	val = edgetpu_usage_get_max_watermark(
			etdev, EDGETPU_MAX_WATERMARK_PREEMPT_DEPTH);
	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
}

static ssize_t preempt_depth_max_store(
	struct device *dev, struct device_attribute *attr,
	const char *buf, size_t count)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;

	if (ustats) {
		mutex_lock(&ustats->usage_stats_lock);
		ustats->max_watermark[EDGETPU_MAX_WATERMARK_PREEMPT_DEPTH] = 0;
		mutex_unlock(&ustats->usage_stats_lock);
	}

	return count;
}
static DEVICE_ATTR(preempt_depth_max, 0664, preempt_depth_max_show,
		   preempt_depth_max_store);

static ssize_t hardware_ctx_save_time_max_show(
	struct device *dev, struct device_attribute *attr, char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	int64_t val;

	val = edgetpu_usage_get_max_watermark(
			etdev, EDGETPU_MAX_WATERMARK_HARDWARE_CTX_SAVE_TIME_US);
	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
}

static ssize_t hardware_ctx_save_time_max_store(
	struct device *dev, struct device_attribute *attr,
	const char *buf, size_t count)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;

	if (ustats) {
		mutex_lock(&ustats->usage_stats_lock);
		ustats->max_watermark[EDGETPU_MAX_WATERMARK_HARDWARE_CTX_SAVE_TIME_US] = 0;
		mutex_unlock(&ustats->usage_stats_lock);
	}

	return count;
}
static DEVICE_ATTR(hardware_ctx_save_time_max, 0664, hardware_ctx_save_time_max_show,
		   hardware_ctx_save_time_max_store);

static ssize_t scalar_fence_wait_time_max_show(
	struct device *dev, struct device_attribute *attr, char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	int64_t val;

	val = edgetpu_usage_get_max_watermark(
			etdev, EDGETPU_MAX_WATERMARK_SCALAR_FENCE_WAIT_TIME_US);
	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
}

static ssize_t scalar_fence_wait_time_max_store(
	struct device *dev, struct device_attribute *attr,
	const char *buf, size_t count)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;

	if (ustats) {
		mutex_lock(&ustats->usage_stats_lock);
		ustats->max_watermark[EDGETPU_MAX_WATERMARK_SCALAR_FENCE_WAIT_TIME_US] = 0;
		mutex_unlock(&ustats->usage_stats_lock);
	}

	return count;
}
static DEVICE_ATTR(scalar_fence_wait_time_max, 0664, scalar_fence_wait_time_max_show,
		   scalar_fence_wait_time_max_store);

static ssize_t suspend_time_max_show(
	struct device *dev, struct device_attribute *attr, char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	int64_t val;

	val = edgetpu_usage_get_max_watermark(
			etdev, EDGETPU_MAX_WATERMARK_SUSPEND_TIME_US);
	return scnprintf(buf, PAGE_SIZE, "%llu\n", val);
}

static ssize_t suspend_time_max_store(
	struct device *dev, struct device_attribute *attr,
	const char *buf, size_t count)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;

	if (ustats) {
		mutex_lock(&ustats->usage_stats_lock);
		ustats->max_watermark[EDGETPU_MAX_WATERMARK_SUSPEND_TIME_US] = 0;
		mutex_unlock(&ustats->usage_stats_lock);
	}

	return count;
}
static DEVICE_ATTR(suspend_time_max, 0664, suspend_time_max_show,
		   suspend_time_max_store);

static ssize_t fw_thread_stats_show(
	struct device *dev, struct device_attribute *attr, char *buf)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;
	int i;
	ssize_t ret = 0;

	edgetpu_kci_update_usage(etdev);
	mutex_lock(&ustats->usage_stats_lock);

	for (i = 0; i < EDGETPU_FW_THREAD_COUNT; i++) {
		if (!ustats->thread_stack_max[i])
			continue;
		ret += scnprintf(buf + ret, PAGE_SIZE - ret,
				 "%u\t%u\n", i, ustats->thread_stack_max[i]);
		/* Not checking ret < PAGE_SIZE is intended. */
	}

	mutex_unlock(&ustats->usage_stats_lock);
	return ret;
}

static ssize_t fw_thread_stats_store(
	struct device *dev, struct device_attribute *attr,
	const char *buf, size_t count)
{
	struct edgetpu_dev *etdev = dev_get_drvdata(dev);
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;
	int i;

	mutex_lock(&ustats->usage_stats_lock);
	for (i = 0; i < EDGETPU_FW_THREAD_COUNT; i++)
		ustats->thread_stack_max[i] = 0;
	mutex_unlock(&ustats->usage_stats_lock);
	return count;
}
static DEVICE_ATTR(fw_thread_stats, 0664, fw_thread_stats_show,
		   fw_thread_stats_store);

static struct attribute *usage_stats_dev_attrs[] = {
	&dev_attr_tpu_usage.attr,
	&dev_attr_device_utilization.attr,
	&dev_attr_tpu_utilization.attr,
	&dev_attr_tpu_active_cycle_count.attr,
	&dev_attr_tpu_throttle_stall_count.attr,
	&dev_attr_inference_count.attr,
	&dev_attr_tpu_op_count.attr,
	&dev_attr_param_cache_hit_count.attr,
	&dev_attr_param_cache_miss_count.attr,
	&dev_attr_context_preempt_count.attr,
	&dev_attr_hardware_preempt_count.attr,
	&dev_attr_hardware_ctx_save_time.attr,
	&dev_attr_scalar_fence_wait_time.attr,
	&dev_attr_long_suspend_count.attr,
	&dev_attr_outstanding_commands_max.attr,
	&dev_attr_preempt_depth_max.attr,
	&dev_attr_hardware_ctx_save_time_max.attr,
	&dev_attr_scalar_fence_wait_time_max.attr,
	&dev_attr_suspend_time_max.attr,
	&dev_attr_fw_thread_stats.attr,
	NULL,
};

static const struct attribute_group usage_stats_attr_group = {
	.attrs = usage_stats_dev_attrs,
};
void edgetpu_usage_stats_init(struct edgetpu_dev *etdev)
{
	struct edgetpu_usage_stats *ustats;
	int ret;

	ustats = devm_kzalloc(etdev->dev, sizeof(*etdev->usage_stats),
			      GFP_KERNEL);
	if (!ustats) {
		etdev_warn(etdev,
			   "failed to allocate memory for usage stats\n");
		return;
	}

	hash_init(ustats->uid_hash_table);
	mutex_init(&ustats->usage_stats_lock);
	etdev->usage_stats = ustats;

	ret = device_add_group(etdev->dev, &usage_stats_attr_group);
	if (ret)
		etdev_warn(etdev, "failed to create the usage_stats attrs\n");

	etdev_dbg(etdev, "%s init\n", __func__);
}

void edgetpu_usage_stats_exit(struct edgetpu_dev *etdev)
{
	struct edgetpu_usage_stats *ustats = etdev->usage_stats;

	if (ustats) {
		usage_stats_remove_uids(ustats);
		device_remove_group(etdev->dev, &usage_stats_attr_group);
		/* free the frequency table if allocated */
		mutex_lock(&etdev->freq_lock);
		if (etdev->freq_table)
			kvfree(etdev->freq_table);
		etdev->freq_table = NULL;
		etdev->freq_count = 0;
		mutex_unlock(&etdev->freq_lock);
	}

	etdev_dbg(etdev, "%s exit\n", __func__);
}