blob: 683e289145d79fe22103ec5b35593cfaaa2b26fe [file] [log] [blame]
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001/*
2 * CCI cache coherent interconnect driver
3 *
4 * Copyright (C) 2013 ARM Ltd.
5 * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/arm-cci.h>
18#include <linux/io.h>
Mark Rutlandc6f85cb2014-06-30 12:20:21 +010019#include <linux/interrupt.h>
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010020#include <linux/module.h>
21#include <linux/of_address.h>
Punit Agrawalb91c8f22013-08-22 14:41:51 +010022#include <linux/of_irq.h>
23#include <linux/of_platform.h>
Mark Rutlandc6f85cb2014-06-30 12:20:21 +010024#include <linux/perf_event.h>
Punit Agrawalb91c8f22013-08-22 14:41:51 +010025#include <linux/platform_device.h>
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010026#include <linux/slab.h>
Punit Agrawalb91c8f22013-08-22 14:41:51 +010027#include <linux/spinlock.h>
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010028
29#include <asm/cacheflush.h>
30#include <asm/smp_plat.h>
31
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000032static void __iomem *cci_ctrl_base;
33static unsigned long cci_ctrl_phys;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010034
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +000035#ifdef CONFIG_ARM_CCI400_PORT_CTRL
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010036struct cci_nb_ports {
37 unsigned int nb_ace;
38 unsigned int nb_ace_lite;
39};
40
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000041static const struct cci_nb_ports cci400_ports = {
42 .nb_ace = 2,
43 .nb_ace_lite = 3
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010044};
45
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +000046#define CCI400_PORTS_DATA (&cci400_ports)
47#else
48#define CCI400_PORTS_DATA (NULL)
49#endif
50
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000051static const struct of_device_id arm_cci_matches[] = {
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +000052#ifdef CONFIG_ARM_CCI400_COMMON
53 {.compatible = "arm,cci-400", .data = CCI400_PORTS_DATA },
54#endif
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +000055#ifdef CONFIG_ARM_CCI5xx_PMU
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +010056 { .compatible = "arm,cci-500", },
57#endif
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000058 {},
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010059};
60
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +010061#ifdef CONFIG_ARM_CCI_PMU
Punit Agrawalb91c8f22013-08-22 14:41:51 +010062
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +010063#define DRIVER_NAME "ARM-CCI"
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000064#define DRIVER_NAME_PMU DRIVER_NAME " PMU"
65
Punit Agrawalb91c8f22013-08-22 14:41:51 +010066#define CCI_PMCR 0x0100
67#define CCI_PID2 0x0fe8
68
69#define CCI_PMCR_CEN 0x00000001
70#define CCI_PMCR_NCNT_MASK 0x0000f800
71#define CCI_PMCR_NCNT_SHIFT 11
72
73#define CCI_PID2_REV_MASK 0xf0
74#define CCI_PID2_REV_SHIFT 4
75
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000076#define CCI_PMU_EVT_SEL 0x000
77#define CCI_PMU_CNTR 0x004
78#define CCI_PMU_CNTR_CTRL 0x008
79#define CCI_PMU_OVRFLW 0x00c
80
81#define CCI_PMU_OVRFLW_FLAG 1
82
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +010083#define CCI_PMU_CNTR_SIZE(model) ((model)->cntr_size)
84#define CCI_PMU_CNTR_BASE(model, idx) ((idx) * CCI_PMU_CNTR_SIZE(model))
85#define CCI_PMU_CNTR_MASK ((1ULL << 32) -1)
86#define CCI_PMU_CNTR_LAST(cci_pmu) (cci_pmu->num_cntrs - 1)
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000087
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +010088#define CCI_PMU_MAX_HW_CNTRS(model) \
89 ((model)->num_hw_cntrs + (model)->fixed_hw_cntrs)
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000090
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +000091/* Types of interfaces that can generate events */
92enum {
93 CCI_IF_SLAVE,
94 CCI_IF_MASTER,
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +000095#ifdef CONFIG_ARM_CCI5xx_PMU
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +010096 CCI_IF_GLOBAL,
97#endif
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +000098 CCI_IF_MAX,
99};
100
101struct event_range {
102 u32 min;
103 u32 max;
104};
105
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000106struct cci_pmu_hw_events {
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100107 struct perf_event **events;
108 unsigned long *used_mask;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000109 raw_spinlock_t pmu_lock;
110};
111
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100112struct cci_pmu;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100113/*
114 * struct cci_pmu_model:
115 * @fixed_hw_cntrs - Number of fixed event counters
116 * @num_hw_cntrs - Maximum number of programmable event counters
117 * @cntr_size - Size of an event counter mapping
118 */
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000119struct cci_pmu_model {
120 char *name;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100121 u32 fixed_hw_cntrs;
122 u32 num_hw_cntrs;
123 u32 cntr_size;
Mark Rutland5e442eb2016-02-23 10:49:43 +0000124 struct attribute **format_attrs;
125 struct attribute **event_attrs;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000126 struct event_range event_ranges[CCI_IF_MAX];
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100127 int (*validate_hw_event)(struct cci_pmu *, unsigned long);
128 int (*get_event_idx)(struct cci_pmu *, struct cci_pmu_hw_events *, unsigned long);
Suzuki K Poulosefff3f1a2016-02-23 10:49:52 +0000129 void (*write_counters)(struct cci_pmu *, unsigned long *);
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000130};
131
132static struct cci_pmu_model cci_pmu_models[];
133
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000134struct cci_pmu {
135 void __iomem *base;
136 struct pmu pmu;
137 int nr_irqs;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100138 int *irqs;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000139 unsigned long active_irqs;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000140 const struct cci_pmu_model *model;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000141 struct cci_pmu_hw_events hw_events;
142 struct platform_device *plat_device;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100143 int num_cntrs;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000144 atomic_t active_events;
145 struct mutex reserve_mutex;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100146 struct notifier_block cpu_nb;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000147 cpumask_t cpus;
148};
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000149
150#define to_cci_pmu(c) (container_of(c, struct cci_pmu, pmu))
151
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100152enum cci_models {
153#ifdef CONFIG_ARM_CCI400_PMU
154 CCI400_R0,
155 CCI400_R1,
156#endif
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000157#ifdef CONFIG_ARM_CCI5xx_PMU
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100158 CCI500_R0,
159#endif
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100160 CCI_MODEL_MAX
161};
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100162
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000163static void pmu_write_counters(struct cci_pmu *cci_pmu,
164 unsigned long *mask);
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100165static ssize_t cci_pmu_format_show(struct device *dev,
166 struct device_attribute *attr, char *buf);
167static ssize_t cci_pmu_event_show(struct device *dev,
168 struct device_attribute *attr, char *buf);
169
Mark Rutland5e442eb2016-02-23 10:49:43 +0000170#define CCI_EXT_ATTR_ENTRY(_name, _func, _config) \
171 &((struct dev_ext_attribute[]) { \
172 { __ATTR(_name, S_IRUGO, _func, NULL), (void *)_config } \
173 })[0].attr.attr
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100174
175#define CCI_FORMAT_EXT_ATTR_ENTRY(_name, _config) \
176 CCI_EXT_ATTR_ENTRY(_name, cci_pmu_format_show, (char *)_config)
177#define CCI_EVENT_EXT_ATTR_ENTRY(_name, _config) \
178 CCI_EXT_ATTR_ENTRY(_name, cci_pmu_event_show, (unsigned long)_config)
179
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100180/* CCI400 PMU Specific definitions */
181
182#ifdef CONFIG_ARM_CCI400_PMU
183
184/* Port ids */
185#define CCI400_PORT_S0 0
186#define CCI400_PORT_S1 1
187#define CCI400_PORT_S2 2
188#define CCI400_PORT_S3 3
189#define CCI400_PORT_S4 4
190#define CCI400_PORT_M0 5
191#define CCI400_PORT_M1 6
192#define CCI400_PORT_M2 7
193
194#define CCI400_R1_PX 5
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100195
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100196/*
197 * Instead of an event id to monitor CCI cycles, a dedicated counter is
198 * provided. Use 0xff to represent CCI cycles and hope that no future revisions
199 * make use of this event in hardware.
200 */
201enum cci400_perf_events {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100202 CCI400_PMU_CYCLES = 0xff
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100203};
204
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100205#define CCI400_PMU_CYCLE_CNTR_IDX 0
206#define CCI400_PMU_CNTR0_IDX 1
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100207
208/*
209 * CCI PMU event id is an 8-bit value made of two parts - bits 7:5 for one of 8
210 * ports and bits 4:0 are event codes. There are different event codes
211 * associated with each port type.
212 *
213 * Additionally, the range of events associated with the port types changed
214 * between Rev0 and Rev1.
215 *
216 * The constants below define the range of valid codes for each port type for
217 * the different revisions and are used to validate the event to be monitored.
218 */
219
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100220#define CCI400_PMU_EVENT_MASK 0xffUL
221#define CCI400_PMU_EVENT_SOURCE_SHIFT 5
222#define CCI400_PMU_EVENT_SOURCE_MASK 0x7
223#define CCI400_PMU_EVENT_CODE_SHIFT 0
224#define CCI400_PMU_EVENT_CODE_MASK 0x1f
225#define CCI400_PMU_EVENT_SOURCE(event) \
226 ((event >> CCI400_PMU_EVENT_SOURCE_SHIFT) & \
227 CCI400_PMU_EVENT_SOURCE_MASK)
228#define CCI400_PMU_EVENT_CODE(event) \
229 ((event >> CCI400_PMU_EVENT_CODE_SHIFT) & CCI400_PMU_EVENT_CODE_MASK)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100230
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100231#define CCI400_R0_SLAVE_PORT_MIN_EV 0x00
232#define CCI400_R0_SLAVE_PORT_MAX_EV 0x13
233#define CCI400_R0_MASTER_PORT_MIN_EV 0x14
234#define CCI400_R0_MASTER_PORT_MAX_EV 0x1a
235
236#define CCI400_R1_SLAVE_PORT_MIN_EV 0x00
237#define CCI400_R1_SLAVE_PORT_MAX_EV 0x14
238#define CCI400_R1_MASTER_PORT_MIN_EV 0x00
239#define CCI400_R1_MASTER_PORT_MAX_EV 0x11
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100240
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100241#define CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(_name, _config) \
242 CCI_EXT_ATTR_ENTRY(_name, cci400_pmu_cycle_event_show, \
243 (unsigned long)_config)
244
245static ssize_t cci400_pmu_cycle_event_show(struct device *dev,
246 struct device_attribute *attr, char *buf);
247
Mark Rutland5e442eb2016-02-23 10:49:43 +0000248static struct attribute *cci400_pmu_format_attrs[] = {
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100249 CCI_FORMAT_EXT_ATTR_ENTRY(event, "config:0-4"),
250 CCI_FORMAT_EXT_ATTR_ENTRY(source, "config:5-7"),
Mark Rutland5e442eb2016-02-23 10:49:43 +0000251 NULL
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100252};
253
Mark Rutland5e442eb2016-02-23 10:49:43 +0000254static struct attribute *cci400_r0_pmu_event_attrs[] = {
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100255 /* Slave events */
256 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_any, 0x0),
257 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_device, 0x01),
258 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_normal_or_nonshareable, 0x2),
259 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_inner_or_outershareable, 0x3),
260 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maintenance, 0x4),
261 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_mem_barrier, 0x5),
262 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_sync_barrier, 0x6),
263 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
264 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg_sync, 0x8),
265 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_tt_full, 0x9),
266 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_last_hs_snoop, 0xA),
267 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall_rvalids_h_rready_l, 0xB),
268 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_any, 0xC),
269 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_device, 0xD),
270 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_normal_or_nonshareable, 0xE),
271 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_inner_or_outershare_wback_wclean, 0xF),
272 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_unique, 0x10),
273 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_line_unique, 0x11),
274 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_evict, 0x12),
275 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall_tt_full, 0x13),
276 /* Master events */
277 CCI_EVENT_EXT_ATTR_ENTRY(mi_retry_speculative_fetch, 0x14),
278 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_addr_hazard, 0x15),
279 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_id_hazard, 0x16),
280 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_tt_full, 0x17),
281 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_barrier_hazard, 0x18),
282 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_barrier_hazard, 0x19),
283 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_tt_full, 0x1A),
284 /* Special event for cycles counter */
285 CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(cycles, 0xff),
Mark Rutland5e442eb2016-02-23 10:49:43 +0000286 NULL
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100287};
288
Mark Rutland5e442eb2016-02-23 10:49:43 +0000289static struct attribute *cci400_r1_pmu_event_attrs[] = {
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100290 /* Slave events */
291 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_any, 0x0),
292 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_device, 0x01),
293 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_normal_or_nonshareable, 0x2),
294 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_inner_or_outershareable, 0x3),
295 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maintenance, 0x4),
296 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_mem_barrier, 0x5),
297 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_sync_barrier, 0x6),
298 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
299 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg_sync, 0x8),
300 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_tt_full, 0x9),
301 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_last_hs_snoop, 0xA),
302 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall_rvalids_h_rready_l, 0xB),
303 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_any, 0xC),
304 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_device, 0xD),
305 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_normal_or_nonshareable, 0xE),
306 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_inner_or_outershare_wback_wclean, 0xF),
307 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_unique, 0x10),
308 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_line_unique, 0x11),
309 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_evict, 0x12),
310 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall_tt_full, 0x13),
311 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_slave_id_hazard, 0x14),
312 /* Master events */
313 CCI_EVENT_EXT_ATTR_ENTRY(mi_retry_speculative_fetch, 0x0),
314 CCI_EVENT_EXT_ATTR_ENTRY(mi_stall_cycle_addr_hazard, 0x1),
315 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_master_id_hazard, 0x2),
316 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_hi_prio_rtq_full, 0x3),
317 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_barrier_hazard, 0x4),
318 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_barrier_hazard, 0x5),
319 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_wtq_full, 0x6),
320 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_low_prio_rtq_full, 0x7),
321 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_mid_prio_rtq_full, 0x8),
322 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn0, 0x9),
323 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn1, 0xA),
324 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn2, 0xB),
325 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn3, 0xC),
326 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn0, 0xD),
327 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn1, 0xE),
328 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn2, 0xF),
329 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn3, 0x10),
330 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_unique_or_line_unique_addr_hazard, 0x11),
331 /* Special event for cycles counter */
332 CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(cycles, 0xff),
Mark Rutland5e442eb2016-02-23 10:49:43 +0000333 NULL
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100334};
335
336static ssize_t cci400_pmu_cycle_event_show(struct device *dev,
337 struct device_attribute *attr, char *buf)
338{
339 struct dev_ext_attribute *eattr = container_of(attr,
340 struct dev_ext_attribute, attr);
341 return snprintf(buf, PAGE_SIZE, "config=0x%lx\n", (unsigned long)eattr->var);
342}
343
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100344static int cci400_get_event_idx(struct cci_pmu *cci_pmu,
345 struct cci_pmu_hw_events *hw,
346 unsigned long cci_event)
347{
348 int idx;
349
350 /* cycles event idx is fixed */
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100351 if (cci_event == CCI400_PMU_CYCLES) {
352 if (test_and_set_bit(CCI400_PMU_CYCLE_CNTR_IDX, hw->used_mask))
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100353 return -EAGAIN;
354
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100355 return CCI400_PMU_CYCLE_CNTR_IDX;
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100356 }
357
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100358 for (idx = CCI400_PMU_CNTR0_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); ++idx)
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100359 if (!test_and_set_bit(idx, hw->used_mask))
360 return idx;
361
362 /* No counters available */
363 return -EAGAIN;
364}
365
366static int cci400_validate_hw_event(struct cci_pmu *cci_pmu, unsigned long hw_event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100367{
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100368 u8 ev_source = CCI400_PMU_EVENT_SOURCE(hw_event);
369 u8 ev_code = CCI400_PMU_EVENT_CODE(hw_event);
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000370 int if_type;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100371
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100372 if (hw_event & ~CCI400_PMU_EVENT_MASK)
Suzuki K. Poulose874c5712015-03-18 12:24:42 +0000373 return -ENOENT;
374
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100375 if (hw_event == CCI400_PMU_CYCLES)
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100376 return hw_event;
377
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100378 switch (ev_source) {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100379 case CCI400_PORT_S0:
380 case CCI400_PORT_S1:
381 case CCI400_PORT_S2:
382 case CCI400_PORT_S3:
383 case CCI400_PORT_S4:
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100384 /* Slave Interface */
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000385 if_type = CCI_IF_SLAVE;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100386 break;
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100387 case CCI400_PORT_M0:
388 case CCI400_PORT_M1:
389 case CCI400_PORT_M2:
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100390 /* Master Interface */
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000391 if_type = CCI_IF_MASTER;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100392 break;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000393 default:
394 return -ENOENT;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100395 }
396
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100397 if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
398 ev_code <= cci_pmu->model->event_ranges[if_type].max)
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000399 return hw_event;
400
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100401 return -ENOENT;
402}
403
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100404static int probe_cci400_revision(void)
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000405{
406 int rev;
407 rev = readl_relaxed(cci_ctrl_base + CCI_PID2) & CCI_PID2_REV_MASK;
408 rev >>= CCI_PID2_REV_SHIFT;
409
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100410 if (rev < CCI400_R1_PX)
411 return CCI400_R0;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000412 else
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100413 return CCI400_R1;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000414}
415
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000416static const struct cci_pmu_model *probe_cci_model(struct platform_device *pdev)
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000417{
Suzuki K. Poulose772742a2015-03-18 12:24:40 +0000418 if (platform_has_secure_cci_access())
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100419 return &cci_pmu_models[probe_cci400_revision()];
Suzuki K. Poulose772742a2015-03-18 12:24:40 +0000420 return NULL;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000421}
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100422#else /* !CONFIG_ARM_CCI400_PMU */
423static inline struct cci_pmu_model *probe_cci_model(struct platform_device *pdev)
424{
425 return NULL;
426}
427#endif /* CONFIG_ARM_CCI400_PMU */
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000428
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000429#ifdef CONFIG_ARM_CCI5xx_PMU
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100430
431/*
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000432 * CCI5xx PMU event id is an 9-bit value made of two parts.
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100433 * bits [8:5] - Source for the event
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100434 * bits [4:0] - Event code (specific to type of interface)
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000435 *
436 *
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100437 */
438
439/* Port ids */
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000440#define CCI5xx_PORT_S0 0x0
441#define CCI5xx_PORT_S1 0x1
442#define CCI5xx_PORT_S2 0x2
443#define CCI5xx_PORT_S3 0x3
444#define CCI5xx_PORT_S4 0x4
445#define CCI5xx_PORT_S5 0x5
446#define CCI5xx_PORT_S6 0x6
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100447
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000448#define CCI5xx_PORT_M0 0x8
449#define CCI5xx_PORT_M1 0x9
450#define CCI5xx_PORT_M2 0xa
451#define CCI5xx_PORT_M3 0xb
452#define CCI5xx_PORT_M4 0xc
453#define CCI5xx_PORT_M5 0xd
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100454
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000455#define CCI5xx_PORT_GLOBAL 0xf
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100456
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000457#define CCI5xx_PMU_EVENT_MASK 0x1ffUL
458#define CCI5xx_PMU_EVENT_SOURCE_SHIFT 0x5
459#define CCI5xx_PMU_EVENT_SOURCE_MASK 0xf
460#define CCI5xx_PMU_EVENT_CODE_SHIFT 0x0
461#define CCI5xx_PMU_EVENT_CODE_MASK 0x1f
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100462
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000463#define CCI5xx_PMU_EVENT_SOURCE(event) \
464 ((event >> CCI5xx_PMU_EVENT_SOURCE_SHIFT) & CCI5xx_PMU_EVENT_SOURCE_MASK)
465#define CCI5xx_PMU_EVENT_CODE(event) \
466 ((event >> CCI5xx_PMU_EVENT_CODE_SHIFT) & CCI5xx_PMU_EVENT_CODE_MASK)
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100467
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000468#define CCI5xx_SLAVE_PORT_MIN_EV 0x00
469#define CCI5xx_SLAVE_PORT_MAX_EV 0x1f
470#define CCI5xx_MASTER_PORT_MIN_EV 0x00
471#define CCI5xx_MASTER_PORT_MAX_EV 0x06
472#define CCI5xx_GLOBAL_PORT_MIN_EV 0x00
473#define CCI5xx_GLOBAL_PORT_MAX_EV 0x0f
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100474
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100475
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000476#define CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(_name, _config) \
477 CCI_EXT_ATTR_ENTRY(_name, cci5xx_pmu_global_event_show, \
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100478 (unsigned long) _config)
479
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000480static ssize_t cci5xx_pmu_global_event_show(struct device *dev,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100481 struct device_attribute *attr, char *buf);
482
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000483static struct attribute *cci5xx_pmu_format_attrs[] = {
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100484 CCI_FORMAT_EXT_ATTR_ENTRY(event, "config:0-4"),
485 CCI_FORMAT_EXT_ATTR_ENTRY(source, "config:5-8"),
Mark Rutland5e442eb2016-02-23 10:49:43 +0000486 NULL,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100487};
488
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000489static struct attribute *cci5xx_pmu_event_attrs[] = {
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100490 /* Slave events */
491 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_arvalid, 0x0),
492 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_dev, 0x1),
493 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_nonshareable, 0x2),
494 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_shareable_non_alloc, 0x3),
495 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_shareable_alloc, 0x4),
496 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_invalidate, 0x5),
497 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maint, 0x6),
498 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
499 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_rval, 0x8),
500 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_rlast_snoop, 0x9),
501 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_awalid, 0xA),
502 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_dev, 0xB),
503 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_non_shareable, 0xC),
504 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wb, 0xD),
505 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wlu, 0xE),
506 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wunique, 0xF),
507 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_evict, 0x10),
508 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_wrevict, 0x11),
509 CCI_EVENT_EXT_ATTR_ENTRY(si_w_data_beat, 0x12),
510 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_acvalid, 0x13),
511 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_read, 0x14),
512 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_clean, 0x15),
513 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_data_transfer_low, 0x16),
514 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_arvalid, 0x17),
515 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall, 0x18),
516 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall, 0x19),
517 CCI_EVENT_EXT_ATTR_ENTRY(si_w_data_stall, 0x1A),
518 CCI_EVENT_EXT_ATTR_ENTRY(si_w_resp_stall, 0x1B),
519 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_stall, 0x1C),
520 CCI_EVENT_EXT_ATTR_ENTRY(si_s_data_stall, 0x1D),
521 CCI_EVENT_EXT_ATTR_ENTRY(si_rq_stall_ot_limit, 0x1E),
522 CCI_EVENT_EXT_ATTR_ENTRY(si_r_stall_arbit, 0x1F),
523
524 /* Master events */
525 CCI_EVENT_EXT_ATTR_ENTRY(mi_r_data_beat_any, 0x0),
526 CCI_EVENT_EXT_ATTR_ENTRY(mi_w_data_beat_any, 0x1),
527 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall, 0x2),
528 CCI_EVENT_EXT_ATTR_ENTRY(mi_r_data_stall, 0x3),
529 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall, 0x4),
530 CCI_EVENT_EXT_ATTR_ENTRY(mi_w_data_stall, 0x5),
531 CCI_EVENT_EXT_ATTR_ENTRY(mi_w_resp_stall, 0x6),
532
533 /* Global events */
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000534 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_0_1, 0x0),
535 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_2_3, 0x1),
536 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_4_5, 0x2),
537 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_6_7, 0x3),
538 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_0_1, 0x4),
539 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_2_3, 0x5),
540 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_4_5, 0x6),
541 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_6_7, 0x7),
542 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_back_invalidation, 0x8),
543 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_stall_alloc_busy, 0x9),
544 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_stall_tt_full, 0xA),
545 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_wrq, 0xB),
546 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_cd_hs, 0xC),
547 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_rq_stall_addr_hazard, 0xD),
548 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snopp_rq_stall_tt_full, 0xE),
549 CCI5xx_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_rq_tzmp1_prot, 0xF),
Mark Rutland5e442eb2016-02-23 10:49:43 +0000550 NULL
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100551};
552
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000553static ssize_t cci5xx_pmu_global_event_show(struct device *dev,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100554 struct device_attribute *attr, char *buf)
555{
556 struct dev_ext_attribute *eattr = container_of(attr,
557 struct dev_ext_attribute, attr);
558 /* Global events have single fixed source code */
559 return snprintf(buf, PAGE_SIZE, "event=0x%lx,source=0x%x\n",
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000560 (unsigned long)eattr->var, CCI5xx_PORT_GLOBAL);
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100561}
562
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000563/*
564 * CCI500 provides 8 independent event counters that can count
565 * any of the events available.
566 * CCI500 PMU event source ids
567 * 0x0-0x6 - Slave interfaces
568 * 0x8-0xD - Master interfaces
569 * 0xf - Global Events
570 * 0x7,0xe - Reserved
571 */
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100572static int cci500_validate_hw_event(struct cci_pmu *cci_pmu,
573 unsigned long hw_event)
574{
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000575 u32 ev_source = CCI5xx_PMU_EVENT_SOURCE(hw_event);
576 u32 ev_code = CCI5xx_PMU_EVENT_CODE(hw_event);
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100577 int if_type;
578
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000579 if (hw_event & ~CCI5xx_PMU_EVENT_MASK)
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100580 return -ENOENT;
581
582 switch (ev_source) {
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000583 case CCI5xx_PORT_S0:
584 case CCI5xx_PORT_S1:
585 case CCI5xx_PORT_S2:
586 case CCI5xx_PORT_S3:
587 case CCI5xx_PORT_S4:
588 case CCI5xx_PORT_S5:
589 case CCI5xx_PORT_S6:
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100590 if_type = CCI_IF_SLAVE;
591 break;
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000592 case CCI5xx_PORT_M0:
593 case CCI5xx_PORT_M1:
594 case CCI5xx_PORT_M2:
595 case CCI5xx_PORT_M3:
596 case CCI5xx_PORT_M4:
597 case CCI5xx_PORT_M5:
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100598 if_type = CCI_IF_MASTER;
599 break;
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000600 case CCI5xx_PORT_GLOBAL:
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100601 if_type = CCI_IF_GLOBAL;
602 break;
603 default:
604 return -ENOENT;
605 }
606
607 if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
608 ev_code <= cci_pmu->model->event_ranges[if_type].max)
609 return hw_event;
610
611 return -ENOENT;
612}
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000613
614#endif /* CONFIG_ARM_CCI5xx_PMU */
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100615
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000616/*
617 * Program the CCI PMU counters which have PERF_HES_ARCH set
618 * with the event period and mark them ready before we enable
619 * PMU.
620 */
621void cci_pmu_sync_counters(struct cci_pmu *cci_pmu)
622{
623 int i;
624 struct cci_pmu_hw_events *cci_hw = &cci_pmu->hw_events;
625
626 DECLARE_BITMAP(mask, cci_pmu->num_cntrs);
627
628 bitmap_zero(mask, cci_pmu->num_cntrs);
629 for_each_set_bit(i, cci_pmu->hw_events.used_mask, cci_pmu->num_cntrs) {
630 struct perf_event *event = cci_hw->events[i];
631
632 if (WARN_ON(!event))
633 continue;
634
635 /* Leave the events which are not counting */
636 if (event->hw.state & PERF_HES_STOPPED)
637 continue;
638 if (event->hw.state & PERF_HES_ARCH) {
639 set_bit(i, mask);
640 event->hw.state &= ~PERF_HES_ARCH;
641 }
642 }
643
644 pmu_write_counters(cci_pmu, mask);
645}
646
Suzuki K Poulosea077c522016-02-23 10:49:46 +0000647/* Should be called with cci_pmu->hw_events->pmu_lock held */
Suzuki K Poulose11300022016-02-23 10:49:51 +0000648static void __cci_pmu_enable_nosync(struct cci_pmu *cci_pmu)
Suzuki K Poulosea077c522016-02-23 10:49:46 +0000649{
650 u32 val;
651
652 /* Enable all the PMU counters. */
653 val = readl_relaxed(cci_ctrl_base + CCI_PMCR) | CCI_PMCR_CEN;
654 writel(val, cci_ctrl_base + CCI_PMCR);
655}
656
657/* Should be called with cci_pmu->hw_events->pmu_lock held */
Suzuki K Poulose11300022016-02-23 10:49:51 +0000658static void __cci_pmu_enable_sync(struct cci_pmu *cci_pmu)
659{
660 cci_pmu_sync_counters(cci_pmu);
661 __cci_pmu_enable_nosync(cci_pmu);
662}
663
664/* Should be called with cci_pmu->hw_events->pmu_lock held */
Suzuki K Poulosea077c522016-02-23 10:49:46 +0000665static void __cci_pmu_disable(void)
666{
667 u32 val;
668
669 /* Disable all the PMU counters. */
670 val = readl_relaxed(cci_ctrl_base + CCI_PMCR) & ~CCI_PMCR_CEN;
671 writel(val, cci_ctrl_base + CCI_PMCR);
672}
673
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100674static ssize_t cci_pmu_format_show(struct device *dev,
675 struct device_attribute *attr, char *buf)
676{
677 struct dev_ext_attribute *eattr = container_of(attr,
678 struct dev_ext_attribute, attr);
679 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)eattr->var);
680}
681
682static ssize_t cci_pmu_event_show(struct device *dev,
683 struct device_attribute *attr, char *buf)
684{
685 struct dev_ext_attribute *eattr = container_of(attr,
686 struct dev_ext_attribute, attr);
687 /* source parameter is mandatory for normal PMU events */
688 return snprintf(buf, PAGE_SIZE, "source=?,event=0x%lx\n",
689 (unsigned long)eattr->var);
690}
691
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100692static int pmu_is_valid_counter(struct cci_pmu *cci_pmu, int idx)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100693{
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100694 return 0 <= idx && idx <= CCI_PMU_CNTR_LAST(cci_pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100695}
696
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100697static u32 pmu_read_register(struct cci_pmu *cci_pmu, int idx, unsigned int offset)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100698{
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100699 return readl_relaxed(cci_pmu->base +
700 CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100701}
702
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100703static void pmu_write_register(struct cci_pmu *cci_pmu, u32 value,
704 int idx, unsigned int offset)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100705{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100706 return writel_relaxed(value, cci_pmu->base +
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100707 CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100708}
709
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100710static void pmu_disable_counter(struct cci_pmu *cci_pmu, int idx)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100711{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100712 pmu_write_register(cci_pmu, 0, idx, CCI_PMU_CNTR_CTRL);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100713}
714
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100715static void pmu_enable_counter(struct cci_pmu *cci_pmu, int idx)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100716{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100717 pmu_write_register(cci_pmu, 1, idx, CCI_PMU_CNTR_CTRL);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100718}
719
Suzuki K Poulose1ce63112016-02-23 10:49:49 +0000720static bool __maybe_unused
721pmu_counter_is_enabled(struct cci_pmu *cci_pmu, int idx)
722{
723 return (pmu_read_register(cci_pmu, idx, CCI_PMU_CNTR_CTRL) & 0x1) != 0;
724}
725
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100726static void pmu_set_event(struct cci_pmu *cci_pmu, int idx, unsigned long event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100727{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100728 pmu_write_register(cci_pmu, event, idx, CCI_PMU_EVT_SEL);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100729}
730
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100731/*
Suzuki K Poulosecea16f82016-02-23 10:49:50 +0000732 * For all counters on the CCI-PMU, disable any 'enabled' counters,
733 * saving the changed counters in the mask, so that we can restore
734 * it later using pmu_restore_counters. The mask is private to the
735 * caller. We cannot rely on the used_mask maintained by the CCI_PMU
736 * as it only tells us if the counter is assigned to perf_event or not.
737 * The state of the perf_event cannot be locked by the PMU layer, hence
738 * we check the individual counter status (which can be locked by
739 * cci_pm->hw_events->pmu_lock).
740 *
741 * @mask should be initialised to empty by the caller.
742 */
743static void __maybe_unused
744pmu_save_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
745{
746 int i;
747
748 for (i = 0; i < cci_pmu->num_cntrs; i++) {
749 if (pmu_counter_is_enabled(cci_pmu, i)) {
750 set_bit(i, mask);
751 pmu_disable_counter(cci_pmu, i);
752 }
753 }
754}
755
756/*
757 * Restore the status of the counters. Reversal of the pmu_save_counters().
758 * For each counter set in the mask, enable the counter back.
759 */
760static void __maybe_unused
761pmu_restore_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
762{
763 int i;
764
765 for_each_set_bit(i, mask, cci_pmu->num_cntrs)
766 pmu_enable_counter(cci_pmu, i);
767}
768
769/*
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100770 * Returns the number of programmable counters actually implemented
771 * by the cci
772 */
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100773static u32 pmu_get_max_counters(void)
774{
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100775 return (readl_relaxed(cci_ctrl_base + CCI_PMCR) &
776 CCI_PMCR_NCNT_MASK) >> CCI_PMCR_NCNT_SHIFT;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100777}
778
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100779static int pmu_get_event_idx(struct cci_pmu_hw_events *hw, struct perf_event *event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100780{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100781 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100782 unsigned long cci_event = event->hw.config_base;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100783 int idx;
784
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100785 if (cci_pmu->model->get_event_idx)
786 return cci_pmu->model->get_event_idx(cci_pmu, hw, cci_event);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100787
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100788 /* Generic code to find an unused idx from the mask */
789 for(idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100790 if (!test_and_set_bit(idx, hw->used_mask))
791 return idx;
792
793 /* No counters available */
794 return -EAGAIN;
795}
796
797static int pmu_map_event(struct perf_event *event)
798{
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100799 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100800
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100801 if (event->attr.type < PERF_TYPE_MAX ||
802 !cci_pmu->model->validate_hw_event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100803 return -ENOENT;
804
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100805 return cci_pmu->model->validate_hw_event(cci_pmu, event->attr.config);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100806}
807
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100808static int pmu_request_irq(struct cci_pmu *cci_pmu, irq_handler_t handler)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100809{
810 int i;
811 struct platform_device *pmu_device = cci_pmu->plat_device;
812
813 if (unlikely(!pmu_device))
814 return -ENODEV;
815
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100816 if (cci_pmu->nr_irqs < 1) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100817 dev_err(&pmu_device->dev, "no irqs for CCI PMUs defined\n");
818 return -ENODEV;
819 }
820
821 /*
822 * Register all available CCI PMU interrupts. In the interrupt handler
823 * we iterate over the counters checking for interrupt source (the
824 * overflowing counter) and clear it.
825 *
826 * This should allow handling of non-unique interrupt for the counters.
827 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100828 for (i = 0; i < cci_pmu->nr_irqs; i++) {
829 int err = request_irq(cci_pmu->irqs[i], handler, IRQF_SHARED,
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100830 "arm-cci-pmu", cci_pmu);
831 if (err) {
832 dev_err(&pmu_device->dev, "unable to request IRQ%d for ARM CCI PMU counters\n",
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100833 cci_pmu->irqs[i]);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100834 return err;
835 }
836
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100837 set_bit(i, &cci_pmu->active_irqs);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100838 }
839
840 return 0;
841}
842
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100843static void pmu_free_irq(struct cci_pmu *cci_pmu)
844{
845 int i;
846
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100847 for (i = 0; i < cci_pmu->nr_irqs; i++) {
848 if (!test_and_clear_bit(i, &cci_pmu->active_irqs))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100849 continue;
850
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100851 free_irq(cci_pmu->irqs[i], cci_pmu);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100852 }
853}
854
855static u32 pmu_read_counter(struct perf_event *event)
856{
857 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
858 struct hw_perf_event *hw_counter = &event->hw;
859 int idx = hw_counter->idx;
860 u32 value;
861
862 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
863 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
864 return 0;
865 }
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100866 value = pmu_read_register(cci_pmu, idx, CCI_PMU_CNTR);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100867
868 return value;
869}
870
Suzuki K Poulosec8bc2b12016-02-23 10:49:48 +0000871static void pmu_write_counter(struct cci_pmu *cci_pmu, u32 value, int idx)
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100872{
Suzuki K Poulosec8bc2b12016-02-23 10:49:48 +0000873 pmu_write_register(cci_pmu, value, idx, CCI_PMU_CNTR);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100874}
875
Suzuki K Poulosefff3f1a2016-02-23 10:49:52 +0000876static void __pmu_write_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
Suzuki K Poulosea53eb5c2016-02-23 10:49:45 +0000877{
878 int i;
879 struct cci_pmu_hw_events *cci_hw = &cci_pmu->hw_events;
880
881 for_each_set_bit(i, mask, cci_pmu->num_cntrs) {
882 struct perf_event *event = cci_hw->events[i];
883
884 if (WARN_ON(!event))
885 continue;
Suzuki K Poulosec8bc2b12016-02-23 10:49:48 +0000886 pmu_write_counter(cci_pmu, local64_read(&event->hw.prev_count), i);
Suzuki K Poulosea53eb5c2016-02-23 10:49:45 +0000887 }
888}
889
Suzuki K Poulosefff3f1a2016-02-23 10:49:52 +0000890static void pmu_write_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
891{
892 if (cci_pmu->model->write_counters)
893 cci_pmu->model->write_counters(cci_pmu, mask);
894 else
895 __pmu_write_counters(cci_pmu, mask);
896}
897
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000898#ifdef CONFIG_ARM_CCI5xx_PMU
Suzuki K Poulosea445fcc2016-02-23 10:49:53 +0000899
900/*
901 * CCI-500 has advanced power saving policies, which could gate the
902 * clocks to the PMU counters, which makes the writes to them ineffective.
903 * The only way to write to those counters is when the global counters
904 * are enabled and the particular counter is enabled.
905 *
906 * So we do the following :
907 *
908 * 1) Disable all the PMU counters, saving their current state
909 * 2) Enable the global PMU profiling, now that all counters are
910 * disabled.
911 *
912 * For each counter to be programmed, repeat steps 3-7:
913 *
914 * 3) Write an invalid event code to the event control register for the
915 counter, so that the counters are not modified.
916 * 4) Enable the counter control for the counter.
917 * 5) Set the counter value
918 * 6) Disable the counter
919 * 7) Restore the event in the target counter
920 *
921 * 8) Disable the global PMU.
922 * 9) Restore the status of the rest of the counters.
923 *
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000924 * We choose an event which for CCI-5xx is guaranteed not to count.
Suzuki K Poulosea445fcc2016-02-23 10:49:53 +0000925 * We use the highest possible event code (0x1f) for the master interface 0.
926 */
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000927#define CCI5xx_INVALID_EVENT ((CCI5xx_PORT_M0 << CCI5xx_PMU_EVENT_SOURCE_SHIFT) | \
928 (CCI5xx_PMU_EVENT_CODE_MASK << CCI5xx_PMU_EVENT_CODE_SHIFT))
929static void cci5xx_pmu_write_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
Suzuki K Poulosea445fcc2016-02-23 10:49:53 +0000930{
931 int i;
932 DECLARE_BITMAP(saved_mask, cci_pmu->num_cntrs);
933
934 bitmap_zero(saved_mask, cci_pmu->num_cntrs);
935 pmu_save_counters(cci_pmu, saved_mask);
936
937 /*
938 * Now that all the counters are disabled, we can safely turn the PMU on,
939 * without syncing the status of the counters
940 */
941 __cci_pmu_enable_nosync(cci_pmu);
942
943 for_each_set_bit(i, mask, cci_pmu->num_cntrs) {
944 struct perf_event *event = cci_pmu->hw_events.events[i];
945
946 if (WARN_ON(!event))
947 continue;
948
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000949 pmu_set_event(cci_pmu, i, CCI5xx_INVALID_EVENT);
Suzuki K Poulosea445fcc2016-02-23 10:49:53 +0000950 pmu_enable_counter(cci_pmu, i);
951 pmu_write_counter(cci_pmu, local64_read(&event->hw.prev_count), i);
952 pmu_disable_counter(cci_pmu, i);
953 pmu_set_event(cci_pmu, i, event->hw.config_base);
954 }
955
956 __cci_pmu_disable();
957
958 pmu_restore_counters(cci_pmu, saved_mask);
959}
960
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +0000961#endif /* CONFIG_ARM_CCI5xx_PMU */
Suzuki K Poulosea445fcc2016-02-23 10:49:53 +0000962
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100963static u64 pmu_event_update(struct perf_event *event)
964{
965 struct hw_perf_event *hwc = &event->hw;
966 u64 delta, prev_raw_count, new_raw_count;
967
968 do {
969 prev_raw_count = local64_read(&hwc->prev_count);
970 new_raw_count = pmu_read_counter(event);
971 } while (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
972 new_raw_count) != prev_raw_count);
973
974 delta = (new_raw_count - prev_raw_count) & CCI_PMU_CNTR_MASK;
975
976 local64_add(delta, &event->count);
977
978 return new_raw_count;
979}
980
981static void pmu_read(struct perf_event *event)
982{
983 pmu_event_update(event);
984}
985
986void pmu_event_set_period(struct perf_event *event)
987{
988 struct hw_perf_event *hwc = &event->hw;
989 /*
990 * The CCI PMU counters have a period of 2^32. To account for the
991 * possiblity of extreme interrupt latency we program for a period of
992 * half that. Hopefully we can handle the interrupt before another 2^31
993 * events occur and the counter overtakes its previous value.
994 */
995 u64 val = 1ULL << 31;
996 local64_set(&hwc->prev_count, val);
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000997
998 /*
999 * CCI PMU uses PERF_HES_ARCH to keep track of the counters, whose
1000 * values needs to be sync-ed with the s/w state before the PMU is
1001 * enabled.
1002 * Mark this counter for sync.
1003 */
1004 hwc->state |= PERF_HES_ARCH;
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001005}
1006
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001007static irqreturn_t pmu_handle_irq(int irq_num, void *dev)
1008{
1009 unsigned long flags;
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001010 struct cci_pmu *cci_pmu = dev;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001011 struct cci_pmu_hw_events *events = &cci_pmu->hw_events;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001012 int idx, handled = IRQ_NONE;
1013
1014 raw_spin_lock_irqsave(&events->pmu_lock, flags);
Suzuki K Poulosec66eea52016-02-23 10:49:47 +00001015
1016 /* Disable the PMU while we walk through the counters */
1017 __cci_pmu_disable();
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001018 /*
1019 * Iterate over counters and update the corresponding perf events.
1020 * This should work regardless of whether we have per-counter overflow
1021 * interrupt or a combined overflow interrupt.
1022 */
Suzuki K. Poulose31216292015-05-26 10:53:13 +01001023 for (idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001024 struct perf_event *event = events->events[idx];
1025 struct hw_perf_event *hw_counter;
1026
1027 if (!event)
1028 continue;
1029
1030 hw_counter = &event->hw;
1031
1032 /* Did this counter overflow? */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001033 if (!(pmu_read_register(cci_pmu, idx, CCI_PMU_OVRFLW) &
Himangi Saraogifc5130d2014-07-30 11:37:35 +01001034 CCI_PMU_OVRFLW_FLAG))
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001035 continue;
1036
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001037 pmu_write_register(cci_pmu, CCI_PMU_OVRFLW_FLAG, idx,
1038 CCI_PMU_OVRFLW);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001039
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001040 pmu_event_update(event);
1041 pmu_event_set_period(event);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001042 handled = IRQ_HANDLED;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001043 }
Suzuki K Poulosec66eea52016-02-23 10:49:47 +00001044
1045 /* Enable the PMU and sync possibly overflowed counters */
Suzuki K Poulose11300022016-02-23 10:49:51 +00001046 __cci_pmu_enable_sync(cci_pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001047 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
1048
1049 return IRQ_RETVAL(handled);
1050}
1051
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001052static int cci_pmu_get_hw(struct cci_pmu *cci_pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001053{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001054 int ret = pmu_request_irq(cci_pmu, pmu_handle_irq);
1055 if (ret) {
1056 pmu_free_irq(cci_pmu);
1057 return ret;
1058 }
1059 return 0;
1060}
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001061
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001062static void cci_pmu_put_hw(struct cci_pmu *cci_pmu)
1063{
1064 pmu_free_irq(cci_pmu);
1065}
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001066
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001067static void hw_perf_event_destroy(struct perf_event *event)
1068{
1069 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1070 atomic_t *active_events = &cci_pmu->active_events;
1071 struct mutex *reserve_mutex = &cci_pmu->reserve_mutex;
1072
1073 if (atomic_dec_and_mutex_lock(active_events, reserve_mutex)) {
1074 cci_pmu_put_hw(cci_pmu);
1075 mutex_unlock(reserve_mutex);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001076 }
1077}
1078
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001079static void cci_pmu_enable(struct pmu *pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001080{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001081 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
1082 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001083 int enabled = bitmap_weight(hw_events->used_mask, cci_pmu->num_cntrs);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001084 unsigned long flags;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001085
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001086 if (!enabled)
1087 return;
1088
1089 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
Suzuki K Poulose11300022016-02-23 10:49:51 +00001090 __cci_pmu_enable_sync(cci_pmu);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001091 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001092
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001093}
1094
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001095static void cci_pmu_disable(struct pmu *pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001096{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001097 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
1098 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001099 unsigned long flags;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001100
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001101 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
Suzuki K Poulosea077c522016-02-23 10:49:46 +00001102 __cci_pmu_disable();
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001103 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001104}
1105
Suzuki K. Poulose31216292015-05-26 10:53:13 +01001106/*
1107 * Check if the idx represents a non-programmable counter.
1108 * All the fixed event counters are mapped before the programmable
1109 * counters.
1110 */
1111static bool pmu_fixed_hw_idx(struct cci_pmu *cci_pmu, int idx)
1112{
1113 return (idx >= 0) && (idx < cci_pmu->model->fixed_hw_cntrs);
1114}
1115
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001116static void cci_pmu_start(struct perf_event *event, int pmu_flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001117{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001118 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1119 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
1120 struct hw_perf_event *hwc = &event->hw;
1121 int idx = hwc->idx;
1122 unsigned long flags;
1123
1124 /*
1125 * To handle interrupt latency, we always reprogram the period
1126 * regardlesss of PERF_EF_RELOAD.
1127 */
1128 if (pmu_flags & PERF_EF_RELOAD)
1129 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
1130
1131 hwc->state = 0;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001132
1133 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
1134 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001135 return;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001136 }
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001137
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001138 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
1139
Suzuki K. Poulose31216292015-05-26 10:53:13 +01001140 /* Configure the counter unless you are counting a fixed event */
1141 if (!pmu_fixed_hw_idx(cci_pmu, idx))
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001142 pmu_set_event(cci_pmu, idx, hwc->config_base);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001143
1144 pmu_event_set_period(event);
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001145 pmu_enable_counter(cci_pmu, idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001146
1147 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001148}
1149
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001150static void cci_pmu_stop(struct perf_event *event, int pmu_flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001151{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001152 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1153 struct hw_perf_event *hwc = &event->hw;
1154 int idx = hwc->idx;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001155
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001156 if (hwc->state & PERF_HES_STOPPED)
1157 return;
1158
1159 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001160 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001161 return;
1162 }
1163
1164 /*
1165 * We always reprogram the counter, so ignore PERF_EF_UPDATE. See
1166 * cci_pmu_start()
1167 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001168 pmu_disable_counter(cci_pmu, idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001169 pmu_event_update(event);
1170 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001171}
1172
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001173static int cci_pmu_add(struct perf_event *event, int flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001174{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001175 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1176 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
1177 struct hw_perf_event *hwc = &event->hw;
1178 int idx;
1179 int err = 0;
1180
1181 perf_pmu_disable(event->pmu);
1182
1183 /* If we don't have a space for the counter then finish early. */
1184 idx = pmu_get_event_idx(hw_events, event);
1185 if (idx < 0) {
1186 err = idx;
1187 goto out;
1188 }
1189
1190 event->hw.idx = idx;
1191 hw_events->events[idx] = event;
1192
1193 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
1194 if (flags & PERF_EF_START)
1195 cci_pmu_start(event, PERF_EF_RELOAD);
1196
1197 /* Propagate our changes to the userspace mapping. */
1198 perf_event_update_userpage(event);
1199
1200out:
1201 perf_pmu_enable(event->pmu);
1202 return err;
1203}
1204
1205static void cci_pmu_del(struct perf_event *event, int flags)
1206{
1207 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1208 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
1209 struct hw_perf_event *hwc = &event->hw;
1210 int idx = hwc->idx;
1211
1212 cci_pmu_stop(event, PERF_EF_UPDATE);
1213 hw_events->events[idx] = NULL;
1214 clear_bit(idx, hw_events->used_mask);
1215
1216 perf_event_update_userpage(event);
1217}
1218
1219static int
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001220validate_event(struct pmu *cci_pmu,
1221 struct cci_pmu_hw_events *hw_events,
1222 struct perf_event *event)
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001223{
1224 if (is_software_event(event))
1225 return 1;
1226
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001227 /*
1228 * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
1229 * core perf code won't check that the pmu->ctx == leader->ctx
1230 * until after pmu->event_init(event).
1231 */
1232 if (event->pmu != cci_pmu)
1233 return 0;
1234
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001235 if (event->state < PERF_EVENT_STATE_OFF)
1236 return 1;
1237
1238 if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
1239 return 1;
1240
1241 return pmu_get_event_idx(hw_events, event) >= 0;
1242}
1243
1244static int
1245validate_group(struct perf_event *event)
1246{
1247 struct perf_event *sibling, *leader = event->group_leader;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001248 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1249 unsigned long mask[BITS_TO_LONGS(cci_pmu->num_cntrs)];
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001250 struct cci_pmu_hw_events fake_pmu = {
1251 /*
1252 * Initialise the fake PMU. We only need to populate the
1253 * used_mask for the purposes of validation.
1254 */
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001255 .used_mask = mask,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001256 };
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001257 memset(mask, 0, BITS_TO_LONGS(cci_pmu->num_cntrs) * sizeof(unsigned long));
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001258
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001259 if (!validate_event(event->pmu, &fake_pmu, leader))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001260 return -EINVAL;
1261
1262 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001263 if (!validate_event(event->pmu, &fake_pmu, sibling))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001264 return -EINVAL;
1265 }
1266
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001267 if (!validate_event(event->pmu, &fake_pmu, event))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001268 return -EINVAL;
1269
1270 return 0;
1271}
1272
1273static int
1274__hw_perf_event_init(struct perf_event *event)
1275{
1276 struct hw_perf_event *hwc = &event->hw;
1277 int mapping;
1278
1279 mapping = pmu_map_event(event);
1280
1281 if (mapping < 0) {
1282 pr_debug("event %x:%llx not supported\n", event->attr.type,
1283 event->attr.config);
1284 return mapping;
1285 }
1286
1287 /*
1288 * We don't assign an index until we actually place the event onto
1289 * hardware. Use -1 to signify that we haven't decided where to put it
1290 * yet.
1291 */
1292 hwc->idx = -1;
1293 hwc->config_base = 0;
1294 hwc->config = 0;
1295 hwc->event_base = 0;
1296
1297 /*
1298 * Store the event encoding into the config_base field.
1299 */
1300 hwc->config_base |= (unsigned long)mapping;
1301
1302 /*
1303 * Limit the sample_period to half of the counter width. That way, the
1304 * new counter value is far less likely to overtake the previous one
1305 * unless you have some serious IRQ latency issues.
1306 */
1307 hwc->sample_period = CCI_PMU_CNTR_MASK >> 1;
1308 hwc->last_period = hwc->sample_period;
1309 local64_set(&hwc->period_left, hwc->sample_period);
1310
1311 if (event->group_leader != event) {
1312 if (validate_group(event) != 0)
1313 return -EINVAL;
1314 }
1315
1316 return 0;
1317}
1318
1319static int cci_pmu_event_init(struct perf_event *event)
1320{
1321 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1322 atomic_t *active_events = &cci_pmu->active_events;
1323 int err = 0;
1324 int cpu;
1325
1326 if (event->attr.type != event->pmu->type)
1327 return -ENOENT;
1328
1329 /* Shared by all CPUs, no meaningful state to sample */
1330 if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
1331 return -EOPNOTSUPP;
1332
1333 /* We have no filtering of any kind */
1334 if (event->attr.exclude_user ||
1335 event->attr.exclude_kernel ||
1336 event->attr.exclude_hv ||
1337 event->attr.exclude_idle ||
1338 event->attr.exclude_host ||
1339 event->attr.exclude_guest)
1340 return -EINVAL;
1341
1342 /*
1343 * Following the example set by other "uncore" PMUs, we accept any CPU
1344 * and rewrite its affinity dynamically rather than having perf core
1345 * handle cpu == -1 and pid == -1 for this case.
1346 *
1347 * The perf core will pin online CPUs for the duration of this call and
1348 * the event being installed into its context, so the PMU's CPU can't
1349 * change under our feet.
1350 */
1351 cpu = cpumask_first(&cci_pmu->cpus);
1352 if (event->cpu < 0 || cpu < 0)
1353 return -EINVAL;
1354 event->cpu = cpu;
1355
1356 event->destroy = hw_perf_event_destroy;
1357 if (!atomic_inc_not_zero(active_events)) {
1358 mutex_lock(&cci_pmu->reserve_mutex);
1359 if (atomic_read(active_events) == 0)
1360 err = cci_pmu_get_hw(cci_pmu);
1361 if (!err)
1362 atomic_inc(active_events);
1363 mutex_unlock(&cci_pmu->reserve_mutex);
1364 }
1365 if (err)
1366 return err;
1367
1368 err = __hw_perf_event_init(event);
1369 if (err)
1370 hw_perf_event_destroy(event);
1371
1372 return err;
1373}
1374
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001375static ssize_t pmu_cpumask_attr_show(struct device *dev,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001376 struct device_attribute *attr, char *buf)
1377{
Mark Rutland5e442eb2016-02-23 10:49:43 +00001378 struct pmu *pmu = dev_get_drvdata(dev);
1379 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001380
Tejun Heo660e5ec2015-02-13 14:37:20 -08001381 int n = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001382 cpumask_pr_args(&cci_pmu->cpus));
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001383 buf[n++] = '\n';
1384 buf[n] = '\0';
1385 return n;
1386}
1387
Mark Rutland5e442eb2016-02-23 10:49:43 +00001388static struct device_attribute pmu_cpumask_attr =
1389 __ATTR(cpumask, S_IRUGO, pmu_cpumask_attr_show, NULL);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001390
1391static struct attribute *pmu_attrs[] = {
Mark Rutland5e442eb2016-02-23 10:49:43 +00001392 &pmu_cpumask_attr.attr,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001393 NULL,
1394};
1395
1396static struct attribute_group pmu_attr_group = {
1397 .attrs = pmu_attrs,
1398};
1399
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001400static struct attribute_group pmu_format_attr_group = {
1401 .name = "format",
1402 .attrs = NULL, /* Filled in cci_pmu_init_attrs */
1403};
1404
1405static struct attribute_group pmu_event_attr_group = {
1406 .name = "events",
1407 .attrs = NULL, /* Filled in cci_pmu_init_attrs */
1408};
1409
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001410static const struct attribute_group *pmu_attr_groups[] = {
1411 &pmu_attr_group,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001412 &pmu_format_attr_group,
1413 &pmu_event_attr_group,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001414 NULL
1415};
1416
1417static int cci_pmu_init(struct cci_pmu *cci_pmu, struct platform_device *pdev)
1418{
Mark Rutland5e442eb2016-02-23 10:49:43 +00001419 const struct cci_pmu_model *model = cci_pmu->model;
1420 char *name = model->name;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001421 u32 num_cntrs;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001422
Mark Rutland5e442eb2016-02-23 10:49:43 +00001423 pmu_event_attr_group.attrs = model->event_attrs;
1424 pmu_format_attr_group.attrs = model->format_attrs;
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001425
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001426 cci_pmu->pmu = (struct pmu) {
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001427 .name = cci_pmu->model->name,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001428 .task_ctx_nr = perf_invalid_context,
1429 .pmu_enable = cci_pmu_enable,
1430 .pmu_disable = cci_pmu_disable,
1431 .event_init = cci_pmu_event_init,
1432 .add = cci_pmu_add,
1433 .del = cci_pmu_del,
1434 .start = cci_pmu_start,
1435 .stop = cci_pmu_stop,
1436 .read = pmu_read,
1437 .attr_groups = pmu_attr_groups,
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001438 };
1439
1440 cci_pmu->plat_device = pdev;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001441 num_cntrs = pmu_get_max_counters();
1442 if (num_cntrs > cci_pmu->model->num_hw_cntrs) {
1443 dev_warn(&pdev->dev,
1444 "PMU implements more counters(%d) than supported by"
1445 " the model(%d), truncated.",
1446 num_cntrs, cci_pmu->model->num_hw_cntrs);
1447 num_cntrs = cci_pmu->model->num_hw_cntrs;
1448 }
1449 cci_pmu->num_cntrs = num_cntrs + cci_pmu->model->fixed_hw_cntrs;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001450
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001451 return perf_pmu_register(&cci_pmu->pmu, name, -1);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001452}
1453
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001454static int cci_pmu_cpu_notifier(struct notifier_block *self,
1455 unsigned long action, void *hcpu)
1456{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001457 struct cci_pmu *cci_pmu = container_of(self,
1458 struct cci_pmu, cpu_nb);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001459 unsigned int cpu = (long)hcpu;
1460 unsigned int target;
1461
1462 switch (action & ~CPU_TASKS_FROZEN) {
1463 case CPU_DOWN_PREPARE:
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001464 if (!cpumask_test_and_clear_cpu(cpu, &cci_pmu->cpus))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001465 break;
1466 target = cpumask_any_but(cpu_online_mask, cpu);
Andrzej Hajda0f173802016-02-23 10:49:44 +00001467 if (target >= nr_cpu_ids) // UP, last CPU
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001468 break;
1469 /*
1470 * TODO: migrate context once core races on event->ctx have
1471 * been fixed.
1472 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001473 cpumask_set_cpu(target, &cci_pmu->cpus);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001474 default:
1475 break;
1476 }
1477
1478 return NOTIFY_OK;
1479}
1480
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001481static struct cci_pmu_model cci_pmu_models[] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001482#ifdef CONFIG_ARM_CCI400_PMU
1483 [CCI400_R0] = {
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001484 .name = "CCI_400",
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001485 .fixed_hw_cntrs = 1, /* Cycle counter */
1486 .num_hw_cntrs = 4,
1487 .cntr_size = SZ_4K,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001488 .format_attrs = cci400_pmu_format_attrs,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001489 .event_attrs = cci400_r0_pmu_event_attrs,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001490 .event_ranges = {
1491 [CCI_IF_SLAVE] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001492 CCI400_R0_SLAVE_PORT_MIN_EV,
1493 CCI400_R0_SLAVE_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001494 },
1495 [CCI_IF_MASTER] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001496 CCI400_R0_MASTER_PORT_MIN_EV,
1497 CCI400_R0_MASTER_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001498 },
1499 },
Suzuki K. Poulose31216292015-05-26 10:53:13 +01001500 .validate_hw_event = cci400_validate_hw_event,
1501 .get_event_idx = cci400_get_event_idx,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001502 },
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001503 [CCI400_R1] = {
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001504 .name = "CCI_400_r1",
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001505 .fixed_hw_cntrs = 1, /* Cycle counter */
1506 .num_hw_cntrs = 4,
1507 .cntr_size = SZ_4K,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001508 .format_attrs = cci400_pmu_format_attrs,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001509 .event_attrs = cci400_r1_pmu_event_attrs,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001510 .event_ranges = {
1511 [CCI_IF_SLAVE] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001512 CCI400_R1_SLAVE_PORT_MIN_EV,
1513 CCI400_R1_SLAVE_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001514 },
1515 [CCI_IF_MASTER] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001516 CCI400_R1_MASTER_PORT_MIN_EV,
1517 CCI400_R1_MASTER_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001518 },
1519 },
Suzuki K. Poulose31216292015-05-26 10:53:13 +01001520 .validate_hw_event = cci400_validate_hw_event,
1521 .get_event_idx = cci400_get_event_idx,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001522 },
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001523#endif
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +00001524#ifdef CONFIG_ARM_CCI5xx_PMU
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001525 [CCI500_R0] = {
1526 .name = "CCI_500",
1527 .fixed_hw_cntrs = 0,
1528 .num_hw_cntrs = 8,
1529 .cntr_size = SZ_64K,
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +00001530 .format_attrs = cci5xx_pmu_format_attrs,
1531 .event_attrs = cci5xx_pmu_event_attrs,
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001532 .event_ranges = {
1533 [CCI_IF_SLAVE] = {
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +00001534 CCI5xx_SLAVE_PORT_MIN_EV,
1535 CCI5xx_SLAVE_PORT_MAX_EV,
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001536 },
1537 [CCI_IF_MASTER] = {
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +00001538 CCI5xx_MASTER_PORT_MIN_EV,
1539 CCI5xx_MASTER_PORT_MAX_EV,
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001540 },
1541 [CCI_IF_GLOBAL] = {
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +00001542 CCI5xx_GLOBAL_PORT_MIN_EV,
1543 CCI5xx_GLOBAL_PORT_MAX_EV,
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001544 },
1545 },
1546 .validate_hw_event = cci500_validate_hw_event,
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +00001547 .write_counters = cci5xx_pmu_write_counters,
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001548 },
1549#endif
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001550};
1551
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001552static const struct of_device_id arm_cci_pmu_matches[] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001553#ifdef CONFIG_ARM_CCI400_PMU
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001554 {
1555 .compatible = "arm,cci-400-pmu",
Suzuki K. Poulose772742a2015-03-18 12:24:40 +00001556 .data = NULL,
1557 },
1558 {
1559 .compatible = "arm,cci-400-pmu,r0",
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001560 .data = &cci_pmu_models[CCI400_R0],
Suzuki K. Poulose772742a2015-03-18 12:24:40 +00001561 },
1562 {
1563 .compatible = "arm,cci-400-pmu,r1",
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001564 .data = &cci_pmu_models[CCI400_R1],
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001565 },
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001566#endif
Suzuki K Poulose3d2e8702016-02-23 10:49:54 +00001567#ifdef CONFIG_ARM_CCI5xx_PMU
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001568 {
1569 .compatible = "arm,cci-500-pmu,r0",
1570 .data = &cci_pmu_models[CCI500_R0],
1571 },
1572#endif
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001573 {},
1574};
1575
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001576static inline const struct cci_pmu_model *get_cci_model(struct platform_device *pdev)
1577{
1578 const struct of_device_id *match = of_match_node(arm_cci_pmu_matches,
1579 pdev->dev.of_node);
1580 if (!match)
1581 return NULL;
Suzuki K. Poulose772742a2015-03-18 12:24:40 +00001582 if (match->data)
1583 return match->data;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001584
Suzuki K. Poulose772742a2015-03-18 12:24:40 +00001585 dev_warn(&pdev->dev, "DEPRECATED compatible property,"
1586 "requires secure access to CCI registers");
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001587 return probe_cci_model(pdev);
1588}
1589
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001590static bool is_duplicate_irq(int irq, int *irqs, int nr_irqs)
1591{
1592 int i;
1593
1594 for (i = 0; i < nr_irqs; i++)
1595 if (irq == irqs[i])
1596 return true;
1597
1598 return false;
1599}
1600
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001601static struct cci_pmu *cci_pmu_alloc(struct platform_device *pdev)
1602{
1603 struct cci_pmu *cci_pmu;
1604 const struct cci_pmu_model *model;
1605
1606 /*
1607 * All allocations are devm_* hence we don't have to free
1608 * them explicitly on an error, as it would end up in driver
1609 * detach.
1610 */
1611 model = get_cci_model(pdev);
1612 if (!model) {
1613 dev_warn(&pdev->dev, "CCI PMU version not supported\n");
1614 return ERR_PTR(-ENODEV);
1615 }
1616
1617 cci_pmu = devm_kzalloc(&pdev->dev, sizeof(*cci_pmu), GFP_KERNEL);
1618 if (!cci_pmu)
1619 return ERR_PTR(-ENOMEM);
1620
1621 cci_pmu->model = model;
1622 cci_pmu->irqs = devm_kcalloc(&pdev->dev, CCI_PMU_MAX_HW_CNTRS(model),
1623 sizeof(*cci_pmu->irqs), GFP_KERNEL);
1624 if (!cci_pmu->irqs)
1625 return ERR_PTR(-ENOMEM);
1626 cci_pmu->hw_events.events = devm_kcalloc(&pdev->dev,
1627 CCI_PMU_MAX_HW_CNTRS(model),
1628 sizeof(*cci_pmu->hw_events.events),
1629 GFP_KERNEL);
1630 if (!cci_pmu->hw_events.events)
1631 return ERR_PTR(-ENOMEM);
1632 cci_pmu->hw_events.used_mask = devm_kcalloc(&pdev->dev,
1633 BITS_TO_LONGS(CCI_PMU_MAX_HW_CNTRS(model)),
1634 sizeof(*cci_pmu->hw_events.used_mask),
1635 GFP_KERNEL);
1636 if (!cci_pmu->hw_events.used_mask)
1637 return ERR_PTR(-ENOMEM);
1638
1639 return cci_pmu;
1640}
1641
1642
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001643static int cci_pmu_probe(struct platform_device *pdev)
1644{
1645 struct resource *res;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001646 struct cci_pmu *cci_pmu;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001647 int i, ret, irq;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001648
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001649 cci_pmu = cci_pmu_alloc(pdev);
1650 if (IS_ERR(cci_pmu))
1651 return PTR_ERR(cci_pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001652
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001653 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001654 cci_pmu->base = devm_ioremap_resource(&pdev->dev, res);
1655 if (IS_ERR(cci_pmu->base))
Wei Yongjunfee4f2c2013-09-22 06:04:23 +01001656 return -ENOMEM;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001657
1658 /*
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001659 * CCI PMU has one overflow interrupt per counter; but some may be tied
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001660 * together to a common interrupt.
1661 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001662 cci_pmu->nr_irqs = 0;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001663 for (i = 0; i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model); i++) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001664 irq = platform_get_irq(pdev, i);
1665 if (irq < 0)
1666 break;
1667
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001668 if (is_duplicate_irq(irq, cci_pmu->irqs, cci_pmu->nr_irqs))
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001669 continue;
1670
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001671 cci_pmu->irqs[cci_pmu->nr_irqs++] = irq;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001672 }
1673
1674 /*
1675 * Ensure that the device tree has as many interrupts as the number
1676 * of counters.
1677 */
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001678 if (i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model)) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001679 dev_warn(&pdev->dev, "In-correct number of interrupts: %d, should be %d\n",
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001680 i, CCI_PMU_MAX_HW_CNTRS(cci_pmu->model));
Wei Yongjunfee4f2c2013-09-22 06:04:23 +01001681 return -EINVAL;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001682 }
1683
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001684 raw_spin_lock_init(&cci_pmu->hw_events.pmu_lock);
1685 mutex_init(&cci_pmu->reserve_mutex);
1686 atomic_set(&cci_pmu->active_events, 0);
1687 cpumask_set_cpu(smp_processor_id(), &cci_pmu->cpus);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001688
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001689 cci_pmu->cpu_nb = (struct notifier_block) {
1690 .notifier_call = cci_pmu_cpu_notifier,
1691 /*
1692 * to migrate uncore events, our notifier should be executed
1693 * before perf core's notifier.
1694 */
1695 .priority = CPU_PRI_PERF + 1,
1696 };
1697
1698 ret = register_cpu_notifier(&cci_pmu->cpu_nb);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001699 if (ret)
1700 return ret;
1701
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001702 ret = cci_pmu_init(cci_pmu, pdev);
1703 if (ret) {
1704 unregister_cpu_notifier(&cci_pmu->cpu_nb);
Wei Yongjunfee4f2c2013-09-22 06:04:23 +01001705 return ret;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001706 }
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001707
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001708 pr_info("ARM %s PMU driver probed", cci_pmu->model->name);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001709 return 0;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001710}
1711
1712static int cci_platform_probe(struct platform_device *pdev)
1713{
1714 if (!cci_probed())
1715 return -ENODEV;
1716
1717 return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
1718}
1719
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001720static struct platform_driver cci_pmu_driver = {
1721 .driver = {
1722 .name = DRIVER_NAME_PMU,
1723 .of_match_table = arm_cci_pmu_matches,
1724 },
1725 .probe = cci_pmu_probe,
1726};
1727
1728static struct platform_driver cci_platform_driver = {
1729 .driver = {
1730 .name = DRIVER_NAME,
1731 .of_match_table = arm_cci_matches,
1732 },
1733 .probe = cci_platform_probe,
1734};
1735
1736static int __init cci_platform_init(void)
1737{
1738 int ret;
1739
1740 ret = platform_driver_register(&cci_pmu_driver);
1741 if (ret)
1742 return ret;
1743
1744 return platform_driver_register(&cci_platform_driver);
1745}
1746
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001747#else /* !CONFIG_ARM_CCI_PMU */
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001748
1749static int __init cci_platform_init(void)
1750{
1751 return 0;
1752}
1753
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001754#endif /* CONFIG_ARM_CCI_PMU */
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +00001755
1756#ifdef CONFIG_ARM_CCI400_PORT_CTRL
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001757
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001758#define CCI_PORT_CTRL 0x0
1759#define CCI_CTRL_STATUS 0xc
1760
1761#define CCI_ENABLE_SNOOP_REQ 0x1
1762#define CCI_ENABLE_DVM_REQ 0x2
1763#define CCI_ENABLE_REQ (CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ)
1764
1765enum cci_ace_port_type {
1766 ACE_INVALID_PORT = 0x0,
1767 ACE_PORT,
1768 ACE_LITE_PORT,
1769};
1770
1771struct cci_ace_port {
1772 void __iomem *base;
1773 unsigned long phys;
1774 enum cci_ace_port_type type;
1775 struct device_node *dn;
1776};
1777
1778static struct cci_ace_port *ports;
1779static unsigned int nb_cci_ports;
1780
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001781struct cpu_port {
1782 u64 mpidr;
1783 u32 port;
1784};
Nicolas Pitre62158f82013-05-21 23:34:41 -04001785
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001786/*
1787 * Use the port MSB as valid flag, shift can be made dynamic
1788 * by computing number of bits required for port indexes.
1789 * Code disabling CCI cpu ports runs with D-cache invalidated
1790 * and SCTLR bit clear so data accesses must be kept to a minimum
1791 * to improve performance; for now shift is left static to
1792 * avoid one more data access while disabling the CCI port.
1793 */
1794#define PORT_VALID_SHIFT 31
1795#define PORT_VALID (0x1 << PORT_VALID_SHIFT)
1796
1797static inline void init_cpu_port(struct cpu_port *port, u32 index, u64 mpidr)
1798{
1799 port->port = PORT_VALID | index;
1800 port->mpidr = mpidr;
1801}
1802
1803static inline bool cpu_port_is_valid(struct cpu_port *port)
1804{
1805 return !!(port->port & PORT_VALID);
1806}
1807
1808static inline bool cpu_port_match(struct cpu_port *port, u64 mpidr)
1809{
1810 return port->mpidr == (mpidr & MPIDR_HWID_BITMASK);
1811}
1812
1813static struct cpu_port cpu_port[NR_CPUS];
1814
1815/**
1816 * __cci_ace_get_port - Function to retrieve the port index connected to
1817 * a cpu or device.
1818 *
1819 * @dn: device node of the device to look-up
1820 * @type: port type
1821 *
1822 * Return value:
1823 * - CCI port index if success
1824 * - -ENODEV if failure
1825 */
1826static int __cci_ace_get_port(struct device_node *dn, int type)
1827{
1828 int i;
1829 bool ace_match;
1830 struct device_node *cci_portn;
1831
1832 cci_portn = of_parse_phandle(dn, "cci-control-port", 0);
1833 for (i = 0; i < nb_cci_ports; i++) {
1834 ace_match = ports[i].type == type;
1835 if (ace_match && cci_portn == ports[i].dn)
1836 return i;
1837 }
1838 return -ENODEV;
1839}
1840
1841int cci_ace_get_port(struct device_node *dn)
1842{
1843 return __cci_ace_get_port(dn, ACE_LITE_PORT);
1844}
1845EXPORT_SYMBOL_GPL(cci_ace_get_port);
1846
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001847static void cci_ace_init_ports(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001848{
Sudeep KarkadaNagesha78b4d6e2013-06-17 14:51:48 +01001849 int port, cpu;
1850 struct device_node *cpun;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001851
1852 /*
1853 * Port index look-up speeds up the function disabling ports by CPU,
1854 * since the logical to port index mapping is done once and does
1855 * not change after system boot.
1856 * The stashed index array is initialized for all possible CPUs
1857 * at probe time.
1858 */
Sudeep KarkadaNagesha78b4d6e2013-06-17 14:51:48 +01001859 for_each_possible_cpu(cpu) {
1860 /* too early to use cpu->of_node */
1861 cpun = of_get_cpu_node(cpu, NULL);
1862
1863 if (WARN(!cpun, "Missing cpu device node\n"))
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001864 continue;
1865
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001866 port = __cci_ace_get_port(cpun, ACE_PORT);
1867 if (port < 0)
1868 continue;
1869
1870 init_cpu_port(&cpu_port[cpu], port, cpu_logical_map(cpu));
1871 }
1872
1873 for_each_possible_cpu(cpu) {
1874 WARN(!cpu_port_is_valid(&cpu_port[cpu]),
1875 "CPU %u does not have an associated CCI port\n",
1876 cpu);
1877 }
1878}
1879/*
1880 * Functions to enable/disable a CCI interconnect slave port
1881 *
1882 * They are called by low-level power management code to disable slave
1883 * interfaces snoops and DVM broadcast.
1884 * Since they may execute with cache data allocation disabled and
1885 * after the caches have been cleaned and invalidated the functions provide
1886 * no explicit locking since they may run with D-cache disabled, so normal
1887 * cacheable kernel locks based on ldrex/strex may not work.
1888 * Locking has to be provided by BSP implementations to ensure proper
1889 * operations.
1890 */
1891
1892/**
1893 * cci_port_control() - function to control a CCI port
1894 *
1895 * @port: index of the port to setup
1896 * @enable: if true enables the port, if false disables it
1897 */
1898static void notrace cci_port_control(unsigned int port, bool enable)
1899{
1900 void __iomem *base = ports[port].base;
1901
1902 writel_relaxed(enable ? CCI_ENABLE_REQ : 0, base + CCI_PORT_CTRL);
1903 /*
1904 * This function is called from power down procedures
1905 * and must not execute any instruction that might
1906 * cause the processor to be put in a quiescent state
1907 * (eg wfi). Hence, cpu_relax() can not be added to this
1908 * read loop to optimize power, since it might hide possibly
1909 * disruptive operations.
1910 */
1911 while (readl_relaxed(cci_ctrl_base + CCI_CTRL_STATUS) & 0x1)
1912 ;
1913}
1914
1915/**
1916 * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
1917 * reference
1918 *
1919 * @mpidr: mpidr of the CPU whose CCI port should be disabled
1920 *
1921 * Disabling a CCI port for a CPU implies disabling the CCI port
1922 * controlling that CPU cluster. Code disabling CPU CCI ports
1923 * must make sure that the CPU running the code is the last active CPU
1924 * in the cluster ie all other CPUs are quiescent in a low power state.
1925 *
1926 * Return:
1927 * 0 on success
1928 * -ENODEV on port look-up failure
1929 */
1930int notrace cci_disable_port_by_cpu(u64 mpidr)
1931{
1932 int cpu;
1933 bool is_valid;
1934 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
1935 is_valid = cpu_port_is_valid(&cpu_port[cpu]);
1936 if (is_valid && cpu_port_match(&cpu_port[cpu], mpidr)) {
1937 cci_port_control(cpu_port[cpu].port, false);
1938 return 0;
1939 }
1940 }
1941 return -ENODEV;
1942}
1943EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu);
1944
1945/**
Nicolas Pitre62158f82013-05-21 23:34:41 -04001946 * cci_enable_port_for_self() - enable a CCI port for calling CPU
1947 *
1948 * Enabling a CCI port for the calling CPU implies enabling the CCI
1949 * port controlling that CPU's cluster. Caller must make sure that the
1950 * CPU running the code is the first active CPU in the cluster and all
1951 * other CPUs are quiescent in a low power state or waiting for this CPU
1952 * to complete the CCI initialization.
1953 *
1954 * Because this is called when the MMU is still off and with no stack,
1955 * the code must be position independent and ideally rely on callee
1956 * clobbered registers only. To achieve this we must code this function
1957 * entirely in assembler.
1958 *
1959 * On success this returns with the proper CCI port enabled. In case of
1960 * any failure this never returns as the inability to enable the CCI is
1961 * fatal and there is no possible recovery at this stage.
1962 */
1963asmlinkage void __naked cci_enable_port_for_self(void)
1964{
1965 asm volatile ("\n"
Arnd Bergmannf4902492013-06-03 15:15:36 +02001966" .arch armv7-a\n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04001967" mrc p15, 0, r0, c0, c0, 5 @ get MPIDR value \n"
1968" and r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n"
1969" adr r1, 5f \n"
1970" ldr r2, [r1] \n"
1971" add r1, r1, r2 @ &cpu_port \n"
1972" add ip, r1, %[sizeof_cpu_port] \n"
1973
1974 /* Loop over the cpu_port array looking for a matching MPIDR */
1975"1: ldr r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
1976" cmp r2, r0 @ compare MPIDR \n"
1977" bne 2f \n"
1978
1979 /* Found a match, now test port validity */
1980" ldr r3, [r1, %[offsetof_cpu_port_port]] \n"
1981" tst r3, #"__stringify(PORT_VALID)" \n"
1982" bne 3f \n"
1983
1984 /* no match, loop with the next cpu_port entry */
1985"2: add r1, r1, %[sizeof_struct_cpu_port] \n"
1986" cmp r1, ip @ done? \n"
1987" blo 1b \n"
1988
1989 /* CCI port not found -- cheaply try to stall this CPU */
1990"cci_port_not_found: \n"
1991" wfi \n"
1992" wfe \n"
1993" b cci_port_not_found \n"
1994
1995 /* Use matched port index to look up the corresponding ports entry */
1996"3: bic r3, r3, #"__stringify(PORT_VALID)" \n"
1997" adr r0, 6f \n"
1998" ldmia r0, {r1, r2} \n"
1999" sub r1, r1, r0 @ virt - phys \n"
2000" ldr r0, [r0, r2] @ *(&ports) \n"
2001" mov r2, %[sizeof_struct_ace_port] \n"
2002" mla r0, r2, r3, r0 @ &ports[index] \n"
2003" sub r0, r0, r1 @ virt_to_phys() \n"
2004
2005 /* Enable the CCI port */
2006" ldr r0, [r0, %[offsetof_port_phys]] \n"
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07002007" mov r3, %[cci_enable_req]\n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04002008" str r3, [r0, #"__stringify(CCI_PORT_CTRL)"] \n"
2009
2010 /* poll the status reg for completion */
2011" adr r1, 7f \n"
2012" ldr r0, [r1] \n"
2013" ldr r0, [r0, r1] @ cci_ctrl_base \n"
2014"4: ldr r1, [r0, #"__stringify(CCI_CTRL_STATUS)"] \n"
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07002015" tst r1, %[cci_control_status_bits] \n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04002016" bne 4b \n"
2017
2018" mov r0, #0 \n"
2019" bx lr \n"
2020
2021" .align 2 \n"
2022"5: .word cpu_port - . \n"
2023"6: .word . \n"
2024" .word ports - 6b \n"
2025"7: .word cci_ctrl_phys - . \n"
2026 : :
2027 [sizeof_cpu_port] "i" (sizeof(cpu_port)),
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07002028 [cci_enable_req] "i" cpu_to_le32(CCI_ENABLE_REQ),
2029 [cci_control_status_bits] "i" cpu_to_le32(1),
Nicolas Pitre62158f82013-05-21 23:34:41 -04002030#ifndef __ARMEB__
2031 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
2032#else
2033 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
2034#endif
2035 [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
2036 [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
2037 [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
2038 [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
2039
2040 unreachable();
2041}
2042
2043/**
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002044 * __cci_control_port_by_device() - function to control a CCI port by device
2045 * reference
2046 *
2047 * @dn: device node pointer of the device whose CCI port should be
2048 * controlled
2049 * @enable: if true enables the port, if false disables it
2050 *
2051 * Return:
2052 * 0 on success
2053 * -ENODEV on port look-up failure
2054 */
2055int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
2056{
2057 int port;
2058
2059 if (!dn)
2060 return -ENODEV;
2061
2062 port = __cci_ace_get_port(dn, ACE_LITE_PORT);
2063 if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n",
2064 dn->full_name))
2065 return -ENODEV;
2066 cci_port_control(port, enable);
2067 return 0;
2068}
2069EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
2070
2071/**
2072 * __cci_control_port_by_index() - function to control a CCI port by port index
2073 *
2074 * @port: port index previously retrieved with cci_ace_get_port()
2075 * @enable: if true enables the port, if false disables it
2076 *
2077 * Return:
2078 * 0 on success
2079 * -ENODEV on port index out of range
2080 * -EPERM if operation carried out on an ACE PORT
2081 */
2082int notrace __cci_control_port_by_index(u32 port, bool enable)
2083{
2084 if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
2085 return -ENODEV;
2086 /*
2087 * CCI control for ports connected to CPUS is extremely fragile
2088 * and must be made to go through a specific and controlled
2089 * interface (ie cci_disable_port_by_cpu(); control by general purpose
2090 * indexing is therefore disabled for ACE ports.
2091 */
2092 if (ports[port].type == ACE_PORT)
2093 return -EPERM;
2094
2095 cci_port_control(port, enable);
2096 return 0;
2097}
2098EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
2099
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002100static const struct of_device_id arm_cci_ctrl_if_matches[] = {
2101 {.compatible = "arm,cci-400-ctrl-if", },
2102 {},
2103};
2104
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002105static int cci_probe_ports(struct device_node *np)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002106{
2107 struct cci_nb_ports const *cci_config;
2108 int ret, i, nb_ace = 0, nb_ace_lite = 0;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002109 struct device_node *cp;
Nicolas Pitre62158f82013-05-21 23:34:41 -04002110 struct resource res;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002111 const char *match_str;
2112 bool is_ace;
2113
Abhilash Kesavan896ddd62015-01-10 08:41:35 +05302114
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002115 cci_config = of_match_node(arm_cci_matches, np)->data;
2116 if (!cci_config)
2117 return -ENODEV;
2118
2119 nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
2120
Lorenzo Pieralisi7c762032014-01-27 10:50:37 +00002121 ports = kcalloc(nb_cci_ports, sizeof(*ports), GFP_KERNEL);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002122 if (!ports)
2123 return -ENOMEM;
2124
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002125 for_each_child_of_node(np, cp) {
2126 if (!of_match_node(arm_cci_ctrl_if_matches, cp))
2127 continue;
2128
2129 i = nb_ace + nb_ace_lite;
2130
2131 if (i >= nb_cci_ports)
2132 break;
2133
2134 if (of_property_read_string(cp, "interface-type",
2135 &match_str)) {
2136 WARN(1, "node %s missing interface-type property\n",
2137 cp->full_name);
2138 continue;
2139 }
2140 is_ace = strcmp(match_str, "ace") == 0;
2141 if (!is_ace && strcmp(match_str, "ace-lite")) {
2142 WARN(1, "node %s containing invalid interface-type property, skipping it\n",
2143 cp->full_name);
2144 continue;
2145 }
2146
Nicolas Pitre62158f82013-05-21 23:34:41 -04002147 ret = of_address_to_resource(cp, 0, &res);
2148 if (!ret) {
2149 ports[i].base = ioremap(res.start, resource_size(&res));
2150 ports[i].phys = res.start;
2151 }
2152 if (ret || !ports[i].base) {
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002153 WARN(1, "unable to ioremap CCI port %d\n", i);
2154 continue;
2155 }
2156
2157 if (is_ace) {
2158 if (WARN_ON(nb_ace >= cci_config->nb_ace))
2159 continue;
2160 ports[i].type = ACE_PORT;
2161 ++nb_ace;
2162 } else {
2163 if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
2164 continue;
2165 ports[i].type = ACE_LITE_PORT;
2166 ++nb_ace_lite;
2167 }
2168 ports[i].dn = cp;
2169 }
2170
2171 /* initialize a stashed array of ACE ports to speed-up look-up */
2172 cci_ace_init_ports();
2173
2174 /*
2175 * Multi-cluster systems may need this data when non-coherent, during
2176 * cluster power-up/power-down. Make sure it reaches main memory.
2177 */
2178 sync_cache_w(&cci_ctrl_base);
Nicolas Pitre62158f82013-05-21 23:34:41 -04002179 sync_cache_w(&cci_ctrl_phys);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002180 sync_cache_w(&ports);
2181 sync_cache_w(&cpu_port);
2182 __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
2183 pr_info("ARM CCI driver probed\n");
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002184
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002185 return 0;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002186}
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +00002187#else /* !CONFIG_ARM_CCI400_PORT_CTRL */
2188static inline int cci_probe_ports(struct device_node *np)
2189{
2190 return 0;
2191}
2192#endif /* CONFIG_ARM_CCI400_PORT_CTRL */
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002193
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002194static int cci_probe(void)
2195{
2196 int ret;
2197 struct device_node *np;
2198 struct resource res;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002199
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002200 np = of_find_matching_node(NULL, arm_cci_matches);
2201 if(!np || !of_device_is_available(np))
2202 return -ENODEV;
2203
2204 ret = of_address_to_resource(np, 0, &res);
2205 if (!ret) {
2206 cci_ctrl_base = ioremap(res.start, resource_size(&res));
2207 cci_ctrl_phys = res.start;
2208 }
2209 if (ret || !cci_ctrl_base) {
2210 WARN(1, "unable to ioremap CCI ctrl\n");
2211 return -ENXIO;
2212 }
2213
2214 return cci_probe_ports(np);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002215}
2216
2217static int cci_init_status = -EAGAIN;
2218static DEFINE_MUTEX(cci_probing);
2219
Punit Agrawalb91c8f22013-08-22 14:41:51 +01002220static int cci_init(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002221{
2222 if (cci_init_status != -EAGAIN)
2223 return cci_init_status;
2224
2225 mutex_lock(&cci_probing);
2226 if (cci_init_status == -EAGAIN)
2227 cci_init_status = cci_probe();
2228 mutex_unlock(&cci_probing);
2229 return cci_init_status;
2230}
2231
2232/*
2233 * To sort out early init calls ordering a helper function is provided to
2234 * check if the CCI driver has beed initialized. Function check if the driver
2235 * has been initialized, if not it calls the init function that probes
2236 * the driver and updates the return value.
2237 */
Punit Agrawalb91c8f22013-08-22 14:41:51 +01002238bool cci_probed(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002239{
2240 return cci_init() == 0;
2241}
2242EXPORT_SYMBOL_GPL(cci_probed);
2243
2244early_initcall(cci_init);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01002245core_initcall(cci_platform_init);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002246MODULE_LICENSE("GPL");
2247MODULE_DESCRIPTION("ARM CCI support");