Skip to content

Commit 9801002

Browse files
perf: riscv_pmu{,_sbi}: Miscallenous improvement & fixes
A series of mostly-independent fixes and cleanups for the RISC-V PMU drivers. Link: https://lore.kernel.org/lkml/CAAhSdy23vE8+HxU5Jxy2rBMjy3rBTrJt_4sriuROac_sEESSVw@mail.gmail.com/T/#m9de15aef1b65ae6155fa33ea1239578ef463c2a2 * palmer/riscv-pmu: RISC-V: Improve SBI definitions RISC-V: Move counter info definition to sbi header file RISC-V: Fix SBI PMU calls for RV32 RISC-V: Update user page mapping only once during start RISC-V: Fix counter restart during overflow for RV32
2 parents 7ab52f7 + f829ee7 commit 9801002

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

arch/riscv/include/asm/sbi.h

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,21 @@ enum sbi_ext_pmu_fid {
122122
SBI_EXT_PMU_COUNTER_FW_READ,
123123
};
124124

125-
#define RISCV_PMU_RAW_EVENT_MASK GENMASK_ULL(55, 0)
125+
union sbi_pmu_ctr_info {
126+
unsigned long value;
127+
struct {
128+
unsigned long csr:12;
129+
unsigned long width:6;
130+
#if __riscv_xlen == 32
131+
unsigned long reserved:13;
132+
#else
133+
unsigned long reserved:45;
134+
#endif
135+
unsigned long type:1;
136+
};
137+
};
138+
139+
#define RISCV_PMU_RAW_EVENT_MASK GENMASK_ULL(47, 0)
126140
#define RISCV_PMU_RAW_EVENT_IDX 0x20000
127141

128142
/** General pmu event codes specified in SBI PMU extension */
@@ -189,12 +203,26 @@ enum sbi_pmu_ctr_type {
189203
SBI_PMU_CTR_TYPE_FW,
190204
};
191205

206+
/* Helper macros to decode event idx */
207+
#define SBI_PMU_EVENT_IDX_OFFSET 20
208+
#define SBI_PMU_EVENT_IDX_MASK 0xFFFFF
209+
#define SBI_PMU_EVENT_IDX_CODE_MASK 0xFFFF
210+
#define SBI_PMU_EVENT_IDX_TYPE_MASK 0xF0000
211+
#define SBI_PMU_EVENT_RAW_IDX 0x20000
212+
#define SBI_PMU_FIXED_CTR_MASK 0x07
213+
214+
#define SBI_PMU_EVENT_CACHE_ID_CODE_MASK 0xFFF8
215+
#define SBI_PMU_EVENT_CACHE_OP_ID_CODE_MASK 0x06
216+
#define SBI_PMU_EVENT_CACHE_RESULT_ID_CODE_MASK 0x01
217+
218+
#define SBI_PMU_EVENT_IDX_INVALID 0xFFFFFFFF
219+
192220
/* Flags defined for config matching function */
193221
#define SBI_PMU_CFG_FLAG_SKIP_MATCH (1 << 0)
194222
#define SBI_PMU_CFG_FLAG_CLEAR_VALUE (1 << 1)
195223
#define SBI_PMU_CFG_FLAG_AUTO_START (1 << 2)
196224
#define SBI_PMU_CFG_FLAG_SET_VUINH (1 << 3)
197-
#define SBI_PMU_CFG_FLAG_SET_VSNH (1 << 4)
225+
#define SBI_PMU_CFG_FLAG_SET_VSINH (1 << 4)
198226
#define SBI_PMU_CFG_FLAG_SET_UINH (1 << 5)
199227
#define SBI_PMU_CFG_FLAG_SET_SINH (1 << 6)
200228
#define SBI_PMU_CFG_FLAG_SET_MINH (1 << 7)

drivers/perf/riscv_pmu.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ int riscv_pmu_event_set_period(struct perf_event *event)
170170
left = (max_period >> 1);
171171

172172
local64_set(&hwc->prev_count, (u64)-left);
173-
perf_event_update_userpage(event);
174173

175174
return overflow;
176175
}

drivers/perf/riscv_pmu_sbi.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,6 @@ static const struct attribute_group *riscv_pmu_attr_groups[] = {
4141
NULL,
4242
};
4343

44-
union sbi_pmu_ctr_info {
45-
unsigned long value;
46-
struct {
47-
unsigned long csr:12;
48-
unsigned long width:6;
49-
#if __riscv_xlen == 32
50-
unsigned long reserved:13;
51-
#else
52-
unsigned long reserved:45;
53-
#endif
54-
unsigned long type:1;
55-
};
56-
};
57-
5844
/*
5945
* RISC-V doesn't have hetergenous harts yet. This need to be part of
6046
* per_cpu in case of harts with different pmu counters
@@ -294,8 +280,13 @@ static int pmu_sbi_ctr_get_idx(struct perf_event *event)
294280
cflags |= SBI_PMU_CFG_FLAG_SET_UINH;
295281

296282
/* retrieve the available counter index */
283+
#if defined(CONFIG_32BIT)
284+
ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, cbase, cmask,
285+
cflags, hwc->event_base, hwc->config, hwc->config >> 32);
286+
#else
297287
ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, cbase, cmask,
298288
cflags, hwc->event_base, hwc->config, 0);
289+
#endif
299290
if (ret.error) {
300291
pr_debug("Not able to find a counter for event %lx config %llx\n",
301292
hwc->event_base, hwc->config);
@@ -437,8 +428,13 @@ static void pmu_sbi_ctr_start(struct perf_event *event, u64 ival)
437428
struct hw_perf_event *hwc = &event->hw;
438429
unsigned long flag = SBI_PMU_START_FLAG_SET_INIT_VALUE;
439430

431+
#if defined(CONFIG_32BIT)
440432
ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, hwc->idx,
441433
1, flag, ival, ival >> 32, 0);
434+
#else
435+
ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, hwc->idx,
436+
1, flag, ival, 0, 0);
437+
#endif
442438
if (ret.error && (ret.error != SBI_ERR_ALREADY_STARTED))
443439
pr_err("Starting counter idx %d failed with error %d\n",
444440
hwc->idx, sbi_err_map_linux_errno(ret.error));
@@ -545,8 +541,14 @@ static inline void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu,
545541
hwc = &event->hw;
546542
max_period = riscv_pmu_ctr_get_width_mask(event);
547543
init_val = local64_read(&hwc->prev_count) & max_period;
544+
#if defined(CONFIG_32BIT)
545+
sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
546+
flag, init_val, init_val >> 32, 0);
547+
#else
548548
sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
549549
flag, init_val, 0, 0);
550+
#endif
551+
perf_event_update_userpage(event);
550552
}
551553
ctr_ovf_mask = ctr_ovf_mask >> 1;
552554
idx++;

0 commit comments

Comments
 (0)