Skip to content

Commit 178e9fc

Browse files
NonerKaopalmer-dabbelt
authored andcommitted
perf: riscv: preliminary RISC-V support
This patch provide a basic PMU, riscv_base_pmu, which supports two general hardware event, instructions and cycles. Furthermore, this PMU serves as a reference implementation to ease the portings in the future. riscv_base_pmu should be able to run on any RISC-V machine that conforms to the Priv-Spec. Note that the latest qemu model hasn't fully support a proper behavior of Priv-Spec 1.10 yet, but work around should be easy with very small fixes. Please check riscvarchive/riscv-qemu#115 for future updates. Cc: Nick Hu <nickhu@andestech.com> Cc: Greentime Hu <greentime@andestech.com> Signed-off-by: Alan Kao <alankao@andestech.com> Signed-off-by: Palmer Dabbelt <palmer@sifive.com>
1 parent 29dcea8 commit 178e9fc

5 files changed

Lines changed: 586 additions & 0 deletions

File tree

‎arch/riscv/Kconfig‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ config RISCV
2525
select HAVE_DMA_API_DEBUG
2626
select HAVE_DMA_CONTIGUOUS
2727
select HAVE_GENERIC_DMA_COHERENT
28+
select HAVE_PERF_EVENTS
2829
select IRQ_DOMAIN
2930
select NO_BOOTMEM
3031
select RISCV_ISA_A if SMP
@@ -198,6 +199,19 @@ config RISCV_ISA_C
198199
config RISCV_ISA_A
199200
def_bool y
200201

202+
menu "supported PMU type"
203+
depends on PERF_EVENTS
204+
205+
config RISCV_BASE_PMU
206+
bool "Base Performance Monitoring Unit"
207+
def_bool y
208+
help
209+
A base PMU that serves as a reference implementation and has limited
210+
feature of perf. It can run on any RISC-V machines so serves as the
211+
fallback, but this option can also be disable to reduce kernel size.
212+
213+
endmenu
214+
201215
endmenu
202216

203217
menu "Kernel type"

‎arch/riscv/include/asm/Kbuild‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ generic-y += kdebug.h
2525
generic-y += kmap_types.h
2626
generic-y += kvm_para.h
2727
generic-y += local.h
28+
generic-y += local64.h
2829
generic-y += mm-arch-hooks.h
2930
generic-y += mman.h
3031
generic-y += module.h
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* Copyright (C) 2018 SiFive
4+
* Copyright (C) 2018 Andes Technology Corporation
5+
*
6+
*/
7+
8+
#ifndef _ASM_RISCV_PERF_EVENT_H
9+
#define _ASM_RISCV_PERF_EVENT_H
10+
11+
#include <linux/perf_event.h>
12+
#include <linux/ptrace.h>
13+
14+
#define RISCV_BASE_COUNTERS 2
15+
16+
/*
17+
* The RISCV_MAX_COUNTERS parameter should be specified.
18+
*/
19+
20+
#ifdef CONFIG_RISCV_BASE_PMU
21+
#define RISCV_MAX_COUNTERS 2
22+
#endif
23+
24+
#ifndef RISCV_MAX_COUNTERS
25+
#error "Please provide a valid RISCV_MAX_COUNTERS for the PMU."
26+
#endif
27+
28+
/*
29+
* These are the indexes of bits in counteren register *minus* 1,
30+
* except for cycle. It would be coherent if it can directly mapped
31+
* to counteren bit definition, but there is a *time* register at
32+
* counteren[1]. Per-cpu structure is scarce resource here.
33+
*
34+
* According to the spec, an implementation can support counter up to
35+
* mhpmcounter31, but many high-end processors has at most 6 general
36+
* PMCs, we give the definition to MHPMCOUNTER8 here.
37+
*/
38+
#define RISCV_PMU_CYCLE 0
39+
#define RISCV_PMU_INSTRET 1
40+
#define RISCV_PMU_MHPMCOUNTER3 2
41+
#define RISCV_PMU_MHPMCOUNTER4 3
42+
#define RISCV_PMU_MHPMCOUNTER5 4
43+
#define RISCV_PMU_MHPMCOUNTER6 5
44+
#define RISCV_PMU_MHPMCOUNTER7 6
45+
#define RISCV_PMU_MHPMCOUNTER8 7
46+
47+
#define RISCV_OP_UNSUPP (-EOPNOTSUPP)
48+
49+
struct cpu_hw_events {
50+
/* # currently enabled events*/
51+
int n_events;
52+
/* currently enabled events */
53+
struct perf_event *events[RISCV_MAX_COUNTERS];
54+
/* vendor-defined PMU data */
55+
void *platform;
56+
};
57+
58+
struct riscv_pmu {
59+
struct pmu *pmu;
60+
61+
/* generic hw/cache events table */
62+
const int *hw_events;
63+
const int (*cache_events)[PERF_COUNT_HW_CACHE_MAX]
64+
[PERF_COUNT_HW_CACHE_OP_MAX]
65+
[PERF_COUNT_HW_CACHE_RESULT_MAX];
66+
/* method used to map hw/cache events */
67+
int (*map_hw_event)(u64 config);
68+
int (*map_cache_event)(u64 config);
69+
70+
/* max generic hw events in map */
71+
int max_events;
72+
/* number total counters, 2(base) + x(general) */
73+
int num_counters;
74+
/* the width of the counter */
75+
int counter_width;
76+
77+
/* vendor-defined PMU features */
78+
void *platform;
79+
80+
irqreturn_t (*handle_irq)(int irq_num, void *dev);
81+
int irq;
82+
};
83+
84+
#endif /* _ASM_RISCV_PERF_EVENT_H */

‎arch/riscv/kernel/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@ obj-$(CONFIG_MODULE_SECTIONS) += module-sections.o
3939
obj-$(CONFIG_FUNCTION_TRACER) += mcount.o ftrace.o
4040
obj-$(CONFIG_DYNAMIC_FTRACE) += mcount-dyn.o
4141

42+
obj-$(CONFIG_PERF_EVENTS) += perf_event.o
43+
4244
clean:

0 commit comments

Comments
 (0)