1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Performance event support - PPC 8xx
5 * Copyright 2016 Christophe Leroy, CS Systemes d'Information
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/perf_event.h>
11 #include <linux/percpu.h>
12 #include <linux/hardirq.h>
14 #include <asm/machdep.h>
15 #include <asm/firmware.h>
16 #include <asm/ptrace.h>
17 #include <asm/code-patching.h>
19 #define PERF_8xx_ID_CPU_CYCLES 1
20 #define PERF_8xx_ID_HW_INSTRUCTIONS 2
21 #define PERF_8xx_ID_ITLB_LOAD_MISS 3
22 #define PERF_8xx_ID_DTLB_LOAD_MISS 4
24 #define C(x) PERF_COUNT_HW_CACHE_##x
25 #define DTLB_LOAD_MISS (C(DTLB) | (C(OP_READ) << 8) | (C(RESULT_MISS) << 16))
26 #define ITLB_LOAD_MISS (C(ITLB) | (C(OP_READ) << 8) | (C(RESULT_MISS) << 16))
28 extern unsigned long itlb_miss_counter
, dtlb_miss_counter
;
29 extern atomic_t instruction_counter
;
31 static atomic_t insn_ctr_ref
;
32 static atomic_t itlb_miss_ref
;
33 static atomic_t dtlb_miss_ref
;
35 static s64
get_insn_ctr(void)
41 ctr
= atomic_read(&instruction_counter
);
42 counta
= mfspr(SPRN_COUNTA
);
43 } while (ctr
!= atomic_read(&instruction_counter
));
45 return ((s64
)ctr
<< 16) | (counta
>> 16);
48 static int event_type(struct perf_event
*event
)
50 switch (event
->attr
.type
) {
51 case PERF_TYPE_HARDWARE
:
52 if (event
->attr
.config
== PERF_COUNT_HW_CPU_CYCLES
)
53 return PERF_8xx_ID_CPU_CYCLES
;
54 if (event
->attr
.config
== PERF_COUNT_HW_INSTRUCTIONS
)
55 return PERF_8xx_ID_HW_INSTRUCTIONS
;
57 case PERF_TYPE_HW_CACHE
:
58 if (event
->attr
.config
== ITLB_LOAD_MISS
)
59 return PERF_8xx_ID_ITLB_LOAD_MISS
;
60 if (event
->attr
.config
== DTLB_LOAD_MISS
)
61 return PERF_8xx_ID_DTLB_LOAD_MISS
;
71 static int mpc8xx_pmu_event_init(struct perf_event
*event
)
73 int type
= event_type(event
);
80 static int mpc8xx_pmu_add(struct perf_event
*event
, int flags
)
82 int type
= event_type(event
);
89 case PERF_8xx_ID_CPU_CYCLES
:
92 case PERF_8xx_ID_HW_INSTRUCTIONS
:
93 if (atomic_inc_return(&insn_ctr_ref
) == 1)
94 mtspr(SPRN_ICTRL
, 0xc0080007);
97 case PERF_8xx_ID_ITLB_LOAD_MISS
:
98 if (atomic_inc_return(&itlb_miss_ref
) == 1) {
99 unsigned long target
= patch_site_addr(&patch__itlbmiss_perf
);
101 patch_branch_site(&patch__itlbmiss_exit_1
, target
, 0);
102 #ifndef CONFIG_PIN_TLB_TEXT
103 patch_branch_site(&patch__itlbmiss_exit_2
, target
, 0);
106 val
= itlb_miss_counter
;
108 case PERF_8xx_ID_DTLB_LOAD_MISS
:
109 if (atomic_inc_return(&dtlb_miss_ref
) == 1) {
110 unsigned long target
= patch_site_addr(&patch__dtlbmiss_perf
);
112 patch_branch_site(&patch__dtlbmiss_exit_1
, target
, 0);
113 patch_branch_site(&patch__dtlbmiss_exit_2
, target
, 0);
114 patch_branch_site(&patch__dtlbmiss_exit_3
, target
, 0);
116 val
= dtlb_miss_counter
;
119 local64_set(&event
->hw
.prev_count
, val
);
123 static void mpc8xx_pmu_read(struct perf_event
*event
)
125 int type
= event_type(event
);
126 s64 prev
, val
= 0, delta
= 0;
132 prev
= local64_read(&event
->hw
.prev_count
);
134 case PERF_8xx_ID_CPU_CYCLES
:
136 delta
= 16 * (val
- prev
);
138 case PERF_8xx_ID_HW_INSTRUCTIONS
:
139 val
= get_insn_ctr();
142 delta
+= 0x1000000000000LL
;
144 case PERF_8xx_ID_ITLB_LOAD_MISS
:
145 val
= itlb_miss_counter
;
146 delta
= (s64
)((s32
)val
- (s32
)prev
);
148 case PERF_8xx_ID_DTLB_LOAD_MISS
:
149 val
= dtlb_miss_counter
;
150 delta
= (s64
)((s32
)val
- (s32
)prev
);
153 } while (local64_cmpxchg(&event
->hw
.prev_count
, prev
, val
) != prev
);
155 local64_add(delta
, &event
->count
);
158 static void mpc8xx_pmu_del(struct perf_event
*event
, int flags
)
160 /* mfspr r10, SPRN_SPRG_SCRATCH0 */
161 unsigned int insn
= PPC_INST_MFSPR
| __PPC_RS(R10
) |
162 __PPC_SPR(SPRN_SPRG_SCRATCH0
);
164 mpc8xx_pmu_read(event
);
166 /* If it was the last user, stop counting to avoid useles overhead */
167 switch (event_type(event
)) {
168 case PERF_8xx_ID_CPU_CYCLES
:
170 case PERF_8xx_ID_HW_INSTRUCTIONS
:
171 if (atomic_dec_return(&insn_ctr_ref
) == 0)
172 mtspr(SPRN_ICTRL
, 7);
174 case PERF_8xx_ID_ITLB_LOAD_MISS
:
175 if (atomic_dec_return(&itlb_miss_ref
) == 0) {
176 patch_instruction_site(&patch__itlbmiss_exit_1
, insn
);
177 #ifndef CONFIG_PIN_TLB_TEXT
178 patch_instruction_site(&patch__itlbmiss_exit_2
, insn
);
182 case PERF_8xx_ID_DTLB_LOAD_MISS
:
183 if (atomic_dec_return(&dtlb_miss_ref
) == 0) {
184 patch_instruction_site(&patch__dtlbmiss_exit_1
, insn
);
185 patch_instruction_site(&patch__dtlbmiss_exit_2
, insn
);
186 patch_instruction_site(&patch__dtlbmiss_exit_3
, insn
);
192 static struct pmu mpc8xx_pmu
= {
193 .event_init
= mpc8xx_pmu_event_init
,
194 .add
= mpc8xx_pmu_add
,
195 .del
= mpc8xx_pmu_del
,
196 .read
= mpc8xx_pmu_read
,
197 .capabilities
= PERF_PMU_CAP_NO_INTERRUPT
|
201 static int init_mpc8xx_pmu(void)
203 mtspr(SPRN_ICTRL
, 7);
205 mtspr(SPRN_COUNTA
, 0xffff);
207 return perf_pmu_register(&mpc8xx_pmu
, "cpu", PERF_TYPE_RAW
);
210 early_initcall(init_mpc8xx_pmu
);