perf/imx_ddr: speed up overflow frequency of cycle
Xu Yang
maintainerPATCH v5 1/2 perf/imx_ddr: speed up overflow frequency of cycle
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.
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.
---
Changes in v5:
- change comments
Changes in v4:
- change comments bit error
- add CYCLES_COUNTER_MASK
Changes in v3:
- modify the comments as suggested from Mark
- use mask to remove bias value
- merge two patches to one
Changes in v2:
- improve if condition
--- drivers/perf/fsl_imx8_ddr_perf.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
drivers/perf/fsl_imx8_ddr_perf.c +21 -0
@@ -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)
@@ -35,6 +37,8 @@#define EVENT_CYCLES_COUNTER 0#define NUM_COUNTERS 4 +/* For removing bias if cycle counter CNTL.CP is set to 0xf0 */ +#define CYCLES_COUNTER_MASK 0x0FFFFFFF#define AXI_MASKING_REVERT 0xffff0000/* AXI_MASKING(MSB 16bits) + AXI_ID(LSB 16bits) */ #define to_ddr_pmu (p) container_of(p, struct ddr_pmu, pmu)
@@ -427,6 +431,17 @@ 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); + +/* +* On i.MX8MP we need to bias the cycle counter to overflow more often. +* We do this by initializing bits [23:16] of the counter value via the +* COUNTER_CTRL Counter Parameter (CP) field. +*/ +if (pmu->devtype_data->quirks &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 */
@@ -466,6 +481,12 @@ static void ddr_perf_event_update(struct perf_event *event)int ret; new_raw_count =ddr_perf_read_counter (pmu, counter); +/* Remove the bias applied in ddr_perf_counter_enable(). */ +if (pmu->devtype_data->quirks &DDR_CAP_AXI_ID_FILTER_ENHANCED ) { +if (counter ==EVENT_CYCLES_COUNTER ) + new_raw_count &=CYCLES_COUNTER_MASK ; + } +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
Xu Yang
maintainerPATCH v5 2/2 perf/imx_ddr: don't enable counter0 if none of 4 counters
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.
---
Changes in v5:
- no change
Changes in v4:
- enable cycle counter when active_counter from 0 -> 1
- disable cycle counter when active_counter from 1 -> 0
- make ddr_perf_pmu_enable/disable() an empty function
Changes in v3:
- don't differentiate cycle counter and other counters
- modify logic in pmu_enable()/pmu_disable()
Changes in v2:
- add active counter count as suggested from Frank
--- drivers/perf/fsl_imx8_ddr_perf.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-)
drivers/perf/fsl_imx8_ddr_perf.c +9 -15
@@ -105,6 +105,7 @@ struct ddr_pmu {const struct fsl_ddr_devtype_data *devtype_data;int irq;int id; +int active_counter; }; static ssize_t ddr_perf_identifier_show(struct device *dev,
@@ -516,6 +517,10 @@ static void ddr_perf_event_start(struct perf_event *event, int flags)ddr_perf_counter_enable (pmu, event->attr.config, counter, true); +if (!pmu->active_counter++) +ddr_perf_counter_enable (pmu,EVENT_CYCLES_ID , +EVENT_CYCLES_COUNTER , true); + hwc->state =0 ; }
@@ -569,6 +574,10 @@ static void ddr_perf_event_stop(struct perf_event *event, int flags)ddr_perf_counter_enable (pmu, event->attr.config, counter, false);ddr_perf_event_update (event); +if (!--pmu->active_counter) +ddr_perf_counter_enable (pmu,EVENT_CYCLES_ID , +EVENT_CYCLES_COUNTER , false); + hwc->state |=PERF_HES_STOPPED ; }
@@ -586,25 +595,10 @@ static void ddr_perf_event_del(struct perf_event *event, int flags)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 ) -ddr_perf_counter_enable (ddr_pmu, -EVENT_CYCLES_ID , -EVENT_CYCLES_COUNTER , - true); }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 ) -ddr_perf_counter_enable (ddr_pmu, -EVENT_CYCLES_ID , -EVENT_CYCLES_COUNTER , - false); } static int ddr_perf_init(struct ddr_pmu *pmu, void __iomem *base,
Signature · 7 lines
2.34.1 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Quoted 11 lines · Xu Yang
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.[...]
Applied to will (for-next/perf), thanks!
[1/2] perf/imx_ddr: speed up overflow frequency of cycle
https://git.kernel.org/will/c/e89ecd836886 [2/2] perf/imx_ddr: don't enable counter0 if none of 4 counters are used https://git.kernel.org/will/c/f4e2bd91ddf5
Cheers,
Signature · 10 lines
Will https://fixes.arm64.dev https://next.arm64.dev https://will.arm64.dev _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel