RISC-V: Add perf platform driver based on SBI PMU extension
[linux-2.6-block.git] / include / linux / perf / riscv_pmu.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2018 SiFive
4  * Copyright (C) 2018 Andes Technology Corporation
5  * Copyright (C) 2021 Western Digital Corporation or its affiliates.
6  *
7  */
8
9 #ifndef _ASM_RISCV_PERF_EVENT_H
10 #define _ASM_RISCV_PERF_EVENT_H
11
12 #include <linux/perf_event.h>
13 #include <linux/ptrace.h>
14 #include <linux/interrupt.h>
15
16 #ifdef CONFIG_RISCV_PMU
17
18 /*
19  * The RISCV_MAX_COUNTERS parameter should be specified.
20  */
21
22 #define RISCV_MAX_COUNTERS      64
23 #define RISCV_OP_UNSUPP         (-EOPNOTSUPP)
24 #define RISCV_PMU_PDEV_NAME     "riscv-pmu"
25 #define RISCV_PMU_LEGACY_PDEV_NAME      "riscv-pmu-legacy"
26
27 #define RISCV_PMU_STOP_FLAG_RESET 1
28
29 struct cpu_hw_events {
30         /* currently enabled events */
31         int                     n_events;
32         /* currently enabled events */
33         struct perf_event       *events[RISCV_MAX_COUNTERS];
34         /* currently enabled hardware counters */
35         DECLARE_BITMAP(used_hw_ctrs, RISCV_MAX_COUNTERS);
36         /* currently enabled firmware counters */
37         DECLARE_BITMAP(used_fw_ctrs, RISCV_MAX_COUNTERS);
38 };
39
40 struct riscv_pmu {
41         struct pmu      pmu;
42         char            *name;
43
44         irqreturn_t     (*handle_irq)(int irq_num, void *dev);
45
46         int             num_counters;
47         u64             (*ctr_read)(struct perf_event *event);
48         int             (*ctr_get_idx)(struct perf_event *event);
49         int             (*ctr_get_width)(int idx);
50         void            (*ctr_clear_idx)(struct perf_event *event);
51         void            (*ctr_start)(struct perf_event *event, u64 init_val);
52         void            (*ctr_stop)(struct perf_event *event, unsigned long flag);
53         int             (*event_map)(struct perf_event *event, u64 *config);
54
55         struct cpu_hw_events    __percpu *hw_events;
56         struct hlist_node       node;
57 };
58
59 #define to_riscv_pmu(p) (container_of(p, struct riscv_pmu, pmu))
60 unsigned long riscv_pmu_ctr_read_csr(unsigned long csr);
61 int riscv_pmu_event_set_period(struct perf_event *event);
62 uint64_t riscv_pmu_ctr_get_width_mask(struct perf_event *event);
63 u64 riscv_pmu_event_update(struct perf_event *event);
64 #ifdef CONFIG_RISCV_PMU_LEGACY
65 void riscv_pmu_legacy_skip_init(void);
66 #else
67 static inline void riscv_pmu_legacy_skip_init(void) {};
68 #endif
69 struct riscv_pmu *riscv_pmu_alloc(void);
70
71 #endif /* CONFIG_RISCV_PMU */
72
73 #endif /* _ASM_RISCV_PERF_EVENT_H */