GNU Linux-libre 4.9.309-gnu1
[releases.git] / arch / powerpc / perf / isa207-common.c
1 /*
2  * Common Performance counter support functions for PowerISA v2.07 processors.
3  *
4  * Copyright 2009 Paul Mackerras, IBM Corporation.
5  * Copyright 2013 Michael Ellerman, IBM Corporation.
6  * Copyright 2016 Madhavan Srinivasan, IBM Corporation.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version
11  * 2 of the License, or (at your option) any later version.
12  */
13 #include "isa207-common.h"
14
15 static inline bool event_is_fab_match(u64 event)
16 {
17         /* Only check pmc, unit and pmcxsel, ignore the edge bit (0) */
18         event &= 0xff0fe;
19
20         /* PM_MRK_FAB_RSP_MATCH & PM_MRK_FAB_RSP_MATCH_CYC */
21         return (event == 0x30056 || event == 0x4f052);
22 }
23
24 int isa207_get_constraint(u64 event, unsigned long *maskp, unsigned long *valp)
25 {
26         unsigned int unit, pmc, cache, ebb;
27         unsigned long mask, value;
28
29         mask = value = 0;
30
31         if (event & ~EVENT_VALID_MASK)
32                 return -1;
33
34         pmc   = (event >> EVENT_PMC_SHIFT)        & EVENT_PMC_MASK;
35         unit  = (event >> EVENT_UNIT_SHIFT)       & EVENT_UNIT_MASK;
36         cache = (event >> EVENT_CACHE_SEL_SHIFT)  & EVENT_CACHE_SEL_MASK;
37         ebb   = (event >> EVENT_EBB_SHIFT)        & EVENT_EBB_MASK;
38
39         if (pmc) {
40                 u64 base_event;
41
42                 if (pmc > 6)
43                         return -1;
44
45                 /* Ignore Linux defined bits when checking event below */
46                 base_event = event & ~EVENT_LINUX_MASK;
47
48                 if (pmc >= 5 && base_event != 0x500fa &&
49                                 base_event != 0x600f4)
50                         return -1;
51
52                 mask  |= CNST_PMC_MASK(pmc);
53                 value |= CNST_PMC_VAL(pmc);
54
55                 /*
56                  * PMC5 and PMC6 are used to count cycles and instructions and
57                  * they do not support most of the constraint bits. Add a check
58                  * to exclude PMC5/6 from most of the constraints except for
59                  * EBB/BHRB.
60                  */
61                 if (pmc >= 5)
62                         goto ebb_bhrb;
63         }
64
65         if (pmc <= 4) {
66                 /*
67                  * Add to number of counters in use. Note this includes events with
68                  * a PMC of 0 - they still need a PMC, it's just assigned later.
69                  * Don't count events on PMC 5 & 6, there is only one valid event
70                  * on each of those counters, and they are handled above.
71                  */
72                 mask  |= CNST_NC_MASK;
73                 value |= CNST_NC_VAL;
74         }
75
76         if (unit >= 6 && unit <= 9) {
77                 /*
78                  * L2/L3 events contain a cache selector field, which is
79                  * supposed to be programmed into MMCRC. However MMCRC is only
80                  * HV writable, and there is no API for guest kernels to modify
81                  * it. The solution is for the hypervisor to initialise the
82                  * field to zeroes, and for us to only ever allow events that
83                  * have a cache selector of zero. The bank selector (bit 3) is
84                  * irrelevant, as long as the rest of the value is 0.
85                  */
86                 if (cache & 0x7)
87                         return -1;
88
89         } else if (event & EVENT_IS_L1) {
90                 mask  |= CNST_L1_QUAL_MASK;
91                 value |= CNST_L1_QUAL_VAL(cache);
92         }
93
94         if (event & EVENT_IS_MARKED) {
95                 mask  |= CNST_SAMPLE_MASK;
96                 value |= CNST_SAMPLE_VAL(event >> EVENT_SAMPLE_SHIFT);
97         }
98
99         /*
100          * Special case for PM_MRK_FAB_RSP_MATCH and PM_MRK_FAB_RSP_MATCH_CYC,
101          * the threshold control bits are used for the match value.
102          */
103         if (event_is_fab_match(event)) {
104                 mask  |= CNST_FAB_MATCH_MASK;
105                 value |= CNST_FAB_MATCH_VAL(event >> EVENT_THR_CTL_SHIFT);
106         } else {
107                 /*
108                  * Check the mantissa upper two bits are not zero, unless the
109                  * exponent is also zero. See the THRESH_CMP_MANTISSA doc.
110                  */
111                 unsigned int cmp, exp;
112
113                 cmp = (event >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
114                 exp = cmp >> 7;
115
116                 if (exp && (cmp & 0x60) == 0)
117                         return -1;
118
119                 mask  |= CNST_THRESH_MASK;
120                 value |= CNST_THRESH_VAL(event >> EVENT_THRESH_SHIFT);
121         }
122
123 ebb_bhrb:
124         if (!pmc && ebb)
125                 /* EBB events must specify the PMC */
126                 return -1;
127
128         if (event & EVENT_WANTS_BHRB) {
129                 if (!ebb)
130                         /* Only EBB events can request BHRB */
131                         return -1;
132
133                 mask  |= CNST_IFM_MASK;
134                 value |= CNST_IFM_VAL(event >> EVENT_IFM_SHIFT);
135         }
136
137         /*
138          * All events must agree on EBB, either all request it or none.
139          * EBB events are pinned & exclusive, so this should never actually
140          * hit, but we leave it as a fallback in case.
141          */
142         mask  |= CNST_EBB_MASK;
143         value |= CNST_EBB_VAL(ebb);
144
145         *maskp = mask;
146         *valp = value;
147
148         return 0;
149 }
150
151 int isa207_compute_mmcr(u64 event[], int n_ev,
152                                unsigned int hwc[], unsigned long mmcr[],
153                                struct perf_event *pevents[])
154 {
155         unsigned long mmcra, mmcr1, mmcr2, unit, combine, psel, cache, val;
156         unsigned int pmc, pmc_inuse;
157         int i;
158
159         pmc_inuse = 0;
160
161         /* First pass to count resource use */
162         for (i = 0; i < n_ev; ++i) {
163                 pmc = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
164                 if (pmc)
165                         pmc_inuse |= 1 << pmc;
166         }
167
168         /* In continuous sampling mode, update SDAR on TLB miss */
169         mmcra = MMCRA_SDAR_MODE_TLB;
170         mmcr1 = mmcr2 = 0;
171
172         /* Second pass: assign PMCs, set all MMCR1 fields */
173         for (i = 0; i < n_ev; ++i) {
174                 pmc     = (event[i] >> EVENT_PMC_SHIFT) & EVENT_PMC_MASK;
175                 unit    = (event[i] >> EVENT_UNIT_SHIFT) & EVENT_UNIT_MASK;
176                 combine = (event[i] >> EVENT_COMBINE_SHIFT) & EVENT_COMBINE_MASK;
177                 psel    =  event[i] & EVENT_PSEL_MASK;
178
179                 if (!pmc) {
180                         for (pmc = 1; pmc <= 4; ++pmc) {
181                                 if (!(pmc_inuse & (1 << pmc)))
182                                         break;
183                         }
184
185                         pmc_inuse |= 1 << pmc;
186                 }
187
188                 if (pmc <= 4) {
189                         mmcr1 |= unit << MMCR1_UNIT_SHIFT(pmc);
190                         mmcr1 |= combine << MMCR1_COMBINE_SHIFT(pmc);
191                         mmcr1 |= psel << MMCR1_PMCSEL_SHIFT(pmc);
192                 }
193
194                 if (event[i] & EVENT_IS_L1) {
195                         cache = event[i] >> EVENT_CACHE_SEL_SHIFT;
196                         mmcr1 |= (cache & 1) << MMCR1_IC_QUAL_SHIFT;
197                         cache >>= 1;
198                         mmcr1 |= (cache & 1) << MMCR1_DC_QUAL_SHIFT;
199                 }
200
201                 if (event[i] & EVENT_IS_MARKED) {
202                         mmcra |= MMCRA_SAMPLE_ENABLE;
203
204                         val = (event[i] >> EVENT_SAMPLE_SHIFT) & EVENT_SAMPLE_MASK;
205                         if (val) {
206                                 mmcra |= (val &  3) << MMCRA_SAMP_MODE_SHIFT;
207                                 mmcra |= (val >> 2) << MMCRA_SAMP_ELIG_SHIFT;
208                         }
209                 }
210
211                 /*
212                  * PM_MRK_FAB_RSP_MATCH and PM_MRK_FAB_RSP_MATCH_CYC,
213                  * the threshold bits are used for the match value.
214                  */
215                 if (event_is_fab_match(event[i])) {
216                         mmcr1 |= ((event[i] >> EVENT_THR_CTL_SHIFT) &
217                                   EVENT_THR_CTL_MASK) << MMCR1_FAB_SHIFT;
218                 } else {
219                         val = (event[i] >> EVENT_THR_CTL_SHIFT) & EVENT_THR_CTL_MASK;
220                         mmcra |= val << MMCRA_THR_CTL_SHIFT;
221                         val = (event[i] >> EVENT_THR_SEL_SHIFT) & EVENT_THR_SEL_MASK;
222                         mmcra |= val << MMCRA_THR_SEL_SHIFT;
223                         val = (event[i] >> EVENT_THR_CMP_SHIFT) & EVENT_THR_CMP_MASK;
224                         mmcra |= val << MMCRA_THR_CMP_SHIFT;
225                 }
226
227                 if (event[i] & EVENT_WANTS_BHRB) {
228                         val = (event[i] >> EVENT_IFM_SHIFT) & EVENT_IFM_MASK;
229                         mmcra |= val << MMCRA_IFM_SHIFT;
230                 }
231
232                 if (pevents[i]->attr.exclude_user)
233                         mmcr2 |= MMCR2_FCP(pmc);
234
235                 if (pevents[i]->attr.exclude_hv)
236                         mmcr2 |= MMCR2_FCH(pmc);
237
238                 if (pevents[i]->attr.exclude_kernel) {
239                         if (cpu_has_feature(CPU_FTR_HVMODE))
240                                 mmcr2 |= MMCR2_FCH(pmc);
241                         else
242                                 mmcr2 |= MMCR2_FCS(pmc);
243                 }
244
245                 hwc[i] = pmc - 1;
246         }
247
248         /* Return MMCRx values */
249         mmcr[0] = 0;
250
251         /* pmc_inuse is 1-based */
252         if (pmc_inuse & 2)
253                 mmcr[0] = MMCR0_PMC1CE;
254
255         if (pmc_inuse & 0x7c)
256                 mmcr[0] |= MMCR0_PMCjCE;
257
258         /* If we're not using PMC 5 or 6, freeze them */
259         if (!(pmc_inuse & 0x60))
260                 mmcr[0] |= MMCR0_FC56;
261
262         mmcr[1] = mmcr1;
263         mmcr[2] = mmcra;
264         mmcr[3] = mmcr2;
265
266         return 0;
267 }
268
269 void isa207_disable_pmc(unsigned int pmc, unsigned long mmcr[])
270 {
271         if (pmc <= 3)
272                 mmcr[1] &= ~(0xffUL << MMCR1_PMCSEL_SHIFT(pmc + 1));
273 }