summaryrefstree
path: root/kernel/drivers/vision/npu/soc/9830/npu-stm-soc.c
blob: 7ed723ea274baa9d8782c344c5f581a7bfe10122 (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
// SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
/*
 * Samsung Exynos SoC series NPU driver
 *
 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
 *              http://www.samsung.com/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <linux/delay.h>
#include <linux/stm.h>
#include <linux/iommu.h>
#include <linux/soc/samsung/exynos-soc.h>
#include <linux/atomic.h>
#include <linux/sched/clock.h>
#include <coresight-priv.h>

#include "npu-log.h"
#include "npu-config.h"
#include "npu-common.h"
#include "npu-device.h"
#include "npu-system.h"
#include "npu-util-regs.h"
#include "npu-stm-soc.h"
#include "npu-if-session-protodrv.h"

/* Constants for STM_SEL SFR for SYNC signal -> USD0[0:15] is available */
static const u32 STM_SEL_SYNC = 0x78;		/* [6:4] = 0b111 / [3:0] = 0b0001 */

/* SYNC definition */
static const u32 SYNC_EVENT_SEQ[] = {
	0xF, 0x0, 0xF, 0x1, 0xF, 0x2, 0xF, 0x3,		/* Preemble */
	0x9, 0x8, 0x3, 0x0,				/* SoC Code */
	0xF, 0xE};					/* Postfix */
static const u32 STM_SYNC_DELAY_US = 500;

/* Valid event table ID lists */
static const u32 VALID_STM_TABLE_ID[] = {
	0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60,	/* NPU sets */
	0x70, 0x71, 0x72, 0x74, 0x78			/* DSP sets */
};

/* TODO: Move to Kconfig */
#define CONFIG_NPU_FW_ACCESS_MCT
static const u32 EXYNOS9830_MCT_PHYS = 0x10040000;
static const u32 EXYNOS9830_MCT_IOVA = 0x50540000;
static const u32 EXYNOS9830_MCT_SIZE = 0x1000;

/* To pass system reference to link/unlink call back */
static int npu_stm_link(struct stm_source_data *data);
static void npu_stm_unlink(struct stm_source_data *data);

/* Value of npu_stm_data.enabled */
enum npu_stm_enabled {
	NPU_STM_DISABLED = 0,
	NPU_STM_ENABLED,
};

struct npu_stm_data {
	struct stm_source_data	stm_source_data;
	struct npu_system	*system;
	atomic_t		enabled;
	atomic_t		probed;
	atomic_t		guarantee;
	atomic_t		guarantee_working;
};

static struct npu_stm_data npu_stm_data = {
	.stm_source_data = {
		.nr_chans = 1,
		.name = "Exynos-NPU",
		.link = npu_stm_link,
		.unlink = npu_stm_unlink,
	},
	.system = NULL,
	.enabled = ATOMIC_INIT(NPU_STM_DISABLED),
	.probed = ATOMIC_INIT(0),
	.guarantee = ATOMIC_INIT(0),
	.guarantee_working = ATOMIC_INIT(0),
};

/**************************************************************************
 * STM timestamp synchronizer sysfs interface
 */
static const unsigned long OSC_CLK_DEFAULT = 26;
static const u32 TRACE_SET_MASK = 0xFF;

/* Definition for STM register offsets */
enum {
	STMHEER		= 0xD00,
	STMHETER	= 0xD20,
	STMHEBSR	= 0xD60,
	STMHEMCR	= 0xD64,
} stm_regs_e;

struct npu_stm_ts_stat {
	unsigned long long kmsg_time_ns;
	u64 stm_val;
	unsigned long stm_rate;
	u32 trace_set;
	struct npu_iomem_area *sfr_mct_g;
};
static struct npu_stm_ts_stat npu_stm_ts_stat = {
	.kmsg_time_ns = 0,
	.stm_val = 0,
	.stm_rate = OSC_CLK_DEFAULT,
	.trace_set = 0,
};

static u64 read_global_mct_64(void)
{
	unsigned int lo, hi, hi2;

	hi2 = npu_read_hw_reg(npu_stm_ts_stat.sfr_mct_g, 0x4);
	do {
		hi = hi2;
		lo = npu_read_hw_reg(npu_stm_ts_stat.sfr_mct_g, 0x0);
		hi2 = npu_read_hw_reg(npu_stm_ts_stat.sfr_mct_g, 0x4);
	} while (hi != hi2);

	return ((u64)hi << 32) | lo;
}

static u32 read_global_mct_32(void)
{
	return npu_read_hw_reg(npu_stm_ts_stat.sfr_mct_g, 0x0);
}

static const struct npu_profile_operation PROFILE_OPS = {
	.timestamp = read_global_mct_32,
};

static ssize_t stm_ts_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	int ret;
	unsigned long long kmsg_time_us;
	u64 stm_val_us;

	/* Calculate rounded value to micro seconds */
	kmsg_time_us = (npu_stm_ts_stat.kmsg_time_ns + 500) / 1000;
	stm_val_us =
		(npu_stm_ts_stat.stm_val + (npu_stm_ts_stat.stm_rate / 2))
		/ npu_stm_ts_stat.stm_rate;

	ret = scnprintf(buf, PAGE_SIZE,
		"STM timestamp status :\n"
		" - kmsg_time : %llu us\n"
		" - kmsg_raw_time : %llu\n"
		" - STM time : %llu us\n"
		" - STM raw_time : %llu\n"
		" - STM TS rate : %lu MHz\n"
		" - Trace set : %02x\n",
		kmsg_time_us, npu_stm_ts_stat.kmsg_time_ns,
		stm_val_us, npu_stm_ts_stat.stm_val,
		npu_stm_ts_stat.stm_rate,
		npu_stm_ts_stat.trace_set);

	return ret;
}

static ssize_t stm_ts_store(
	struct device *dev, struct device_attribute *attr,
	const char *buf, size_t len)
{
	unsigned long trace_set;
	int ret;
	size_t i;

	ret = kstrtoul(buf, 16, &trace_set);
	if (ret) {
		npu_err("Failed to parsing trace set. ret = %d\n", ret);
		return ret;
	}

	for (i = 0; i < ARRAY_SIZE(VALID_STM_TABLE_ID); i++) {
		if ((u32)trace_set == VALID_STM_TABLE_ID[i]) {
			npu_stm_ts_stat.trace_set = ((u32)trace_set & TRACE_SET_MASK);
			return len;
		}
	}

	/* No matching trace set */
	return -EINVAL;
}

/*
 * sysfs attribute for STM Timestamp stat
 *   name = dev_attr_stm_ts
 *   show = stm_ts_show
 *   store = stm_ts_store
 */
static DEVICE_ATTR_RW(stm_ts);

static ssize_t stm_guarantee_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	if (atomic_read(&npu_stm_data.guarantee))
		buf[0] = '1';
	else
		buf[0] = '0';

	buf[1] = '\n';

	return 2;
}

static ssize_t stm_guarantee_store(
	struct device *dev, struct device_attribute *attr,
	const char *buf, size_t len)
{
	if (len <= 0)
		return 0;

	switch (buf[0]) {
	case '0':
		atomic_set(&npu_stm_data.guarantee, 0);
		return len;
	case '1':
		atomic_set(&npu_stm_data.guarantee, 1);
		return len;
	default:
		return -EINVAL;
	}
}

/*
 * sysfs attribute for guaranteed STM
 *   name = dev_attr_stm_guarantee
 *   show = stm_guarantee_show
 *   store = stm_guarantee_store
 */
static DEVICE_ATTR_RW(stm_guarantee);

/* Register SYSFS entry and initialize MCT clock rate */
static int register_stm_ts_sysfs(struct npu_system *system)
{
	int ret = 0;
	struct clk *mct_clk;
	struct npu_device *device = container_of(system, struct npu_device, system);

	/* Create SYSFS entry for Guaranteed STM setting */
	ret = sysfs_create_file(&device->dev->kobj, &dev_attr_stm_guarantee.attr);
	if (ret) {
		probe_err("sysfs_create_file error : ret = %d\n", ret);
		goto err_exit;
	}

	/* Create SYSFS entry for STM Timestamp check */
	ret = sysfs_create_file(&device->dev->kobj, &dev_attr_stm_ts.attr);
	if (ret) {
		probe_err("sysfs_create_file error : ret = %d\n", ret);
		goto err_exit;
	}

	mct_clk = clk_get(NULL, "fin_pll");
	if (IS_ERR(mct_clk))
		npu_stm_ts_stat.stm_rate = OSC_CLK_DEFAULT;
	else
		npu_stm_ts_stat.stm_rate = clk_get_rate(mct_clk) / 1000000;

	npu_stm_ts_stat.sfr_mct_g = &system->sfr_mct_g;
err_exit:
	return ret;
}

/* Make sync point of MCT and kernel clock */
static void update_stm_ts_stat(void)
{
	/* Get timer value */
	npu_stm_ts_stat.stm_val = read_global_mct_64();
	npu_stm_ts_stat.kmsg_time_ns = sched_clock();
}

static u32 get_stm_trace_set(void)
{
	return npu_stm_ts_stat.trace_set;
}

/*********************************************************
 * STM managing functions
 */
static int __busp_div_1to1(struct npu_system *system)
{
	int ret = 0;
	const struct reg_set_map_2 busp_div_1to1_regs[] = {
		/* Addr			Offset	Val	Mask */
		{&system->sfr_npu[0],	0x1804,	0x0,	0x0000000F},	/* NPU00_CMU_CLK_CON_DIV_DIV_CLK_NPUC_BUSP */
		{&system->sfr_npu[1],	0x1800,	0x0,	0x0000000F},	/* NPU01_CMU_CLK_CON_DIV_DIV_CLK_NPU_BUSP */
		{&system->sfr_npu[2],	0x1804,	0x0,	0x0000000F},	/* NPU10_CMU_CLK_CON_DIV_DIV_CLK_NPUC_BUSP */
		{&system->sfr_npu[3],	0x1800,	0x0,	0x0000000F},	/* NPU11_CMU_CLK_CON_DIV_DIV_CLK_NPU_BUSP */
	};

	ret = npu_set_hw_reg_2(busp_div_1to1_regs, ARRAY_SIZE(busp_div_1to1_regs), 0);
	if (ret) {
		npu_err("Failed to write registers on busp_div_1to1_regs array (%d)\n", ret);
		goto err_exit;
	}

	npu_info("BUSP divider is set to 1:1. Guaranteed STM available.\n");
err_exit:
	return ret;
}

static int __busp_div_1to4(struct npu_system *system)
{
	int ret = 0;
	const struct reg_set_map_2 busp_div_1to4_regs[] = {
		/* Addr			Offset	Val	Mask */
		{&system->sfr_npu[0],	0x1804,	0x3,	0x0000000F},	/* NPU00_CMU_CLK_CON_DIV_DIV_CLK_NPUC_BUSP */
		{&system->sfr_npu[1],	0x1800,	0x3,	0x0000000F},	/* NPU01_CMU_CLK_CON_DIV_DIV_CLK_NPU_BUSP */
		{&system->sfr_npu[2],	0x1804,	0x3,	0x0000000F},	/* NPU10_CMU_CLK_CON_DIV_DIV_CLK_NPUC_BUSP */
		{&system->sfr_npu[3],	0x1800,	0x3,	0x0000000F},	/* NPU11_CMU_CLK_CON_DIV_DIV_CLK_NPU_BUSP */
	};

	ret = npu_set_hw_reg_2(busp_div_1to4_regs, ARRAY_SIZE(busp_div_1to4_regs), 0);
	if (ret) {
		npu_err("Failed to write registers on busp_div_1to4_regs array (%d)\n", ret);
		goto err_exit;
	}

	npu_info("BUSP divider is restored to 1:4. Guaranteed STM no longer available.\n");
err_exit:
	return ret;
}

static int __enable_npu_stm_sfr(struct npu_system *system, u32 stm_sel)
{
	int ret = 0;
	/*const u32 DBG_ATCLK = 800 * 1000 * 1000;	ATCLK = 800MHz */

	const struct reg_set_map_2 enable_npu_stm_regs[] = {
		/* Addr				Offset	Val		Mask */
		{&system->sfr_dnc,		0x0078,	stm_sel,	0x0000007F},	/* STM_SEL[6:4][3:0] */
		{&system->sfr_dnc,		0x0074,	0x1,		0x00000001},	/* STM_ENABLE(DNC) */
		{&system->sfr_npuc[0],		0xF0418,	0x1,	0x00000001},	/* STM_ENABLE(NPUC0) */
		{&system->sfr_npuc[1],		0xF0418,	0x1,	0x00000001},	/* STM_ENABLE(NPUC1) */
		{&system->sfr_coresight,	0x0034,	0x1,		0x00000007},	/* Route NPU to STM */
		{&system->sfr_coresight,	0x002C,	0x1,		0x00000001},	/* Coresight clock enable */
	};

	BUG_ON(!system->sfr_coresight.vaddr);

	ret = npu_set_hw_reg_2(enable_npu_stm_regs, ARRAY_SIZE(enable_npu_stm_regs), 0);
	if (ret) {
		npu_err("Failed to write registers on enable_npu_stm_regs array (%d)\n", ret);
		goto err_exit;
	}

	npu_info("STM enabled with STM_SEL = 0x%08x\n", stm_sel);
err_exit:
	return ret;
}

static int __disable_npu_stm_sfr(struct npu_system *system)
{
	int ret = 0;
	const struct reg_set_map_2 disable_npu_stm_regs[] = {
		/* Addr				Offset		Val	Mask */
		{&system->sfr_coresight,	0x0034,		0x0,	0x00000007},	/* Route NPU to STM */
		{&system->sfr_dnc,		0x0078,		0x00,	0x0000007F},	/* STM_SEL[6:4][3:0] */
		{&system->sfr_dnc,		0x0074,		0x0,	0x00000001},	/* STM_ENABLE(DNC) */
		{&system->sfr_npuc[0],		0xF0418,	0x0,	0x00000001},	/* STM_ENABLE(NPUC0) */
		{&system->sfr_npuc[1],		0xF0418,	0x0,	0x00000001},	/* STM_ENABLE(NPUC1) */
	};

	ret = npu_set_hw_reg_2(disable_npu_stm_regs, ARRAY_SIZE(disable_npu_stm_regs), 0);
	if (ret) {
		npu_err("Failed to write registers on disable_npu_stm_regs array (%d)\n", ret);
		goto err_exit;
	}

	npu_info("STM disabled\n");
err_exit:
	return ret;
}

/*
 * Allow all 64 HW events generated in NPU pass through the CoreSight path.
 * Current CoreSight driver implementation only allows either 0~31th or 32~63th
 * can be traced, but not both, while the CoreSight hardware can trace
 * 64 events at once. This function manually overrides STMHEBSR, STMHETER and STMHEER
 * to make the events traced.
 */
static int npu_stm_allow_64_trace(struct npu_system *system)
{
	int	ret = 0;
	u32	old_hemcr;	/* Backup of current STMHEMCR value */
	struct reg_set_map_2 allow64_npu_stm_regs[] = {
		/* Addr			Offset	Val		Mask */
		{&system->sfr_stm,	STMHEBSR,	0x0,		0x00000001},	/* Bank 0 / event 0~31 */
		{&system->sfr_stm,	STMHETER,	0xFFFFFFFF,	0xFFFFFFFF},	/* Enable all */
		{&system->sfr_stm,	STMHEER,	0xFFFFFFFF,	0xFFFFFFFF},	/* Enable all */
		{&system->sfr_stm,	STMHEBSR,	0x1,		0x00000001},	/* Bank 1 / event 32~63 */
		{&system->sfr_stm,	STMHETER,	0xFFFFFFFF,	0xFFFFFFFF},	/* Enable all */
		{&system->sfr_stm,	STMHEER,	0xFFFFFFFF,	0xFFFFFFFF},	/* Enable all */
	};

	BUG_ON(!system->sfr_stm.vaddr);

	/* Unlock STM register for writing */
	CS_UNLOCK(system->sfr_stm.vaddr);
	old_hemcr = npu_read_hw_reg(&system->sfr_stm, STMHEMCR);

	/* Stop STM before set trace configuration : Settings are in effect after restarting */
	npu_write_hw_reg(&system->sfr_stm, STMHEMCR, 0x0, 0xFFFFFFFF);

	/* Enable events both bank 0 and 1 */
	ret = npu_set_hw_reg_2(allow64_npu_stm_regs, ARRAY_SIZE(allow64_npu_stm_regs), 0);
	if (ret) {
		npu_err("Failed to write registers on probe_npu_stm_regs array (%d)\n", ret);
		goto err_exit;
	}

	/* Restore STMHEMCR */
	npu_write_hw_reg(&system->sfr_stm, STMHEMCR, old_hemcr, 0xFFFFFFFF);

	npu_dbg("64 STM events are enabled\n");
err_exit:
	/* Lock the STM SFR again */
	CS_LOCK(system->sfr_stm.vaddr);
	return ret;
}

/*
 * Write SYNC_EVENT_SEQ to lower 16b part of STM_SFR0 with STM_SYNC_DELAY_US.
 * This function will be a match point when synching timestamp of
 * STM and ftrace
 */
static void stm_sync_signal(struct npu_system *system)
{
	size_t i;
	u32 val;

	for (i = 0; i < ARRAY_SIZE(SYNC_EVENT_SEQ); i++) {
		val = (1 << SYNC_EVENT_SEQ[i]);
		udelay(STM_SYNC_DELAY_US);
		npu_write_hw_reg(&system->sfr_dnc, 0x0080, val, 0xFFFF);
	}
}

/*
 * Make Sync HW signal
 * STM source of DNC will be switched to DSP #1 temporary, to enable host-triggerd signal.
 */

static int __stm_sync(struct npu_system *system)
{
	u32	ret = 0;

	/* Enable STM with DSP #1 setting */
	ret = __enable_npu_stm_sfr(system, STM_SEL_SYNC);
	if (ret) {
		npu_err("__enable_npu_stm_sfr failed : %d\n", ret);
		goto err_exit;
	}

	/* Post 8 STM trigger, with 100us delay */
	npu_info("Start STM sync.\n");
	stm_sync_signal(system);

	/* Update timestamp */
	update_stm_ts_stat();

	/* Disable STM */
	ret = __disable_npu_stm_sfr(system);
	if (ret) {
		npu_err("__disable_npu_stm_sfr failed : %d\n", ret);
		goto err_exit;
	}

err_exit:
	return ret;
}

/*********************************************************
 * STM Link / Unlink handlers
 */
/* Helper function to invoke npu_stm_enable or npu_stm_disable if NPU is currently running */
static int npu_stm_enable_if_running(struct npu_system *system)
{
	if (npu_if_session_protodrv_is_opened())
		return npu_stm_enable(system);

	npu_info("STM enabling will be deferred until the block power enabled.\n");
	return 0;
}

static int npu_stm_disable_if_running(struct npu_system *system)
{
	if (npu_if_session_protodrv_is_opened())
		return npu_stm_disable(system);

	npu_info("STM disabling is not necessary when block power is off.\n");
	return 0;
}


static int npu_stm_link(struct stm_source_data *data)
{
	int ret = 0;

	struct npu_stm_data *npu_stm
		= container_of(data, struct npu_stm_data, stm_source_data);
	npu_info("%s called : data = %p, system = %p\n", __func__, data, npu_stm->system);

	/* Enable full STM events */
	ret = npu_stm_allow_64_trace(npu_stm->system);
	if (ret) {
		npu_err("npu_stm_allow_64_trace failed : %d\n", ret);
		goto err_exit;
	}

	/* Invoke enable_npu_stm if the state is changed */
	if (atomic_cmpxchg(&npu_stm->enabled, NPU_STM_DISABLED, NPU_STM_ENABLED)
		== NPU_STM_DISABLED) {

		npu_info("Do enable_npu_stm\n");
		if (likely(npu_stm->system)) {
			ret = npu_stm_enable_if_running(npu_stm->system);
			if (unlikely(ret)) {
				npu_err("npu_stm_enable_if_running() error : %d\n", ret);
				goto err_exit;
			}
		}
	}

err_exit:
	return ret;
}

static void npu_stm_unlink(struct stm_source_data *data)
{
	int ret = 0;

	struct npu_stm_data *npu_stm
		= container_of(data, struct npu_stm_data, stm_source_data);
	npu_info("%s called : data = %p, system = %p\n", __func__, data, npu_stm->system);

	/* Invoke disable_npu_stm if the state is changed */
	if (atomic_cmpxchg(&npu_stm->enabled, NPU_STM_ENABLED, NPU_STM_DISABLED)
		== NPU_STM_ENABLED) {

		npu_info("Do disable_npu_stm\n");
		if (likely(npu_stm->system)) {
			ret = npu_stm_disable_if_running(npu_stm->system);
			if (unlikely(ret))
				npu_err("npu_stm_disable() error : %d\n", ret);
		}
	}
}

/*********************************************************
 * Exported functions
 */
int npu_stm_enable(struct npu_system *system)
{
	int ret = 0;

	BUG_ON(!system);

	if (atomic_read(&npu_stm_data.enabled) != NPU_STM_ENABLED) {
		/* No need to be enabled */
		return ret;
	}

	/* Adjust BUSP divider if guaranteed STM is activated */
	if (atomic_read(&npu_stm_data.guarantee)) {
		ret = __busp_div_1to1(system);
		if (ret) {
			npu_err("__busp_div_1to1 failed : %d\n", ret);
			goto err_exit;
		}
		atomic_set(&npu_stm_data.guarantee_working, 1);
	}

	/* Send sync signal */
	ret = __stm_sync(system);
	if (ret) {
		npu_err("__stm_sync failed : %d\n", ret);
		goto err_exit;
	}

	/* Enable STM with trace set defined in SYSFS */
	ret = __enable_npu_stm_sfr(system, get_stm_trace_set());
	if (ret) {
		npu_err("__enable_npu_stm_sfr failed : %d\n", ret);
		goto err_exit;
	}
	npu_info("complete %s\n", __func__);

err_exit:
	return ret;
}

int npu_stm_disable(struct npu_system *system)
{
	int ret = 0;

	BUG_ON(!system);

	/*
	 * Disabling will not check npu_stm->enabled flag,
	 * to make sure the STM disabled before shutdown.
	 * But please keep in mind that the npu_stm_disable() can be
	 * more than one time when stm_unlink() is called while the
	 * NPU is turned on.
	 */
	ret = __disable_npu_stm_sfr(system);
	if (ret) {
		npu_err("__disable_npu_stm_sfr failed : %d\n", ret);
		goto err_exit;
	}

	/* Restores BUSP divider if guarantee flag was asserted */
	if (atomic_cmpxchg(&npu_stm_data.guarantee_working, 1, 0) == 1) {
		ret = __busp_div_1to4(system);
		if (ret) {
			npu_err("__busp_div_1to4 failed : %d\n", ret);
			goto err_exit;
		}
	}

	npu_info("complete %s\n", __func__);
err_exit:
	return ret;
}

#ifdef CONFIG_NPU_FW_ACCESS_MCT
static int enable_mct_fw_access(struct npu_system *system)
{
	struct iommu_domain	*domain;
	int			ret = 0;

	if (!system->pdev)
		return 0;

	ret = npu_profile_set_operation(&PROFILE_OPS);
	if (ret) {
		probe_err("fail(%d) in npu_profile_set_operation()\n", ret);
		goto err_exit;
	}

	/* Create a mapping for MCT to let the firmware access to it */
	BUG_ON(!system->pdev);
	domain = iommu_get_domain_for_dev(&(system->pdev->dev));
	if (IS_ERR_OR_NULL(domain)) {
		probe_err("Fail(%d) in iommu_get_domain_for_dev()\n", ret);
		goto err_exit;
	}

	ret = iommu_map(
		domain, EXYNOS9830_MCT_IOVA, EXYNOS9830_MCT_PHYS,
		EXYNOS9830_MCT_SIZE, IOMMU_READ);
	if (ret) {
		probe_err("fail(%d) in iommu_map\n", ret);
		goto err_exit;
	}

err_exit:
	return ret;
}

static int disable_mct_fw_access(struct npu_system *system)
{
	struct iommu_domain	*domain;

	if (system->pdev) {
		domain = iommu_get_domain_for_dev(&(system->pdev->dev));
		iommu_unmap(domain, EXYNOS9830_MCT_IOVA, EXYNOS9830_MCT_SIZE);
	}

	return 0;
}
#else	/* !CONFIG_NPU_FW_ACCESS_MCT */
#define enable_mct_fw_access(s)		(0)
#define disable_mct_fw_access(s)	(0)
#endif

/* Probe and Release called from npu_system_soc_probe() and npu_system_soc_release() */
int npu_stm_probe(struct npu_system *system)
{
	int			ret = 0;

	npu_stm_data.system = system;
	ret = stm_source_register_device(NULL, &npu_stm_data.stm_source_data);
	if (ret) {
		probe_err("fail(%d) in stm_source_register_device()\n", ret);
		goto err_exit;
	}

	ret = register_stm_ts_sysfs(system);
	if (ret) {
		probe_err("fail(%d) in register_stm_ts_sysfs()\n", ret);
		goto err_exit_stm_register;
	}

	ret = enable_mct_fw_access(system);
	if (ret) {
		probe_err("fail(%d) in enable_mct_fw_access()\n", ret);
		goto err_exit_stm_register;
	}

	probe_info("%s completed\n", __func__);

	atomic_set(&npu_stm_data.probed, 1);

	return ret;

err_exit_stm_register:
	stm_source_unregister_device(&npu_stm_data.stm_source_data);
err_exit:

	return ret;
}

int npu_stm_release(struct npu_system *system)
{
	int			ret = 0;

	/* Check whether the initialization is completed */
	if (!atomic_read(&npu_stm_data.probed))
		return ret;

	stm_source_unregister_device(&npu_stm_data.stm_source_data);

	ret = disable_mct_fw_access(system);
	if (ret) {
		probe_err("fail(%d) in disable_mct_fw_access()\n", ret);
		goto err_exit;
	}

	atomic_set(&npu_stm_data.probed, 0);
	return ret;
err_exit:
	return ret;
}