Skip to contentSkip to conversation

perf/imx_ddr: speed up overflow frequency of cycle counter

PATCH In review Xu Yang 2 participants 9 messages Jul 5, 2023 – Jul 6, 2023

Subsystems (3)
lore
Share & tools
lore

Xu Yang

author maintainer
#0 lore raw

PATCH 1/3 perf/imx_ddr: speed up overflow frequency of cycle counter

For i.MX8MP, we cannot ensure that cycle counter overflow occurs at least 4 times as often as other events. Due to byte counters will count for any event configured, it will overflow more often. And if byte counters overflow that related counters would stop since they share the COUNTER_CNTL. We can speed up cycle counter overflow frequency by setting counter parameter (CP) field of cycle counter. In this way, we can avoid stop counting byte counters when interrupt didn't come and the byte counters can be fetched or updated from each cycle counter overflow interrupt.

---
 drivers/perf/fsl_imx8_ddr_perf.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
drivers/perf/fsl_imx8_ddr_perf.c +16 -0
index 5222ba1e79d0..cc7693f49950 100644
@@ -28,6 +28,8 @@
 #define CNTL_CLEAR_MASK		0xFFFFFFFD
 #define CNTL_OVER_MASK		0xFFFFFFFE
 
+#define CNTL_CP_SHIFT		16
+#define CNTL_CP_MASK		(0xFF << CNTL_CP_SHIFT)
 #define CNTL_CSV_SHIFT		24
 #define CNTL_CSV_MASK		(0xFFU << CNTL_CSV_SHIFT)
 
@@ -427,6 +429,20 @@ static void ddr_perf_counter_enable(struct ddr_pmu *pmu, int config,
 		writel(0, pmu->base + reg);
 		val = CNTL_EN | CNTL_CLEAR;
 		val |= FIELD_PREP(CNTL_CSV_MASK, config);
+
+		/*
+		 * Workaround for i.MX8MP:
+		 * Common counters and byte counters share the same COUNTER_CNTL,
+		 * and byte counters could overflow before cycle counter. Need set
+		 * counter parameter(CP) of cycle counter to give it initial value
+		 * which can speed up cycle counter overflow frequency.
+		 */
+		if ((pmu->devtype_data->quirks & DDR_CAP_AXI_ID_FILTER_ENHANCED) ==
+		    DDR_CAP_AXI_ID_FILTER_ENHANCED) {
+			if (counter == EVENT_CYCLES_COUNTER)
+				val |= FIELD_PREP(CNTL_CP_MASK, 0xf0);
+		}
+
 		writel(val, pmu->base + reg);
 	} else {
 		/* Disable counter */
Signature · 7 lines
2.34.1


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
author maintainer
#1 lore raw

PATCH 2/3 perf/imx_ddr: adjust counter result after read cycle counter

Because we initialize CP filed to shorten counter0 overflow time, the cycle counter will start couting from a fixed/base value each time. We need to remove the base from the result too. Therefore, we could get precise result from cycle counter.

---
 drivers/perf/fsl_imx8_ddr_perf.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/perf/fsl_imx8_ddr_perf.c +7 -0
index cc7693f49950..4e6cbfc48429 100644
@@ -482,6 +482,13 @@ static void ddr_perf_event_update(struct perf_event *event)
 	int ret;
 
 	new_raw_count = ddr_perf_read_counter(pmu, counter);
+	/* Workaround for i.MXMP */
+	if ((pmu->devtype_data->quirks & DDR_CAP_AXI_ID_FILTER_ENHANCED) ==
+		DDR_CAP_AXI_ID_FILTER_ENHANCED) {
+		if (counter == EVENT_CYCLES_COUNTER)
+			new_raw_count -= 0xF0000000;
+	}
+
 	local64_add(new_raw_count, &event->count);
 
 	/*
Signature · 7 lines
2.34.1


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer
#2 lore raw

RE: [PATCH 2/3] perf/imx_ddr: adjust counter result after read cycle counter

Quoted 36 lines

-----Original Message-----
From: Xu Yang <[email protected]>
Sent: Wednesday, July 5, 2023 5:48 AM
To: Frank Li <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; dl-linux-imx <linux- [email protected]>; [email protected]; Xu Yang <[email protected]>
Subject: [PATCH 2/3] perf/imx_ddr: adjust counter result after read cycle counter

Because we initialize CP filed to shorten counter0 overflow time, the cycle counter will start couting from a fixed/base value each time. We need to remove the base from the result too. Therefore, we could get precise result from cycle counter.

---
 drivers/perf/fsl_imx8_ddr_perf.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/perf/fsl_imx8_ddr_perf.c +2 -0
index cc7693f49950..4e6cbfc48429 100644
@@ -482,6 +482,13 @@ static void ddr_perf_event_update(struct
 perf_event *event)
 	int ret;
 
 	new_raw_count = ddr_perf_read_counter(pmu, counter);
+	/* Workaround for i.MXMP */
+	if ((pmu->devtype_data->quirks &

DDR_CAP_AXI_ID_FILTER_ENHANCED) ==

drivers/perf/fsl_imx8_ddr_perf.c static void ddr_perf_event_update(struct perf_event *event) · line 487

Quoted 1 line
drivers/perf/fsl_imx8_ddr_perf.c +1 -0
+		DDR_CAP_AXI_ID_FILTER_ENHANCED) {

Supposed below check should be enough.

if (pmu->devtype_data->quirks & DDR_CAP_AXI_ID_FILTER_ENHANCED)

Quoted 9 lines
drivers/perf/fsl_imx8_ddr_perf.c +4 -1
+		if (counter == EVENT_CYCLES_COUNTER)
+			new_raw_count -= 0xF0000000;
+	}
+
 	local64_add(new_raw_count, &event->count);
 
 	/*
--

2.34.1

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

author maintainer
#3 lore raw

RE: [PATCH 2/3] perf/imx_ddr: adjust counter result after read cycle counter

Hi Frank,

Quoted 40 lines

-----Original Message-----
From: Frank Li <[email protected]>
Sent: Wednesday, July 5, 2023 10:53 PM
To: Xu Yang <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; dl-linux-imx <[email protected]>; [email protected]
Subject: RE: [PATCH 2/3] perf/imx_ddr: adjust counter result after read cycle counter

Quoted 30 lines

-----Original Message-----
From: Xu Yang <[email protected]>
Sent: Wednesday, July 5, 2023 5:48 AM
To: Frank Li <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; dl-linux-imx <linux- [email protected]>; [email protected]; Xu Yang <[email protected]>
Subject: [PATCH 2/3] perf/imx_ddr: adjust counter result after read cycle counter

Because we initialize CP filed to shorten counter0 overflow time, the cycle counter will start couting from a fixed/base value each time. We need to remove the base from the result too. Therefore, we could get precise result from cycle counter.

---
 drivers/perf/fsl_imx8_ddr_perf.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/perf/fsl_imx8_ddr_perf.c +0 -0
index cc7693f49950..4e6cbfc48429 100644
@@ -482,6 +482,13 @@ static void ddr_perf_event_update(struct
 perf_event *event)
 	int ret;
 
Quoted 7 lines

drivers/perf/fsl_imx8_ddr_perf.c static void ddr_perf_event_update(struct perf_event *event) · line 487

Quoted 5 lines
drivers/perf/fsl_imx8_ddr_perf.c +2 -0
 	new_raw_count = ddr_perf_read_counter(pmu, counter);
+	/* Workaround for i.MXMP */
+	if ((pmu->devtype_data->quirks &

DDR_CAP_AXI_ID_FILTER_ENHANCED) ==

drivers/perf/fsl_imx8_ddr_perf.c +1 -0
+		DDR_CAP_AXI_ID_FILTER_ENHANCED) {

Supposed below check should be enough.

No, this workaround is only for imx8mp. We need this condition to filter it. For other platforms, we'd better let cycle counter start counting from 0.

Thanks,
Xu Yang

Quoted 9 lines

if (pmu->devtype_data->quirks & DDR_CAP_AXI_ID_FILTER_ENHANCED)

Quoted 6 lines
drivers/perf/fsl_imx8_ddr_perf.c +4 -0
+		if (counter == EVENT_CYCLES_COUNTER)
+			new_raw_count -= 0xF0000000;
+	}
+
 	local64_add(new_raw_count, &event->count);
Quoted 3 lines
Quoted 3 lines

/*
--
2.34.1

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

author maintainer
#4 lore raw

PATCH 3/3 perf/imx_ddr: don't enable counter0 if none of 4 counters are used

In reply to Xu Yang

In current driver, counter0 will be enabled after ddr_perf_pmu_enable() is called even though none of the 4 counters are used. This will cause counter0 continue to count until ddr_perf_pmu_disabled() is called. If pmu is not disabled all the time, the pmu interrupt will be asserted from time to time due to counter0 will overflow and irq handler will clear it. It's not an expected behavior. This patch will not enable counter0 if none of 4 counters are used.

Fixes 9a66d36cc7ac ("drivers/perf: imx_ddr: Add DDR performance counter support to perf")
---
 drivers/perf/fsl_imx8_ddr_perf.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/perf/fsl_imx8_ddr_perf.c +6 -2
index 4e6cbfc48429..f7c832611713 100644
@@ -591,7 +591,9 @@ static void ddr_perf_pmu_enable(struct pmu *pmu)
 	struct ddr_pmu *ddr_pmu = to_ddr_pmu(pmu);
 
 	/* enable cycle counter if cycle is not active event list */
-	if (ddr_pmu->events[EVENT_CYCLES_COUNTER] == NULL)
+	if (ddr_pmu->events[EVENT_CYCLES_COUNTER] == NULL &&
+	   (ddr_pmu->events[0] || ddr_pmu->events[1] ||
+	    ddr_pmu->events[2] || ddr_pmu->events[3]))
 		ddr_perf_counter_enable(ddr_pmu,
 				      EVENT_CYCLES_ID,
 				      EVENT_CYCLES_COUNTER,
@@ -602,7 +604,9 @@ static void ddr_perf_pmu_disable(struct pmu *pmu)
 {
 	struct ddr_pmu *ddr_pmu = to_ddr_pmu(pmu);
 
-	if (ddr_pmu->events[EVENT_CYCLES_COUNTER] == NULL)
+	if (ddr_pmu->events[EVENT_CYCLES_COUNTER] == NULL &&
+	   (ddr_pmu->events[0] || ddr_pmu->events[1] ||
+	    ddr_pmu->events[2] || ddr_pmu->events[3]))
 		ddr_perf_counter_enable(ddr_pmu,
 				      EVENT_CYCLES_ID,
 				      EVENT_CYCLES_COUNTER,
Signature · 7 lines
2.34.1


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer
#5 lore raw

RE: [PATCH 3/3] perf/imx_ddr: don't enable counter0 if none of 4 counters are used

drivers/perf/fsl_imx8_ddr_perf.c static void ddr_perf_pmu_enable(struct pmu *pmu) · line 594

Quoted 12 lines
drivers/perf/fsl_imx8_ddr_perf.c +1 -1
index 4e6cbfc48429..f7c832611713 100644
@@ -591,7 +591,9 @@ static void ddr_perf_pmu_enable(struct pmu *pmu)
 	struct ddr_pmu *ddr_pmu = to_ddr_pmu(pmu);
 
 	/* enable cycle counter if cycle is not active event list */
-	if (ddr_pmu->events[EVENT_CYCLES_COUNTER] == NULL)
+	if (ddr_pmu->events[EVENT_CYCLES_COUNTER] == NULL &&

EVENT_CYCLES_COUNTER is 0, if only enable cycles counter, below logic
Will failure. Then counter will not work.

drivers/perf/fsl_imx8_ddr_perf.c static void ddr_perf_pmu_enable(struct pmu *pmu) · line 596

Quoted 2 lines
drivers/perf/fsl_imx8_ddr_perf.c +2 -0
+	   (ddr_pmu->events[0] || ddr_pmu->events[1] ||
+	    ddr_pmu->events[2] || ddr_pmu->events[3]))

I suggest maintain a enabled_event counter,

	If(ddr_pmu->active_num==0)
		ddr_perf_counter_enable(..., true);
	ddr_pmu->active_num++;

At disable function()

{
       ddr_pmu->active_num --;
       if( ddr_pmu->active_num == 0)
	ddr_perf_counter_enable(.., false);
}
Quoted 15 lines
drivers/perf/fsl_imx8_ddr_perf.c +3 -2
 		ddr_perf_counter_enable(ddr_pmu,
 				      EVENT_CYCLES_ID,
 				      EVENT_CYCLES_COUNTER,
@@ -602,7 +604,9 @@ static void ddr_perf_pmu_disable(struct pmu *pmu)
 {
 	struct ddr_pmu *ddr_pmu = to_ddr_pmu(pmu);
 
-	if (ddr_pmu->events[EVENT_CYCLES_COUNTER] == NULL)
+	if (ddr_pmu->events[EVENT_CYCLES_COUNTER] == NULL &&
+	   (ddr_pmu->events[0] || ddr_pmu->events[1] ||
+	    ddr_pmu->events[2] || ddr_pmu->events[3]))
 		ddr_perf_counter_enable(ddr_pmu,
 				      EVENT_CYCLES_ID,
 				      EVENT_CYCLES_COUNTER,
--
Quoted 1 line

2.34.1

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

author maintainer
#6 lore raw

RE: [PATCH 3/3] perf/imx_ddr: don't enable counter0 if none of 4 counters are used

Hi Frank,

Quoted 17 lines

-----Original Message-----
From: Frank Li <[email protected]>
Sent: Wednesday, July 5, 2023 10:51 PM
To: Xu Yang <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; dl-linux-imx <[email protected]>; [email protected]
Subject: RE: [PATCH 3/3] perf/imx_ddr: don't enable counter0 if none of 4 counters are used

Quoted 9 lines
drivers/perf/fsl_imx8_ddr_perf.c +0 -0
index 4e6cbfc48429..f7c832611713 100644
@@ -591,7 +591,9 @@ static void ddr_perf_pmu_enable(struct pmu *pmu)
 	struct ddr_pmu *ddr_pmu = to_ddr_pmu(pmu);
 
Quoted 6 lines

drivers/perf/fsl_imx8_ddr_perf.c static void ddr_perf_pmu_enable(struct pmu *pmu) · line 594

Quoted 3 lines
drivers/perf/fsl_imx8_ddr_perf.c +1 -1
 	/* enable cycle counter if cycle is not active event list */
-	if (ddr_pmu->events[EVENT_CYCLES_COUNTER] == NULL)
+	if (ddr_pmu->events[EVENT_CYCLES_COUNTER] == NULL &&

EVENT_CYCLES_COUNTER is 0, if only enable cycles counter, below logic
Will failure. Then counter will not work.

Yes, cycle counter will not be enabled for counting at this point. However, cycle counter will be enabled after perf_pmu_enable() is called. My understanding is that ddr_perf_pmu_enable() is used to enable cycle counter when other counters is used for counting since other counters depends on cycle counter.

Quoted 10 lines

drivers/perf/fsl_imx8_ddr_perf.c static void ddr_perf_pmu_enable(struct pmu *pmu) · line 596

Quoted 3 lines
drivers/perf/fsl_imx8_ddr_perf.c +2 -0
+	   (ddr_pmu->events[0] || ddr_pmu->events[1] ||
+	    ddr_pmu->events[2] || ddr_pmu->events[3]))

I suggest maintain a enabled_event counter,

	If(ddr_pmu->active_num==0)
		ddr_perf_counter_enable(..., true);
	ddr_pmu->active_num++;
Quoted 6 lines

At disable function()

{
       ddr_pmu->active_num --;
       if( ddr_pmu->active_num == 0)
	ddr_perf_counter_enable(.., false);
}

Will try to maintain a enabled_event counter.

Thanks,
Xu Yang

Quoted 9 lines
Quoted 9 lines
drivers/perf/fsl_imx8_ddr_perf.c +0 -0
 		ddr_perf_counter_enable(ddr_pmu,
 				      EVENT_CYCLES_ID,
 				      EVENT_CYCLES_COUNTER,
@@ -602,7 +604,9 @@ static void ddr_perf_pmu_disable(struct pmu *pmu)
 {
 	struct ddr_pmu *ddr_pmu = to_ddr_pmu(pmu);
 
Quoted 9 lines
Quoted 9 lines
drivers/perf/fsl_imx8_ddr_perf.c +3 -2
-	if (ddr_pmu->events[EVENT_CYCLES_COUNTER] == NULL)
+	if (ddr_pmu->events[EVENT_CYCLES_COUNTER] == NULL &&
+	   (ddr_pmu->events[0] || ddr_pmu->events[1] ||
+	    ddr_pmu->events[2] || ddr_pmu->events[3]))
 		ddr_perf_counter_enable(ddr_pmu,
 				      EVENT_CYCLES_ID,
 				      EVENT_CYCLES_COUNTER,
--

2.34.1

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

maintainer
#7 lore raw
In reply to Xu Yang
Quoted 13 lines
drivers/perf/fsl_imx8_ddr_perf.c +4 -0
+
+		/*
+		 * Workaround for i.MX8MP:
+		 * Common counters and byte counters share the same

COUNTER_CNTL,

(quoted diff) +1 -0
+		 * and byte counters could overflow before cycle counter.

Need set

(quoted diff) +1 -0
+		 * counter parameter(CP) of cycle counter to give it initial

value

drivers/perf/fsl_imx8_ddr_perf.c +3 -0
+		 * which can speed up cycle counter overflow frequency.
+		 */
+		if ((pmu->devtype_data->quirks &

DDR_CAP_AXI_ID_FILTER_ENHANCED) ==

drivers/perf/fsl_imx8_ddr_perf.c static void ddr_perf_counter_enable(struct ddr_pmu *pmu, int config, · line 441

Quoted 1 line
drivers/perf/fsl_imx8_ddr_perf.c +1 -0
+		    DDR_CAP_AXI_ID_FILTER_ENHANCED) {

needn't ==
if (pmu->devtype_data->quirks & DDR_CAP_AXI_ID_FILTER_ENHANCED)

Did you fixed cycle counter also? Dose cycle counter read get correct value?

Frank

Quoted 9 lines
drivers/perf/fsl_imx8_ddr_perf.c +4 -1
+			if (counter == EVENT_CYCLES_COUNTER)
+				val |= FIELD_PREP(CNTL_CP_MASK, 0xf0);
+		}
+
 		writel(val, pmu->base + reg);
 	} else {
 		/* Disable counter */
--

2.34.1

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

author maintainer
#8 lore raw

Hi Frank,

Quoted 17 lines

-----Original Message-----
From: Frank Li <[email protected]>
Sent: Wednesday, July 5, 2023 10:57 PM
To: Xu Yang <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; dl-linux-imx <[email protected]>; [email protected]
Subject: RE: [PATCH 1/3] perf/imx_ddr: speed up overflow frequency of cycle counter

Quoted 9 lines
drivers/perf/fsl_imx8_ddr_perf.c +4 -0
+
+		/*
+		 * Workaround for i.MX8MP:
+		 * Common counters and byte counters share the same

COUNTER_CNTL,

(quoted diff) +1 -0
+		 * and byte counters could overflow before cycle counter.

Need set

(quoted diff) +1 -0
+		 * counter parameter(CP) of cycle counter to give it initial

value

Quoted 8 lines

drivers/perf/fsl_imx8_ddr_perf.c static void ddr_perf_counter_enable(struct ddr_pmu *pmu, int config, · line 441

Quoted 5 lines
drivers/perf/fsl_imx8_ddr_perf.c +3 -0
+		 * which can speed up cycle counter overflow frequency.
+		 */
+		if ((pmu->devtype_data->quirks &

DDR_CAP_AXI_ID_FILTER_ENHANCED) ==

drivers/perf/fsl_imx8_ddr_perf.c +1 -0
+		    DDR_CAP_AXI_ID_FILTER_ENHANCED) {

needn't ==
if (pmu->devtype_data->quirks & DDR_CAP_AXI_ID_FILTER_ENHANCED)

No, this workaround is only for imx8mp. We need this condition to filter it.

Quoted 2 lines

Did you fixed cycle counter also? Dose cycle counter read get correct value?

Does cycle counter has some issues? I have tested it and can get correct value from cycle counter.

Thanks,
Xu Yang

Quoted 12 lines

Frank

Quoted 9 lines
drivers/perf/fsl_imx8_ddr_perf.c +4 -1
+			if (counter == EVENT_CYCLES_COUNTER)
+				val |= FIELD_PREP(CNTL_CP_MASK, 0xf0);
+		}
+
 		writel(val, pmu->base + reg);
 	} else {
 		/* Disable counter */
--

2.34.1

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel