blob: d28903712eb82e88805434c001758be14d979388 [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. Poulosea95791e2015-05-26 10:53:15 +010055#ifdef CONFIG_ARM_CCI500_PMU
56 { .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. Poulosea95791e2015-05-26 10:53:15 +010095#ifdef CONFIG_ARM_CCI500_PMU
96 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. Poulosefc17c832015-03-18 12:24:39 +0000129};
130
131static struct cci_pmu_model cci_pmu_models[];
132
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000133struct cci_pmu {
134 void __iomem *base;
135 struct pmu pmu;
136 int nr_irqs;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100137 int *irqs;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000138 unsigned long active_irqs;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000139 const struct cci_pmu_model *model;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000140 struct cci_pmu_hw_events hw_events;
141 struct platform_device *plat_device;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100142 int num_cntrs;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000143 atomic_t active_events;
144 struct mutex reserve_mutex;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100145 struct notifier_block cpu_nb;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000146 cpumask_t cpus;
147};
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000148
149#define to_cci_pmu(c) (container_of(c, struct cci_pmu, pmu))
150
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100151enum cci_models {
152#ifdef CONFIG_ARM_CCI400_PMU
153 CCI400_R0,
154 CCI400_R1,
155#endif
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100156#ifdef CONFIG_ARM_CCI500_PMU
157 CCI500_R0,
158#endif
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100159 CCI_MODEL_MAX
160};
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100161
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000162static void pmu_write_counters(struct cci_pmu *cci_pmu,
163 unsigned long *mask);
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100164static ssize_t cci_pmu_format_show(struct device *dev,
165 struct device_attribute *attr, char *buf);
166static ssize_t cci_pmu_event_show(struct device *dev,
167 struct device_attribute *attr, char *buf);
168
Mark Rutland5e442eb2016-02-23 10:49:43 +0000169#define CCI_EXT_ATTR_ENTRY(_name, _func, _config) \
170 &((struct dev_ext_attribute[]) { \
171 { __ATTR(_name, S_IRUGO, _func, NULL), (void *)_config } \
172 })[0].attr.attr
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100173
174#define CCI_FORMAT_EXT_ATTR_ENTRY(_name, _config) \
175 CCI_EXT_ATTR_ENTRY(_name, cci_pmu_format_show, (char *)_config)
176#define CCI_EVENT_EXT_ATTR_ENTRY(_name, _config) \
177 CCI_EXT_ATTR_ENTRY(_name, cci_pmu_event_show, (unsigned long)_config)
178
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100179/* CCI400 PMU Specific definitions */
180
181#ifdef CONFIG_ARM_CCI400_PMU
182
183/* Port ids */
184#define CCI400_PORT_S0 0
185#define CCI400_PORT_S1 1
186#define CCI400_PORT_S2 2
187#define CCI400_PORT_S3 3
188#define CCI400_PORT_S4 4
189#define CCI400_PORT_M0 5
190#define CCI400_PORT_M1 6
191#define CCI400_PORT_M2 7
192
193#define CCI400_R1_PX 5
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100194
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100195/*
196 * Instead of an event id to monitor CCI cycles, a dedicated counter is
197 * provided. Use 0xff to represent CCI cycles and hope that no future revisions
198 * make use of this event in hardware.
199 */
200enum cci400_perf_events {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100201 CCI400_PMU_CYCLES = 0xff
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100202};
203
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100204#define CCI400_PMU_CYCLE_CNTR_IDX 0
205#define CCI400_PMU_CNTR0_IDX 1
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100206
207/*
208 * CCI PMU event id is an 8-bit value made of two parts - bits 7:5 for one of 8
209 * ports and bits 4:0 are event codes. There are different event codes
210 * associated with each port type.
211 *
212 * Additionally, the range of events associated with the port types changed
213 * between Rev0 and Rev1.
214 *
215 * The constants below define the range of valid codes for each port type for
216 * the different revisions and are used to validate the event to be monitored.
217 */
218
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100219#define CCI400_PMU_EVENT_MASK 0xffUL
220#define CCI400_PMU_EVENT_SOURCE_SHIFT 5
221#define CCI400_PMU_EVENT_SOURCE_MASK 0x7
222#define CCI400_PMU_EVENT_CODE_SHIFT 0
223#define CCI400_PMU_EVENT_CODE_MASK 0x1f
224#define CCI400_PMU_EVENT_SOURCE(event) \
225 ((event >> CCI400_PMU_EVENT_SOURCE_SHIFT) & \
226 CCI400_PMU_EVENT_SOURCE_MASK)
227#define CCI400_PMU_EVENT_CODE(event) \
228 ((event >> CCI400_PMU_EVENT_CODE_SHIFT) & CCI400_PMU_EVENT_CODE_MASK)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100229
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100230#define CCI400_R0_SLAVE_PORT_MIN_EV 0x00
231#define CCI400_R0_SLAVE_PORT_MAX_EV 0x13
232#define CCI400_R0_MASTER_PORT_MIN_EV 0x14
233#define CCI400_R0_MASTER_PORT_MAX_EV 0x1a
234
235#define CCI400_R1_SLAVE_PORT_MIN_EV 0x00
236#define CCI400_R1_SLAVE_PORT_MAX_EV 0x14
237#define CCI400_R1_MASTER_PORT_MIN_EV 0x00
238#define CCI400_R1_MASTER_PORT_MAX_EV 0x11
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100239
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100240#define CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(_name, _config) \
241 CCI_EXT_ATTR_ENTRY(_name, cci400_pmu_cycle_event_show, \
242 (unsigned long)_config)
243
244static ssize_t cci400_pmu_cycle_event_show(struct device *dev,
245 struct device_attribute *attr, char *buf);
246
Mark Rutland5e442eb2016-02-23 10:49:43 +0000247static struct attribute *cci400_pmu_format_attrs[] = {
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100248 CCI_FORMAT_EXT_ATTR_ENTRY(event, "config:0-4"),
249 CCI_FORMAT_EXT_ATTR_ENTRY(source, "config:5-7"),
Mark Rutland5e442eb2016-02-23 10:49:43 +0000250 NULL
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100251};
252
Mark Rutland5e442eb2016-02-23 10:49:43 +0000253static struct attribute *cci400_r0_pmu_event_attrs[] = {
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100254 /* Slave events */
255 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_any, 0x0),
256 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_device, 0x01),
257 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_normal_or_nonshareable, 0x2),
258 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_inner_or_outershareable, 0x3),
259 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maintenance, 0x4),
260 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_mem_barrier, 0x5),
261 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_sync_barrier, 0x6),
262 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
263 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg_sync, 0x8),
264 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_tt_full, 0x9),
265 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_last_hs_snoop, 0xA),
266 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall_rvalids_h_rready_l, 0xB),
267 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_any, 0xC),
268 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_device, 0xD),
269 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_normal_or_nonshareable, 0xE),
270 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_inner_or_outershare_wback_wclean, 0xF),
271 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_unique, 0x10),
272 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_line_unique, 0x11),
273 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_evict, 0x12),
274 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall_tt_full, 0x13),
275 /* Master events */
276 CCI_EVENT_EXT_ATTR_ENTRY(mi_retry_speculative_fetch, 0x14),
277 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_addr_hazard, 0x15),
278 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_id_hazard, 0x16),
279 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_tt_full, 0x17),
280 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_barrier_hazard, 0x18),
281 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_barrier_hazard, 0x19),
282 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_tt_full, 0x1A),
283 /* Special event for cycles counter */
284 CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(cycles, 0xff),
Mark Rutland5e442eb2016-02-23 10:49:43 +0000285 NULL
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100286};
287
Mark Rutland5e442eb2016-02-23 10:49:43 +0000288static struct attribute *cci400_r1_pmu_event_attrs[] = {
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100289 /* Slave events */
290 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_any, 0x0),
291 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_device, 0x01),
292 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_normal_or_nonshareable, 0x2),
293 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_inner_or_outershareable, 0x3),
294 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maintenance, 0x4),
295 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_mem_barrier, 0x5),
296 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_sync_barrier, 0x6),
297 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
298 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg_sync, 0x8),
299 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_tt_full, 0x9),
300 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_last_hs_snoop, 0xA),
301 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall_rvalids_h_rready_l, 0xB),
302 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_any, 0xC),
303 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_device, 0xD),
304 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_normal_or_nonshareable, 0xE),
305 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_inner_or_outershare_wback_wclean, 0xF),
306 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_unique, 0x10),
307 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_line_unique, 0x11),
308 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_evict, 0x12),
309 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall_tt_full, 0x13),
310 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_slave_id_hazard, 0x14),
311 /* Master events */
312 CCI_EVENT_EXT_ATTR_ENTRY(mi_retry_speculative_fetch, 0x0),
313 CCI_EVENT_EXT_ATTR_ENTRY(mi_stall_cycle_addr_hazard, 0x1),
314 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_master_id_hazard, 0x2),
315 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_hi_prio_rtq_full, 0x3),
316 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_barrier_hazard, 0x4),
317 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_barrier_hazard, 0x5),
318 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_wtq_full, 0x6),
319 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_low_prio_rtq_full, 0x7),
320 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_mid_prio_rtq_full, 0x8),
321 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn0, 0x9),
322 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn1, 0xA),
323 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn2, 0xB),
324 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn3, 0xC),
325 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn0, 0xD),
326 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn1, 0xE),
327 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn2, 0xF),
328 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn3, 0x10),
329 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_unique_or_line_unique_addr_hazard, 0x11),
330 /* Special event for cycles counter */
331 CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(cycles, 0xff),
Mark Rutland5e442eb2016-02-23 10:49:43 +0000332 NULL
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100333};
334
335static ssize_t cci400_pmu_cycle_event_show(struct device *dev,
336 struct device_attribute *attr, char *buf)
337{
338 struct dev_ext_attribute *eattr = container_of(attr,
339 struct dev_ext_attribute, attr);
340 return snprintf(buf, PAGE_SIZE, "config=0x%lx\n", (unsigned long)eattr->var);
341}
342
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100343static int cci400_get_event_idx(struct cci_pmu *cci_pmu,
344 struct cci_pmu_hw_events *hw,
345 unsigned long cci_event)
346{
347 int idx;
348
349 /* cycles event idx is fixed */
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100350 if (cci_event == CCI400_PMU_CYCLES) {
351 if (test_and_set_bit(CCI400_PMU_CYCLE_CNTR_IDX, hw->used_mask))
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100352 return -EAGAIN;
353
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100354 return CCI400_PMU_CYCLE_CNTR_IDX;
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100355 }
356
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100357 for (idx = CCI400_PMU_CNTR0_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); ++idx)
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100358 if (!test_and_set_bit(idx, hw->used_mask))
359 return idx;
360
361 /* No counters available */
362 return -EAGAIN;
363}
364
365static int cci400_validate_hw_event(struct cci_pmu *cci_pmu, unsigned long hw_event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100366{
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100367 u8 ev_source = CCI400_PMU_EVENT_SOURCE(hw_event);
368 u8 ev_code = CCI400_PMU_EVENT_CODE(hw_event);
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000369 int if_type;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100370
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100371 if (hw_event & ~CCI400_PMU_EVENT_MASK)
Suzuki K. Poulose874c5712015-03-18 12:24:42 +0000372 return -ENOENT;
373
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100374 if (hw_event == CCI400_PMU_CYCLES)
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100375 return hw_event;
376
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100377 switch (ev_source) {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100378 case CCI400_PORT_S0:
379 case CCI400_PORT_S1:
380 case CCI400_PORT_S2:
381 case CCI400_PORT_S3:
382 case CCI400_PORT_S4:
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100383 /* Slave Interface */
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000384 if_type = CCI_IF_SLAVE;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100385 break;
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100386 case CCI400_PORT_M0:
387 case CCI400_PORT_M1:
388 case CCI400_PORT_M2:
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100389 /* Master Interface */
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000390 if_type = CCI_IF_MASTER;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100391 break;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000392 default:
393 return -ENOENT;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100394 }
395
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100396 if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
397 ev_code <= cci_pmu->model->event_ranges[if_type].max)
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000398 return hw_event;
399
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100400 return -ENOENT;
401}
402
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100403static int probe_cci400_revision(void)
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000404{
405 int rev;
406 rev = readl_relaxed(cci_ctrl_base + CCI_PID2) & CCI_PID2_REV_MASK;
407 rev >>= CCI_PID2_REV_SHIFT;
408
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100409 if (rev < CCI400_R1_PX)
410 return CCI400_R0;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000411 else
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100412 return CCI400_R1;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000413}
414
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000415static const struct cci_pmu_model *probe_cci_model(struct platform_device *pdev)
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000416{
Suzuki K. Poulose772742a2015-03-18 12:24:40 +0000417 if (platform_has_secure_cci_access())
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100418 return &cci_pmu_models[probe_cci400_revision()];
Suzuki K. Poulose772742a2015-03-18 12:24:40 +0000419 return NULL;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000420}
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100421#else /* !CONFIG_ARM_CCI400_PMU */
422static inline struct cci_pmu_model *probe_cci_model(struct platform_device *pdev)
423{
424 return NULL;
425}
426#endif /* CONFIG_ARM_CCI400_PMU */
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000427
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100428#ifdef CONFIG_ARM_CCI500_PMU
429
430/*
431 * CCI500 provides 8 independent event counters that can count
432 * any of the events available.
433 *
434 * CCI500 PMU event id is an 9-bit value made of two parts.
435 * bits [8:5] - Source for the event
436 * 0x0-0x6 - Slave interfaces
437 * 0x8-0xD - Master interfaces
438 * 0xf - Global Events
439 * 0x7,0xe - Reserved
440 *
441 * bits [4:0] - Event code (specific to type of interface)
442 */
443
444/* Port ids */
445#define CCI500_PORT_S0 0x0
446#define CCI500_PORT_S1 0x1
447#define CCI500_PORT_S2 0x2
448#define CCI500_PORT_S3 0x3
449#define CCI500_PORT_S4 0x4
450#define CCI500_PORT_S5 0x5
451#define CCI500_PORT_S6 0x6
452
453#define CCI500_PORT_M0 0x8
454#define CCI500_PORT_M1 0x9
455#define CCI500_PORT_M2 0xa
456#define CCI500_PORT_M3 0xb
457#define CCI500_PORT_M4 0xc
458#define CCI500_PORT_M5 0xd
459
460#define CCI500_PORT_GLOBAL 0xf
461
462#define CCI500_PMU_EVENT_MASK 0x1ffUL
463#define CCI500_PMU_EVENT_SOURCE_SHIFT 0x5
464#define CCI500_PMU_EVENT_SOURCE_MASK 0xf
465#define CCI500_PMU_EVENT_CODE_SHIFT 0x0
466#define CCI500_PMU_EVENT_CODE_MASK 0x1f
467
468#define CCI500_PMU_EVENT_SOURCE(event) \
469 ((event >> CCI500_PMU_EVENT_SOURCE_SHIFT) & CCI500_PMU_EVENT_SOURCE_MASK)
470#define CCI500_PMU_EVENT_CODE(event) \
471 ((event >> CCI500_PMU_EVENT_CODE_SHIFT) & CCI500_PMU_EVENT_CODE_MASK)
472
473#define CCI500_SLAVE_PORT_MIN_EV 0x00
474#define CCI500_SLAVE_PORT_MAX_EV 0x1f
475#define CCI500_MASTER_PORT_MIN_EV 0x00
476#define CCI500_MASTER_PORT_MAX_EV 0x06
477#define CCI500_GLOBAL_PORT_MIN_EV 0x00
478#define CCI500_GLOBAL_PORT_MAX_EV 0x0f
479
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100480
481#define CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(_name, _config) \
482 CCI_EXT_ATTR_ENTRY(_name, cci500_pmu_global_event_show, \
483 (unsigned long) _config)
484
485static ssize_t cci500_pmu_global_event_show(struct device *dev,
486 struct device_attribute *attr, char *buf);
487
Mark Rutland5e442eb2016-02-23 10:49:43 +0000488static struct attribute *cci500_pmu_format_attrs[] = {
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100489 CCI_FORMAT_EXT_ATTR_ENTRY(event, "config:0-4"),
490 CCI_FORMAT_EXT_ATTR_ENTRY(source, "config:5-8"),
Mark Rutland5e442eb2016-02-23 10:49:43 +0000491 NULL,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100492};
493
Mark Rutland5e442eb2016-02-23 10:49:43 +0000494static struct attribute *cci500_pmu_event_attrs[] = {
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100495 /* Slave events */
496 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_arvalid, 0x0),
497 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_dev, 0x1),
498 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_nonshareable, 0x2),
499 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_shareable_non_alloc, 0x3),
500 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_shareable_alloc, 0x4),
501 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_invalidate, 0x5),
502 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maint, 0x6),
503 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
504 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_rval, 0x8),
505 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_rlast_snoop, 0x9),
506 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_awalid, 0xA),
507 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_dev, 0xB),
508 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_non_shareable, 0xC),
509 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wb, 0xD),
510 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wlu, 0xE),
511 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wunique, 0xF),
512 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_evict, 0x10),
513 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_wrevict, 0x11),
514 CCI_EVENT_EXT_ATTR_ENTRY(si_w_data_beat, 0x12),
515 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_acvalid, 0x13),
516 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_read, 0x14),
517 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_clean, 0x15),
518 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_data_transfer_low, 0x16),
519 CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_arvalid, 0x17),
520 CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall, 0x18),
521 CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall, 0x19),
522 CCI_EVENT_EXT_ATTR_ENTRY(si_w_data_stall, 0x1A),
523 CCI_EVENT_EXT_ATTR_ENTRY(si_w_resp_stall, 0x1B),
524 CCI_EVENT_EXT_ATTR_ENTRY(si_srq_stall, 0x1C),
525 CCI_EVENT_EXT_ATTR_ENTRY(si_s_data_stall, 0x1D),
526 CCI_EVENT_EXT_ATTR_ENTRY(si_rq_stall_ot_limit, 0x1E),
527 CCI_EVENT_EXT_ATTR_ENTRY(si_r_stall_arbit, 0x1F),
528
529 /* Master events */
530 CCI_EVENT_EXT_ATTR_ENTRY(mi_r_data_beat_any, 0x0),
531 CCI_EVENT_EXT_ATTR_ENTRY(mi_w_data_beat_any, 0x1),
532 CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall, 0x2),
533 CCI_EVENT_EXT_ATTR_ENTRY(mi_r_data_stall, 0x3),
534 CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall, 0x4),
535 CCI_EVENT_EXT_ATTR_ENTRY(mi_w_data_stall, 0x5),
536 CCI_EVENT_EXT_ATTR_ENTRY(mi_w_resp_stall, 0x6),
537
538 /* Global events */
539 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_0_1, 0x0),
540 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_2_3, 0x1),
541 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_4_5, 0x2),
542 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_6_7, 0x3),
543 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_0_1, 0x4),
544 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_2_3, 0x5),
545 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_4_5, 0x6),
546 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_6_7, 0x7),
547 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_back_invalidation, 0x8),
548 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_stall_alloc_busy, 0x9),
549 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_stall_tt_full, 0xA),
550 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_wrq, 0xB),
551 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_cd_hs, 0xC),
552 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_rq_stall_addr_hazard, 0xD),
553 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snopp_rq_stall_tt_full, 0xE),
554 CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_rq_tzmp1_prot, 0xF),
Mark Rutland5e442eb2016-02-23 10:49:43 +0000555 NULL
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100556};
557
558static ssize_t cci500_pmu_global_event_show(struct device *dev,
559 struct device_attribute *attr, char *buf)
560{
561 struct dev_ext_attribute *eattr = container_of(attr,
562 struct dev_ext_attribute, attr);
563 /* Global events have single fixed source code */
564 return snprintf(buf, PAGE_SIZE, "event=0x%lx,source=0x%x\n",
565 (unsigned long)eattr->var, CCI500_PORT_GLOBAL);
566}
567
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +0100568static int cci500_validate_hw_event(struct cci_pmu *cci_pmu,
569 unsigned long hw_event)
570{
571 u32 ev_source = CCI500_PMU_EVENT_SOURCE(hw_event);
572 u32 ev_code = CCI500_PMU_EVENT_CODE(hw_event);
573 int if_type;
574
575 if (hw_event & ~CCI500_PMU_EVENT_MASK)
576 return -ENOENT;
577
578 switch (ev_source) {
579 case CCI500_PORT_S0:
580 case CCI500_PORT_S1:
581 case CCI500_PORT_S2:
582 case CCI500_PORT_S3:
583 case CCI500_PORT_S4:
584 case CCI500_PORT_S5:
585 case CCI500_PORT_S6:
586 if_type = CCI_IF_SLAVE;
587 break;
588 case CCI500_PORT_M0:
589 case CCI500_PORT_M1:
590 case CCI500_PORT_M2:
591 case CCI500_PORT_M3:
592 case CCI500_PORT_M4:
593 case CCI500_PORT_M5:
594 if_type = CCI_IF_MASTER;
595 break;
596 case CCI500_PORT_GLOBAL:
597 if_type = CCI_IF_GLOBAL;
598 break;
599 default:
600 return -ENOENT;
601 }
602
603 if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
604 ev_code <= cci_pmu->model->event_ranges[if_type].max)
605 return hw_event;
606
607 return -ENOENT;
608}
609#endif /* CONFIG_ARM_CCI500_PMU */
610
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000611/*
612 * Program the CCI PMU counters which have PERF_HES_ARCH set
613 * with the event period and mark them ready before we enable
614 * PMU.
615 */
616void cci_pmu_sync_counters(struct cci_pmu *cci_pmu)
617{
618 int i;
619 struct cci_pmu_hw_events *cci_hw = &cci_pmu->hw_events;
620
621 DECLARE_BITMAP(mask, cci_pmu->num_cntrs);
622
623 bitmap_zero(mask, cci_pmu->num_cntrs);
624 for_each_set_bit(i, cci_pmu->hw_events.used_mask, cci_pmu->num_cntrs) {
625 struct perf_event *event = cci_hw->events[i];
626
627 if (WARN_ON(!event))
628 continue;
629
630 /* Leave the events which are not counting */
631 if (event->hw.state & PERF_HES_STOPPED)
632 continue;
633 if (event->hw.state & PERF_HES_ARCH) {
634 set_bit(i, mask);
635 event->hw.state &= ~PERF_HES_ARCH;
636 }
637 }
638
639 pmu_write_counters(cci_pmu, mask);
640}
641
Suzuki K Poulosea077c522016-02-23 10:49:46 +0000642/* Should be called with cci_pmu->hw_events->pmu_lock held */
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000643static void __cci_pmu_enable(struct cci_pmu *cci_pmu)
Suzuki K Poulosea077c522016-02-23 10:49:46 +0000644{
645 u32 val;
646
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000647 cci_pmu_sync_counters(cci_pmu);
648
Suzuki K Poulosea077c522016-02-23 10:49:46 +0000649 /* Enable all the PMU counters. */
650 val = readl_relaxed(cci_ctrl_base + CCI_PMCR) | CCI_PMCR_CEN;
651 writel(val, cci_ctrl_base + CCI_PMCR);
652}
653
654/* Should be called with cci_pmu->hw_events->pmu_lock held */
655static void __cci_pmu_disable(void)
656{
657 u32 val;
658
659 /* Disable all the PMU counters. */
660 val = readl_relaxed(cci_ctrl_base + CCI_PMCR) & ~CCI_PMCR_CEN;
661 writel(val, cci_ctrl_base + CCI_PMCR);
662}
663
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +0100664static ssize_t cci_pmu_format_show(struct device *dev,
665 struct device_attribute *attr, char *buf)
666{
667 struct dev_ext_attribute *eattr = container_of(attr,
668 struct dev_ext_attribute, attr);
669 return snprintf(buf, PAGE_SIZE, "%s\n", (char *)eattr->var);
670}
671
672static ssize_t cci_pmu_event_show(struct device *dev,
673 struct device_attribute *attr, char *buf)
674{
675 struct dev_ext_attribute *eattr = container_of(attr,
676 struct dev_ext_attribute, attr);
677 /* source parameter is mandatory for normal PMU events */
678 return snprintf(buf, PAGE_SIZE, "source=?,event=0x%lx\n",
679 (unsigned long)eattr->var);
680}
681
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100682static int pmu_is_valid_counter(struct cci_pmu *cci_pmu, int idx)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100683{
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100684 return 0 <= idx && idx <= CCI_PMU_CNTR_LAST(cci_pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100685}
686
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100687static u32 pmu_read_register(struct cci_pmu *cci_pmu, int idx, unsigned int offset)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100688{
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100689 return readl_relaxed(cci_pmu->base +
690 CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100691}
692
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100693static void pmu_write_register(struct cci_pmu *cci_pmu, u32 value,
694 int idx, unsigned int offset)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100695{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100696 return writel_relaxed(value, cci_pmu->base +
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100697 CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100698}
699
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100700static void pmu_disable_counter(struct cci_pmu *cci_pmu, int idx)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100701{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100702 pmu_write_register(cci_pmu, 0, idx, CCI_PMU_CNTR_CTRL);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100703}
704
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100705static void pmu_enable_counter(struct cci_pmu *cci_pmu, int idx)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100706{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100707 pmu_write_register(cci_pmu, 1, idx, CCI_PMU_CNTR_CTRL);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100708}
709
Suzuki K Poulose1ce63112016-02-23 10:49:49 +0000710static bool __maybe_unused
711pmu_counter_is_enabled(struct cci_pmu *cci_pmu, int idx)
712{
713 return (pmu_read_register(cci_pmu, idx, CCI_PMU_CNTR_CTRL) & 0x1) != 0;
714}
715
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100716static void pmu_set_event(struct cci_pmu *cci_pmu, int idx, unsigned long event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100717{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100718 pmu_write_register(cci_pmu, event, idx, CCI_PMU_EVT_SEL);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100719}
720
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100721/*
722 * Returns the number of programmable counters actually implemented
723 * by the cci
724 */
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100725static u32 pmu_get_max_counters(void)
726{
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100727 return (readl_relaxed(cci_ctrl_base + CCI_PMCR) &
728 CCI_PMCR_NCNT_MASK) >> CCI_PMCR_NCNT_SHIFT;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100729}
730
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100731static int pmu_get_event_idx(struct cci_pmu_hw_events *hw, struct perf_event *event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100732{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100733 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100734 unsigned long cci_event = event->hw.config_base;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100735 int idx;
736
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100737 if (cci_pmu->model->get_event_idx)
738 return cci_pmu->model->get_event_idx(cci_pmu, hw, cci_event);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100739
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100740 /* Generic code to find an unused idx from the mask */
741 for(idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100742 if (!test_and_set_bit(idx, hw->used_mask))
743 return idx;
744
745 /* No counters available */
746 return -EAGAIN;
747}
748
749static int pmu_map_event(struct perf_event *event)
750{
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100751 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100752
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100753 if (event->attr.type < PERF_TYPE_MAX ||
754 !cci_pmu->model->validate_hw_event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100755 return -ENOENT;
756
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100757 return cci_pmu->model->validate_hw_event(cci_pmu, event->attr.config);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100758}
759
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100760static int pmu_request_irq(struct cci_pmu *cci_pmu, irq_handler_t handler)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100761{
762 int i;
763 struct platform_device *pmu_device = cci_pmu->plat_device;
764
765 if (unlikely(!pmu_device))
766 return -ENODEV;
767
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100768 if (cci_pmu->nr_irqs < 1) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100769 dev_err(&pmu_device->dev, "no irqs for CCI PMUs defined\n");
770 return -ENODEV;
771 }
772
773 /*
774 * Register all available CCI PMU interrupts. In the interrupt handler
775 * we iterate over the counters checking for interrupt source (the
776 * overflowing counter) and clear it.
777 *
778 * This should allow handling of non-unique interrupt for the counters.
779 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100780 for (i = 0; i < cci_pmu->nr_irqs; i++) {
781 int err = request_irq(cci_pmu->irqs[i], handler, IRQF_SHARED,
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100782 "arm-cci-pmu", cci_pmu);
783 if (err) {
784 dev_err(&pmu_device->dev, "unable to request IRQ%d for ARM CCI PMU counters\n",
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100785 cci_pmu->irqs[i]);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100786 return err;
787 }
788
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100789 set_bit(i, &cci_pmu->active_irqs);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100790 }
791
792 return 0;
793}
794
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100795static void pmu_free_irq(struct cci_pmu *cci_pmu)
796{
797 int i;
798
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100799 for (i = 0; i < cci_pmu->nr_irqs; i++) {
800 if (!test_and_clear_bit(i, &cci_pmu->active_irqs))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100801 continue;
802
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100803 free_irq(cci_pmu->irqs[i], cci_pmu);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100804 }
805}
806
807static u32 pmu_read_counter(struct perf_event *event)
808{
809 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
810 struct hw_perf_event *hw_counter = &event->hw;
811 int idx = hw_counter->idx;
812 u32 value;
813
814 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
815 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
816 return 0;
817 }
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100818 value = pmu_read_register(cci_pmu, idx, CCI_PMU_CNTR);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100819
820 return value;
821}
822
Suzuki K Poulosec8bc2b12016-02-23 10:49:48 +0000823static void pmu_write_counter(struct cci_pmu *cci_pmu, u32 value, int idx)
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100824{
Suzuki K Poulosec8bc2b12016-02-23 10:49:48 +0000825 pmu_write_register(cci_pmu, value, idx, CCI_PMU_CNTR);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100826}
827
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000828static void pmu_write_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
Suzuki K Poulosea53eb5c2016-02-23 10:49:45 +0000829{
830 int i;
831 struct cci_pmu_hw_events *cci_hw = &cci_pmu->hw_events;
832
833 for_each_set_bit(i, mask, cci_pmu->num_cntrs) {
834 struct perf_event *event = cci_hw->events[i];
835
836 if (WARN_ON(!event))
837 continue;
Suzuki K Poulosec8bc2b12016-02-23 10:49:48 +0000838 pmu_write_counter(cci_pmu, local64_read(&event->hw.prev_count), i);
Suzuki K Poulosea53eb5c2016-02-23 10:49:45 +0000839 }
840}
841
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100842static u64 pmu_event_update(struct perf_event *event)
843{
844 struct hw_perf_event *hwc = &event->hw;
845 u64 delta, prev_raw_count, new_raw_count;
846
847 do {
848 prev_raw_count = local64_read(&hwc->prev_count);
849 new_raw_count = pmu_read_counter(event);
850 } while (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
851 new_raw_count) != prev_raw_count);
852
853 delta = (new_raw_count - prev_raw_count) & CCI_PMU_CNTR_MASK;
854
855 local64_add(delta, &event->count);
856
857 return new_raw_count;
858}
859
860static void pmu_read(struct perf_event *event)
861{
862 pmu_event_update(event);
863}
864
865void pmu_event_set_period(struct perf_event *event)
866{
867 struct hw_perf_event *hwc = &event->hw;
868 /*
869 * The CCI PMU counters have a period of 2^32. To account for the
870 * possiblity of extreme interrupt latency we program for a period of
871 * half that. Hopefully we can handle the interrupt before another 2^31
872 * events occur and the counter overtakes its previous value.
873 */
874 u64 val = 1ULL << 31;
875 local64_set(&hwc->prev_count, val);
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000876
877 /*
878 * CCI PMU uses PERF_HES_ARCH to keep track of the counters, whose
879 * values needs to be sync-ed with the s/w state before the PMU is
880 * enabled.
881 * Mark this counter for sync.
882 */
883 hwc->state |= PERF_HES_ARCH;
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100884}
885
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100886static irqreturn_t pmu_handle_irq(int irq_num, void *dev)
887{
888 unsigned long flags;
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100889 struct cci_pmu *cci_pmu = dev;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100890 struct cci_pmu_hw_events *events = &cci_pmu->hw_events;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100891 int idx, handled = IRQ_NONE;
892
893 raw_spin_lock_irqsave(&events->pmu_lock, flags);
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000894
895 /* Disable the PMU while we walk through the counters */
896 __cci_pmu_disable();
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100897 /*
898 * Iterate over counters and update the corresponding perf events.
899 * This should work regardless of whether we have per-counter overflow
900 * interrupt or a combined overflow interrupt.
901 */
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100902 for (idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100903 struct perf_event *event = events->events[idx];
904 struct hw_perf_event *hw_counter;
905
906 if (!event)
907 continue;
908
909 hw_counter = &event->hw;
910
911 /* Did this counter overflow? */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100912 if (!(pmu_read_register(cci_pmu, idx, CCI_PMU_OVRFLW) &
Himangi Saraogifc5130d2014-07-30 11:37:35 +0100913 CCI_PMU_OVRFLW_FLAG))
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100914 continue;
915
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100916 pmu_write_register(cci_pmu, CCI_PMU_OVRFLW_FLAG, idx,
917 CCI_PMU_OVRFLW);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100918
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100919 pmu_event_update(event);
920 pmu_event_set_period(event);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100921 handled = IRQ_HANDLED;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100922 }
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000923
924 /* Enable the PMU and sync possibly overflowed counters */
925 __cci_pmu_enable(cci_pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100926 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
927
928 return IRQ_RETVAL(handled);
929}
930
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100931static int cci_pmu_get_hw(struct cci_pmu *cci_pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100932{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100933 int ret = pmu_request_irq(cci_pmu, pmu_handle_irq);
934 if (ret) {
935 pmu_free_irq(cci_pmu);
936 return ret;
937 }
938 return 0;
939}
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100940
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100941static void cci_pmu_put_hw(struct cci_pmu *cci_pmu)
942{
943 pmu_free_irq(cci_pmu);
944}
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100945
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100946static void hw_perf_event_destroy(struct perf_event *event)
947{
948 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
949 atomic_t *active_events = &cci_pmu->active_events;
950 struct mutex *reserve_mutex = &cci_pmu->reserve_mutex;
951
952 if (atomic_dec_and_mutex_lock(active_events, reserve_mutex)) {
953 cci_pmu_put_hw(cci_pmu);
954 mutex_unlock(reserve_mutex);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100955 }
956}
957
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100958static void cci_pmu_enable(struct pmu *pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100959{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100960 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
961 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100962 int enabled = bitmap_weight(hw_events->used_mask, cci_pmu->num_cntrs);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100963 unsigned long flags;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100964
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100965 if (!enabled)
966 return;
967
968 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000969 __cci_pmu_enable(cci_pmu);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100970 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100971
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100972}
973
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100974static void cci_pmu_disable(struct pmu *pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100975{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100976 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
977 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100978 unsigned long flags;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100979
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100980 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
Suzuki K Poulosea077c522016-02-23 10:49:46 +0000981 __cci_pmu_disable();
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100982 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100983}
984
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100985/*
986 * Check if the idx represents a non-programmable counter.
987 * All the fixed event counters are mapped before the programmable
988 * counters.
989 */
990static bool pmu_fixed_hw_idx(struct cci_pmu *cci_pmu, int idx)
991{
992 return (idx >= 0) && (idx < cci_pmu->model->fixed_hw_cntrs);
993}
994
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100995static void cci_pmu_start(struct perf_event *event, int pmu_flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100996{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100997 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
998 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
999 struct hw_perf_event *hwc = &event->hw;
1000 int idx = hwc->idx;
1001 unsigned long flags;
1002
1003 /*
1004 * To handle interrupt latency, we always reprogram the period
1005 * regardlesss of PERF_EF_RELOAD.
1006 */
1007 if (pmu_flags & PERF_EF_RELOAD)
1008 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
1009
1010 hwc->state = 0;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001011
1012 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
1013 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001014 return;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001015 }
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001016
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001017 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
1018
Suzuki K. Poulose31216292015-05-26 10:53:13 +01001019 /* Configure the counter unless you are counting a fixed event */
1020 if (!pmu_fixed_hw_idx(cci_pmu, idx))
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001021 pmu_set_event(cci_pmu, idx, hwc->config_base);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001022
1023 pmu_event_set_period(event);
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001024 pmu_enable_counter(cci_pmu, idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001025
1026 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001027}
1028
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001029static void cci_pmu_stop(struct perf_event *event, int pmu_flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001030{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001031 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1032 struct hw_perf_event *hwc = &event->hw;
1033 int idx = hwc->idx;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001034
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001035 if (hwc->state & PERF_HES_STOPPED)
1036 return;
1037
1038 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001039 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001040 return;
1041 }
1042
1043 /*
1044 * We always reprogram the counter, so ignore PERF_EF_UPDATE. See
1045 * cci_pmu_start()
1046 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001047 pmu_disable_counter(cci_pmu, idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001048 pmu_event_update(event);
1049 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001050}
1051
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001052static int cci_pmu_add(struct perf_event *event, int flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001053{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001054 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1055 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
1056 struct hw_perf_event *hwc = &event->hw;
1057 int idx;
1058 int err = 0;
1059
1060 perf_pmu_disable(event->pmu);
1061
1062 /* If we don't have a space for the counter then finish early. */
1063 idx = pmu_get_event_idx(hw_events, event);
1064 if (idx < 0) {
1065 err = idx;
1066 goto out;
1067 }
1068
1069 event->hw.idx = idx;
1070 hw_events->events[idx] = event;
1071
1072 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
1073 if (flags & PERF_EF_START)
1074 cci_pmu_start(event, PERF_EF_RELOAD);
1075
1076 /* Propagate our changes to the userspace mapping. */
1077 perf_event_update_userpage(event);
1078
1079out:
1080 perf_pmu_enable(event->pmu);
1081 return err;
1082}
1083
1084static void cci_pmu_del(struct perf_event *event, int flags)
1085{
1086 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1087 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
1088 struct hw_perf_event *hwc = &event->hw;
1089 int idx = hwc->idx;
1090
1091 cci_pmu_stop(event, PERF_EF_UPDATE);
1092 hw_events->events[idx] = NULL;
1093 clear_bit(idx, hw_events->used_mask);
1094
1095 perf_event_update_userpage(event);
1096}
1097
1098static int
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001099validate_event(struct pmu *cci_pmu,
1100 struct cci_pmu_hw_events *hw_events,
1101 struct perf_event *event)
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001102{
1103 if (is_software_event(event))
1104 return 1;
1105
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001106 /*
1107 * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
1108 * core perf code won't check that the pmu->ctx == leader->ctx
1109 * until after pmu->event_init(event).
1110 */
1111 if (event->pmu != cci_pmu)
1112 return 0;
1113
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001114 if (event->state < PERF_EVENT_STATE_OFF)
1115 return 1;
1116
1117 if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
1118 return 1;
1119
1120 return pmu_get_event_idx(hw_events, event) >= 0;
1121}
1122
1123static int
1124validate_group(struct perf_event *event)
1125{
1126 struct perf_event *sibling, *leader = event->group_leader;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001127 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1128 unsigned long mask[BITS_TO_LONGS(cci_pmu->num_cntrs)];
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001129 struct cci_pmu_hw_events fake_pmu = {
1130 /*
1131 * Initialise the fake PMU. We only need to populate the
1132 * used_mask for the purposes of validation.
1133 */
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001134 .used_mask = mask,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001135 };
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001136 memset(mask, 0, BITS_TO_LONGS(cci_pmu->num_cntrs) * sizeof(unsigned long));
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001137
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001138 if (!validate_event(event->pmu, &fake_pmu, leader))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001139 return -EINVAL;
1140
1141 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001142 if (!validate_event(event->pmu, &fake_pmu, sibling))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001143 return -EINVAL;
1144 }
1145
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001146 if (!validate_event(event->pmu, &fake_pmu, event))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001147 return -EINVAL;
1148
1149 return 0;
1150}
1151
1152static int
1153__hw_perf_event_init(struct perf_event *event)
1154{
1155 struct hw_perf_event *hwc = &event->hw;
1156 int mapping;
1157
1158 mapping = pmu_map_event(event);
1159
1160 if (mapping < 0) {
1161 pr_debug("event %x:%llx not supported\n", event->attr.type,
1162 event->attr.config);
1163 return mapping;
1164 }
1165
1166 /*
1167 * We don't assign an index until we actually place the event onto
1168 * hardware. Use -1 to signify that we haven't decided where to put it
1169 * yet.
1170 */
1171 hwc->idx = -1;
1172 hwc->config_base = 0;
1173 hwc->config = 0;
1174 hwc->event_base = 0;
1175
1176 /*
1177 * Store the event encoding into the config_base field.
1178 */
1179 hwc->config_base |= (unsigned long)mapping;
1180
1181 /*
1182 * Limit the sample_period to half of the counter width. That way, the
1183 * new counter value is far less likely to overtake the previous one
1184 * unless you have some serious IRQ latency issues.
1185 */
1186 hwc->sample_period = CCI_PMU_CNTR_MASK >> 1;
1187 hwc->last_period = hwc->sample_period;
1188 local64_set(&hwc->period_left, hwc->sample_period);
1189
1190 if (event->group_leader != event) {
1191 if (validate_group(event) != 0)
1192 return -EINVAL;
1193 }
1194
1195 return 0;
1196}
1197
1198static int cci_pmu_event_init(struct perf_event *event)
1199{
1200 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1201 atomic_t *active_events = &cci_pmu->active_events;
1202 int err = 0;
1203 int cpu;
1204
1205 if (event->attr.type != event->pmu->type)
1206 return -ENOENT;
1207
1208 /* Shared by all CPUs, no meaningful state to sample */
1209 if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
1210 return -EOPNOTSUPP;
1211
1212 /* We have no filtering of any kind */
1213 if (event->attr.exclude_user ||
1214 event->attr.exclude_kernel ||
1215 event->attr.exclude_hv ||
1216 event->attr.exclude_idle ||
1217 event->attr.exclude_host ||
1218 event->attr.exclude_guest)
1219 return -EINVAL;
1220
1221 /*
1222 * Following the example set by other "uncore" PMUs, we accept any CPU
1223 * and rewrite its affinity dynamically rather than having perf core
1224 * handle cpu == -1 and pid == -1 for this case.
1225 *
1226 * The perf core will pin online CPUs for the duration of this call and
1227 * the event being installed into its context, so the PMU's CPU can't
1228 * change under our feet.
1229 */
1230 cpu = cpumask_first(&cci_pmu->cpus);
1231 if (event->cpu < 0 || cpu < 0)
1232 return -EINVAL;
1233 event->cpu = cpu;
1234
1235 event->destroy = hw_perf_event_destroy;
1236 if (!atomic_inc_not_zero(active_events)) {
1237 mutex_lock(&cci_pmu->reserve_mutex);
1238 if (atomic_read(active_events) == 0)
1239 err = cci_pmu_get_hw(cci_pmu);
1240 if (!err)
1241 atomic_inc(active_events);
1242 mutex_unlock(&cci_pmu->reserve_mutex);
1243 }
1244 if (err)
1245 return err;
1246
1247 err = __hw_perf_event_init(event);
1248 if (err)
1249 hw_perf_event_destroy(event);
1250
1251 return err;
1252}
1253
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001254static ssize_t pmu_cpumask_attr_show(struct device *dev,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001255 struct device_attribute *attr, char *buf)
1256{
Mark Rutland5e442eb2016-02-23 10:49:43 +00001257 struct pmu *pmu = dev_get_drvdata(dev);
1258 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001259
Tejun Heo660e5ec2015-02-13 14:37:20 -08001260 int n = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001261 cpumask_pr_args(&cci_pmu->cpus));
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001262 buf[n++] = '\n';
1263 buf[n] = '\0';
1264 return n;
1265}
1266
Mark Rutland5e442eb2016-02-23 10:49:43 +00001267static struct device_attribute pmu_cpumask_attr =
1268 __ATTR(cpumask, S_IRUGO, pmu_cpumask_attr_show, NULL);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001269
1270static struct attribute *pmu_attrs[] = {
Mark Rutland5e442eb2016-02-23 10:49:43 +00001271 &pmu_cpumask_attr.attr,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001272 NULL,
1273};
1274
1275static struct attribute_group pmu_attr_group = {
1276 .attrs = pmu_attrs,
1277};
1278
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001279static struct attribute_group pmu_format_attr_group = {
1280 .name = "format",
1281 .attrs = NULL, /* Filled in cci_pmu_init_attrs */
1282};
1283
1284static struct attribute_group pmu_event_attr_group = {
1285 .name = "events",
1286 .attrs = NULL, /* Filled in cci_pmu_init_attrs */
1287};
1288
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001289static const struct attribute_group *pmu_attr_groups[] = {
1290 &pmu_attr_group,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001291 &pmu_format_attr_group,
1292 &pmu_event_attr_group,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001293 NULL
1294};
1295
1296static int cci_pmu_init(struct cci_pmu *cci_pmu, struct platform_device *pdev)
1297{
Mark Rutland5e442eb2016-02-23 10:49:43 +00001298 const struct cci_pmu_model *model = cci_pmu->model;
1299 char *name = model->name;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001300 u32 num_cntrs;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001301
Mark Rutland5e442eb2016-02-23 10:49:43 +00001302 pmu_event_attr_group.attrs = model->event_attrs;
1303 pmu_format_attr_group.attrs = model->format_attrs;
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001304
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001305 cci_pmu->pmu = (struct pmu) {
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001306 .name = cci_pmu->model->name,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001307 .task_ctx_nr = perf_invalid_context,
1308 .pmu_enable = cci_pmu_enable,
1309 .pmu_disable = cci_pmu_disable,
1310 .event_init = cci_pmu_event_init,
1311 .add = cci_pmu_add,
1312 .del = cci_pmu_del,
1313 .start = cci_pmu_start,
1314 .stop = cci_pmu_stop,
1315 .read = pmu_read,
1316 .attr_groups = pmu_attr_groups,
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001317 };
1318
1319 cci_pmu->plat_device = pdev;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001320 num_cntrs = pmu_get_max_counters();
1321 if (num_cntrs > cci_pmu->model->num_hw_cntrs) {
1322 dev_warn(&pdev->dev,
1323 "PMU implements more counters(%d) than supported by"
1324 " the model(%d), truncated.",
1325 num_cntrs, cci_pmu->model->num_hw_cntrs);
1326 num_cntrs = cci_pmu->model->num_hw_cntrs;
1327 }
1328 cci_pmu->num_cntrs = num_cntrs + cci_pmu->model->fixed_hw_cntrs;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001329
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001330 return perf_pmu_register(&cci_pmu->pmu, name, -1);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001331}
1332
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001333static int cci_pmu_cpu_notifier(struct notifier_block *self,
1334 unsigned long action, void *hcpu)
1335{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001336 struct cci_pmu *cci_pmu = container_of(self,
1337 struct cci_pmu, cpu_nb);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001338 unsigned int cpu = (long)hcpu;
1339 unsigned int target;
1340
1341 switch (action & ~CPU_TASKS_FROZEN) {
1342 case CPU_DOWN_PREPARE:
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001343 if (!cpumask_test_and_clear_cpu(cpu, &cci_pmu->cpus))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001344 break;
1345 target = cpumask_any_but(cpu_online_mask, cpu);
Andrzej Hajda0f173802016-02-23 10:49:44 +00001346 if (target >= nr_cpu_ids) // UP, last CPU
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001347 break;
1348 /*
1349 * TODO: migrate context once core races on event->ctx have
1350 * been fixed.
1351 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001352 cpumask_set_cpu(target, &cci_pmu->cpus);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001353 default:
1354 break;
1355 }
1356
1357 return NOTIFY_OK;
1358}
1359
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001360static struct cci_pmu_model cci_pmu_models[] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001361#ifdef CONFIG_ARM_CCI400_PMU
1362 [CCI400_R0] = {
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001363 .name = "CCI_400",
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001364 .fixed_hw_cntrs = 1, /* Cycle counter */
1365 .num_hw_cntrs = 4,
1366 .cntr_size = SZ_4K,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001367 .format_attrs = cci400_pmu_format_attrs,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001368 .event_attrs = cci400_r0_pmu_event_attrs,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001369 .event_ranges = {
1370 [CCI_IF_SLAVE] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001371 CCI400_R0_SLAVE_PORT_MIN_EV,
1372 CCI400_R0_SLAVE_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001373 },
1374 [CCI_IF_MASTER] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001375 CCI400_R0_MASTER_PORT_MIN_EV,
1376 CCI400_R0_MASTER_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001377 },
1378 },
Suzuki K. Poulose31216292015-05-26 10:53:13 +01001379 .validate_hw_event = cci400_validate_hw_event,
1380 .get_event_idx = cci400_get_event_idx,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001381 },
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001382 [CCI400_R1] = {
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001383 .name = "CCI_400_r1",
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001384 .fixed_hw_cntrs = 1, /* Cycle counter */
1385 .num_hw_cntrs = 4,
1386 .cntr_size = SZ_4K,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001387 .format_attrs = cci400_pmu_format_attrs,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001388 .event_attrs = cci400_r1_pmu_event_attrs,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001389 .event_ranges = {
1390 [CCI_IF_SLAVE] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001391 CCI400_R1_SLAVE_PORT_MIN_EV,
1392 CCI400_R1_SLAVE_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001393 },
1394 [CCI_IF_MASTER] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001395 CCI400_R1_MASTER_PORT_MIN_EV,
1396 CCI400_R1_MASTER_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001397 },
1398 },
Suzuki K. Poulose31216292015-05-26 10:53:13 +01001399 .validate_hw_event = cci400_validate_hw_event,
1400 .get_event_idx = cci400_get_event_idx,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001401 },
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001402#endif
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001403#ifdef CONFIG_ARM_CCI500_PMU
1404 [CCI500_R0] = {
1405 .name = "CCI_500",
1406 .fixed_hw_cntrs = 0,
1407 .num_hw_cntrs = 8,
1408 .cntr_size = SZ_64K,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001409 .format_attrs = cci500_pmu_format_attrs,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001410 .event_attrs = cci500_pmu_event_attrs,
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001411 .event_ranges = {
1412 [CCI_IF_SLAVE] = {
1413 CCI500_SLAVE_PORT_MIN_EV,
1414 CCI500_SLAVE_PORT_MAX_EV,
1415 },
1416 [CCI_IF_MASTER] = {
1417 CCI500_MASTER_PORT_MIN_EV,
1418 CCI500_MASTER_PORT_MAX_EV,
1419 },
1420 [CCI_IF_GLOBAL] = {
1421 CCI500_GLOBAL_PORT_MIN_EV,
1422 CCI500_GLOBAL_PORT_MAX_EV,
1423 },
1424 },
1425 .validate_hw_event = cci500_validate_hw_event,
1426 },
1427#endif
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001428};
1429
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001430static const struct of_device_id arm_cci_pmu_matches[] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001431#ifdef CONFIG_ARM_CCI400_PMU
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001432 {
1433 .compatible = "arm,cci-400-pmu",
Suzuki K. Poulose772742a2015-03-18 12:24:40 +00001434 .data = NULL,
1435 },
1436 {
1437 .compatible = "arm,cci-400-pmu,r0",
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001438 .data = &cci_pmu_models[CCI400_R0],
Suzuki K. Poulose772742a2015-03-18 12:24:40 +00001439 },
1440 {
1441 .compatible = "arm,cci-400-pmu,r1",
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001442 .data = &cci_pmu_models[CCI400_R1],
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001443 },
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001444#endif
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001445#ifdef CONFIG_ARM_CCI500_PMU
1446 {
1447 .compatible = "arm,cci-500-pmu,r0",
1448 .data = &cci_pmu_models[CCI500_R0],
1449 },
1450#endif
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001451 {},
1452};
1453
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001454static inline const struct cci_pmu_model *get_cci_model(struct platform_device *pdev)
1455{
1456 const struct of_device_id *match = of_match_node(arm_cci_pmu_matches,
1457 pdev->dev.of_node);
1458 if (!match)
1459 return NULL;
Suzuki K. Poulose772742a2015-03-18 12:24:40 +00001460 if (match->data)
1461 return match->data;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001462
Suzuki K. Poulose772742a2015-03-18 12:24:40 +00001463 dev_warn(&pdev->dev, "DEPRECATED compatible property,"
1464 "requires secure access to CCI registers");
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001465 return probe_cci_model(pdev);
1466}
1467
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001468static bool is_duplicate_irq(int irq, int *irqs, int nr_irqs)
1469{
1470 int i;
1471
1472 for (i = 0; i < nr_irqs; i++)
1473 if (irq == irqs[i])
1474 return true;
1475
1476 return false;
1477}
1478
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001479static struct cci_pmu *cci_pmu_alloc(struct platform_device *pdev)
1480{
1481 struct cci_pmu *cci_pmu;
1482 const struct cci_pmu_model *model;
1483
1484 /*
1485 * All allocations are devm_* hence we don't have to free
1486 * them explicitly on an error, as it would end up in driver
1487 * detach.
1488 */
1489 model = get_cci_model(pdev);
1490 if (!model) {
1491 dev_warn(&pdev->dev, "CCI PMU version not supported\n");
1492 return ERR_PTR(-ENODEV);
1493 }
1494
1495 cci_pmu = devm_kzalloc(&pdev->dev, sizeof(*cci_pmu), GFP_KERNEL);
1496 if (!cci_pmu)
1497 return ERR_PTR(-ENOMEM);
1498
1499 cci_pmu->model = model;
1500 cci_pmu->irqs = devm_kcalloc(&pdev->dev, CCI_PMU_MAX_HW_CNTRS(model),
1501 sizeof(*cci_pmu->irqs), GFP_KERNEL);
1502 if (!cci_pmu->irqs)
1503 return ERR_PTR(-ENOMEM);
1504 cci_pmu->hw_events.events = devm_kcalloc(&pdev->dev,
1505 CCI_PMU_MAX_HW_CNTRS(model),
1506 sizeof(*cci_pmu->hw_events.events),
1507 GFP_KERNEL);
1508 if (!cci_pmu->hw_events.events)
1509 return ERR_PTR(-ENOMEM);
1510 cci_pmu->hw_events.used_mask = devm_kcalloc(&pdev->dev,
1511 BITS_TO_LONGS(CCI_PMU_MAX_HW_CNTRS(model)),
1512 sizeof(*cci_pmu->hw_events.used_mask),
1513 GFP_KERNEL);
1514 if (!cci_pmu->hw_events.used_mask)
1515 return ERR_PTR(-ENOMEM);
1516
1517 return cci_pmu;
1518}
1519
1520
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001521static int cci_pmu_probe(struct platform_device *pdev)
1522{
1523 struct resource *res;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001524 struct cci_pmu *cci_pmu;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001525 int i, ret, irq;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001526
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001527 cci_pmu = cci_pmu_alloc(pdev);
1528 if (IS_ERR(cci_pmu))
1529 return PTR_ERR(cci_pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001530
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001531 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001532 cci_pmu->base = devm_ioremap_resource(&pdev->dev, res);
1533 if (IS_ERR(cci_pmu->base))
Wei Yongjunfee4f2c2013-09-22 06:04:23 +01001534 return -ENOMEM;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001535
1536 /*
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001537 * CCI PMU has one overflow interrupt per counter; but some may be tied
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001538 * together to a common interrupt.
1539 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001540 cci_pmu->nr_irqs = 0;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001541 for (i = 0; i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model); i++) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001542 irq = platform_get_irq(pdev, i);
1543 if (irq < 0)
1544 break;
1545
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001546 if (is_duplicate_irq(irq, cci_pmu->irqs, cci_pmu->nr_irqs))
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001547 continue;
1548
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001549 cci_pmu->irqs[cci_pmu->nr_irqs++] = irq;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001550 }
1551
1552 /*
1553 * Ensure that the device tree has as many interrupts as the number
1554 * of counters.
1555 */
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001556 if (i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model)) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001557 dev_warn(&pdev->dev, "In-correct number of interrupts: %d, should be %d\n",
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001558 i, CCI_PMU_MAX_HW_CNTRS(cci_pmu->model));
Wei Yongjunfee4f2c2013-09-22 06:04:23 +01001559 return -EINVAL;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001560 }
1561
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001562 raw_spin_lock_init(&cci_pmu->hw_events.pmu_lock);
1563 mutex_init(&cci_pmu->reserve_mutex);
1564 atomic_set(&cci_pmu->active_events, 0);
1565 cpumask_set_cpu(smp_processor_id(), &cci_pmu->cpus);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001566
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001567 cci_pmu->cpu_nb = (struct notifier_block) {
1568 .notifier_call = cci_pmu_cpu_notifier,
1569 /*
1570 * to migrate uncore events, our notifier should be executed
1571 * before perf core's notifier.
1572 */
1573 .priority = CPU_PRI_PERF + 1,
1574 };
1575
1576 ret = register_cpu_notifier(&cci_pmu->cpu_nb);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001577 if (ret)
1578 return ret;
1579
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001580 ret = cci_pmu_init(cci_pmu, pdev);
1581 if (ret) {
1582 unregister_cpu_notifier(&cci_pmu->cpu_nb);
Wei Yongjunfee4f2c2013-09-22 06:04:23 +01001583 return ret;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001584 }
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001585
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001586 pr_info("ARM %s PMU driver probed", cci_pmu->model->name);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001587 return 0;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001588}
1589
1590static int cci_platform_probe(struct platform_device *pdev)
1591{
1592 if (!cci_probed())
1593 return -ENODEV;
1594
1595 return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
1596}
1597
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001598static struct platform_driver cci_pmu_driver = {
1599 .driver = {
1600 .name = DRIVER_NAME_PMU,
1601 .of_match_table = arm_cci_pmu_matches,
1602 },
1603 .probe = cci_pmu_probe,
1604};
1605
1606static struct platform_driver cci_platform_driver = {
1607 .driver = {
1608 .name = DRIVER_NAME,
1609 .of_match_table = arm_cci_matches,
1610 },
1611 .probe = cci_platform_probe,
1612};
1613
1614static int __init cci_platform_init(void)
1615{
1616 int ret;
1617
1618 ret = platform_driver_register(&cci_pmu_driver);
1619 if (ret)
1620 return ret;
1621
1622 return platform_driver_register(&cci_platform_driver);
1623}
1624
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001625#else /* !CONFIG_ARM_CCI_PMU */
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001626
1627static int __init cci_platform_init(void)
1628{
1629 return 0;
1630}
1631
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001632#endif /* CONFIG_ARM_CCI_PMU */
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +00001633
1634#ifdef CONFIG_ARM_CCI400_PORT_CTRL
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001635
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001636#define CCI_PORT_CTRL 0x0
1637#define CCI_CTRL_STATUS 0xc
1638
1639#define CCI_ENABLE_SNOOP_REQ 0x1
1640#define CCI_ENABLE_DVM_REQ 0x2
1641#define CCI_ENABLE_REQ (CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ)
1642
1643enum cci_ace_port_type {
1644 ACE_INVALID_PORT = 0x0,
1645 ACE_PORT,
1646 ACE_LITE_PORT,
1647};
1648
1649struct cci_ace_port {
1650 void __iomem *base;
1651 unsigned long phys;
1652 enum cci_ace_port_type type;
1653 struct device_node *dn;
1654};
1655
1656static struct cci_ace_port *ports;
1657static unsigned int nb_cci_ports;
1658
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001659struct cpu_port {
1660 u64 mpidr;
1661 u32 port;
1662};
Nicolas Pitre62158f82013-05-21 23:34:41 -04001663
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001664/*
1665 * Use the port MSB as valid flag, shift can be made dynamic
1666 * by computing number of bits required for port indexes.
1667 * Code disabling CCI cpu ports runs with D-cache invalidated
1668 * and SCTLR bit clear so data accesses must be kept to a minimum
1669 * to improve performance; for now shift is left static to
1670 * avoid one more data access while disabling the CCI port.
1671 */
1672#define PORT_VALID_SHIFT 31
1673#define PORT_VALID (0x1 << PORT_VALID_SHIFT)
1674
1675static inline void init_cpu_port(struct cpu_port *port, u32 index, u64 mpidr)
1676{
1677 port->port = PORT_VALID | index;
1678 port->mpidr = mpidr;
1679}
1680
1681static inline bool cpu_port_is_valid(struct cpu_port *port)
1682{
1683 return !!(port->port & PORT_VALID);
1684}
1685
1686static inline bool cpu_port_match(struct cpu_port *port, u64 mpidr)
1687{
1688 return port->mpidr == (mpidr & MPIDR_HWID_BITMASK);
1689}
1690
1691static struct cpu_port cpu_port[NR_CPUS];
1692
1693/**
1694 * __cci_ace_get_port - Function to retrieve the port index connected to
1695 * a cpu or device.
1696 *
1697 * @dn: device node of the device to look-up
1698 * @type: port type
1699 *
1700 * Return value:
1701 * - CCI port index if success
1702 * - -ENODEV if failure
1703 */
1704static int __cci_ace_get_port(struct device_node *dn, int type)
1705{
1706 int i;
1707 bool ace_match;
1708 struct device_node *cci_portn;
1709
1710 cci_portn = of_parse_phandle(dn, "cci-control-port", 0);
1711 for (i = 0; i < nb_cci_ports; i++) {
1712 ace_match = ports[i].type == type;
1713 if (ace_match && cci_portn == ports[i].dn)
1714 return i;
1715 }
1716 return -ENODEV;
1717}
1718
1719int cci_ace_get_port(struct device_node *dn)
1720{
1721 return __cci_ace_get_port(dn, ACE_LITE_PORT);
1722}
1723EXPORT_SYMBOL_GPL(cci_ace_get_port);
1724
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001725static void cci_ace_init_ports(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001726{
Sudeep KarkadaNagesha78b4d6e2013-06-17 14:51:48 +01001727 int port, cpu;
1728 struct device_node *cpun;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001729
1730 /*
1731 * Port index look-up speeds up the function disabling ports by CPU,
1732 * since the logical to port index mapping is done once and does
1733 * not change after system boot.
1734 * The stashed index array is initialized for all possible CPUs
1735 * at probe time.
1736 */
Sudeep KarkadaNagesha78b4d6e2013-06-17 14:51:48 +01001737 for_each_possible_cpu(cpu) {
1738 /* too early to use cpu->of_node */
1739 cpun = of_get_cpu_node(cpu, NULL);
1740
1741 if (WARN(!cpun, "Missing cpu device node\n"))
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001742 continue;
1743
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001744 port = __cci_ace_get_port(cpun, ACE_PORT);
1745 if (port < 0)
1746 continue;
1747
1748 init_cpu_port(&cpu_port[cpu], port, cpu_logical_map(cpu));
1749 }
1750
1751 for_each_possible_cpu(cpu) {
1752 WARN(!cpu_port_is_valid(&cpu_port[cpu]),
1753 "CPU %u does not have an associated CCI port\n",
1754 cpu);
1755 }
1756}
1757/*
1758 * Functions to enable/disable a CCI interconnect slave port
1759 *
1760 * They are called by low-level power management code to disable slave
1761 * interfaces snoops and DVM broadcast.
1762 * Since they may execute with cache data allocation disabled and
1763 * after the caches have been cleaned and invalidated the functions provide
1764 * no explicit locking since they may run with D-cache disabled, so normal
1765 * cacheable kernel locks based on ldrex/strex may not work.
1766 * Locking has to be provided by BSP implementations to ensure proper
1767 * operations.
1768 */
1769
1770/**
1771 * cci_port_control() - function to control a CCI port
1772 *
1773 * @port: index of the port to setup
1774 * @enable: if true enables the port, if false disables it
1775 */
1776static void notrace cci_port_control(unsigned int port, bool enable)
1777{
1778 void __iomem *base = ports[port].base;
1779
1780 writel_relaxed(enable ? CCI_ENABLE_REQ : 0, base + CCI_PORT_CTRL);
1781 /*
1782 * This function is called from power down procedures
1783 * and must not execute any instruction that might
1784 * cause the processor to be put in a quiescent state
1785 * (eg wfi). Hence, cpu_relax() can not be added to this
1786 * read loop to optimize power, since it might hide possibly
1787 * disruptive operations.
1788 */
1789 while (readl_relaxed(cci_ctrl_base + CCI_CTRL_STATUS) & 0x1)
1790 ;
1791}
1792
1793/**
1794 * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
1795 * reference
1796 *
1797 * @mpidr: mpidr of the CPU whose CCI port should be disabled
1798 *
1799 * Disabling a CCI port for a CPU implies disabling the CCI port
1800 * controlling that CPU cluster. Code disabling CPU CCI ports
1801 * must make sure that the CPU running the code is the last active CPU
1802 * in the cluster ie all other CPUs are quiescent in a low power state.
1803 *
1804 * Return:
1805 * 0 on success
1806 * -ENODEV on port look-up failure
1807 */
1808int notrace cci_disable_port_by_cpu(u64 mpidr)
1809{
1810 int cpu;
1811 bool is_valid;
1812 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
1813 is_valid = cpu_port_is_valid(&cpu_port[cpu]);
1814 if (is_valid && cpu_port_match(&cpu_port[cpu], mpidr)) {
1815 cci_port_control(cpu_port[cpu].port, false);
1816 return 0;
1817 }
1818 }
1819 return -ENODEV;
1820}
1821EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu);
1822
1823/**
Nicolas Pitre62158f82013-05-21 23:34:41 -04001824 * cci_enable_port_for_self() - enable a CCI port for calling CPU
1825 *
1826 * Enabling a CCI port for the calling CPU implies enabling the CCI
1827 * port controlling that CPU's cluster. Caller must make sure that the
1828 * CPU running the code is the first active CPU in the cluster and all
1829 * other CPUs are quiescent in a low power state or waiting for this CPU
1830 * to complete the CCI initialization.
1831 *
1832 * Because this is called when the MMU is still off and with no stack,
1833 * the code must be position independent and ideally rely on callee
1834 * clobbered registers only. To achieve this we must code this function
1835 * entirely in assembler.
1836 *
1837 * On success this returns with the proper CCI port enabled. In case of
1838 * any failure this never returns as the inability to enable the CCI is
1839 * fatal and there is no possible recovery at this stage.
1840 */
1841asmlinkage void __naked cci_enable_port_for_self(void)
1842{
1843 asm volatile ("\n"
Arnd Bergmannf4902492013-06-03 15:15:36 +02001844" .arch armv7-a\n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04001845" mrc p15, 0, r0, c0, c0, 5 @ get MPIDR value \n"
1846" and r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n"
1847" adr r1, 5f \n"
1848" ldr r2, [r1] \n"
1849" add r1, r1, r2 @ &cpu_port \n"
1850" add ip, r1, %[sizeof_cpu_port] \n"
1851
1852 /* Loop over the cpu_port array looking for a matching MPIDR */
1853"1: ldr r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
1854" cmp r2, r0 @ compare MPIDR \n"
1855" bne 2f \n"
1856
1857 /* Found a match, now test port validity */
1858" ldr r3, [r1, %[offsetof_cpu_port_port]] \n"
1859" tst r3, #"__stringify(PORT_VALID)" \n"
1860" bne 3f \n"
1861
1862 /* no match, loop with the next cpu_port entry */
1863"2: add r1, r1, %[sizeof_struct_cpu_port] \n"
1864" cmp r1, ip @ done? \n"
1865" blo 1b \n"
1866
1867 /* CCI port not found -- cheaply try to stall this CPU */
1868"cci_port_not_found: \n"
1869" wfi \n"
1870" wfe \n"
1871" b cci_port_not_found \n"
1872
1873 /* Use matched port index to look up the corresponding ports entry */
1874"3: bic r3, r3, #"__stringify(PORT_VALID)" \n"
1875" adr r0, 6f \n"
1876" ldmia r0, {r1, r2} \n"
1877" sub r1, r1, r0 @ virt - phys \n"
1878" ldr r0, [r0, r2] @ *(&ports) \n"
1879" mov r2, %[sizeof_struct_ace_port] \n"
1880" mla r0, r2, r3, r0 @ &ports[index] \n"
1881" sub r0, r0, r1 @ virt_to_phys() \n"
1882
1883 /* Enable the CCI port */
1884" ldr r0, [r0, %[offsetof_port_phys]] \n"
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07001885" mov r3, %[cci_enable_req]\n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04001886" str r3, [r0, #"__stringify(CCI_PORT_CTRL)"] \n"
1887
1888 /* poll the status reg for completion */
1889" adr r1, 7f \n"
1890" ldr r0, [r1] \n"
1891" ldr r0, [r0, r1] @ cci_ctrl_base \n"
1892"4: ldr r1, [r0, #"__stringify(CCI_CTRL_STATUS)"] \n"
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07001893" tst r1, %[cci_control_status_bits] \n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04001894" bne 4b \n"
1895
1896" mov r0, #0 \n"
1897" bx lr \n"
1898
1899" .align 2 \n"
1900"5: .word cpu_port - . \n"
1901"6: .word . \n"
1902" .word ports - 6b \n"
1903"7: .word cci_ctrl_phys - . \n"
1904 : :
1905 [sizeof_cpu_port] "i" (sizeof(cpu_port)),
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07001906 [cci_enable_req] "i" cpu_to_le32(CCI_ENABLE_REQ),
1907 [cci_control_status_bits] "i" cpu_to_le32(1),
Nicolas Pitre62158f82013-05-21 23:34:41 -04001908#ifndef __ARMEB__
1909 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
1910#else
1911 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
1912#endif
1913 [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
1914 [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
1915 [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
1916 [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
1917
1918 unreachable();
1919}
1920
1921/**
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001922 * __cci_control_port_by_device() - function to control a CCI port by device
1923 * reference
1924 *
1925 * @dn: device node pointer of the device whose CCI port should be
1926 * controlled
1927 * @enable: if true enables the port, if false disables it
1928 *
1929 * Return:
1930 * 0 on success
1931 * -ENODEV on port look-up failure
1932 */
1933int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
1934{
1935 int port;
1936
1937 if (!dn)
1938 return -ENODEV;
1939
1940 port = __cci_ace_get_port(dn, ACE_LITE_PORT);
1941 if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n",
1942 dn->full_name))
1943 return -ENODEV;
1944 cci_port_control(port, enable);
1945 return 0;
1946}
1947EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
1948
1949/**
1950 * __cci_control_port_by_index() - function to control a CCI port by port index
1951 *
1952 * @port: port index previously retrieved with cci_ace_get_port()
1953 * @enable: if true enables the port, if false disables it
1954 *
1955 * Return:
1956 * 0 on success
1957 * -ENODEV on port index out of range
1958 * -EPERM if operation carried out on an ACE PORT
1959 */
1960int notrace __cci_control_port_by_index(u32 port, bool enable)
1961{
1962 if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
1963 return -ENODEV;
1964 /*
1965 * CCI control for ports connected to CPUS is extremely fragile
1966 * and must be made to go through a specific and controlled
1967 * interface (ie cci_disable_port_by_cpu(); control by general purpose
1968 * indexing is therefore disabled for ACE ports.
1969 */
1970 if (ports[port].type == ACE_PORT)
1971 return -EPERM;
1972
1973 cci_port_control(port, enable);
1974 return 0;
1975}
1976EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
1977
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001978static const struct of_device_id arm_cci_ctrl_if_matches[] = {
1979 {.compatible = "arm,cci-400-ctrl-if", },
1980 {},
1981};
1982
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001983static int cci_probe_ports(struct device_node *np)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001984{
1985 struct cci_nb_ports const *cci_config;
1986 int ret, i, nb_ace = 0, nb_ace_lite = 0;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001987 struct device_node *cp;
Nicolas Pitre62158f82013-05-21 23:34:41 -04001988 struct resource res;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001989 const char *match_str;
1990 bool is_ace;
1991
Abhilash Kesavan896ddd62015-01-10 08:41:35 +05301992
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001993 cci_config = of_match_node(arm_cci_matches, np)->data;
1994 if (!cci_config)
1995 return -ENODEV;
1996
1997 nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
1998
Lorenzo Pieralisi7c762032014-01-27 10:50:37 +00001999 ports = kcalloc(nb_cci_ports, sizeof(*ports), GFP_KERNEL);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002000 if (!ports)
2001 return -ENOMEM;
2002
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002003 for_each_child_of_node(np, cp) {
2004 if (!of_match_node(arm_cci_ctrl_if_matches, cp))
2005 continue;
2006
2007 i = nb_ace + nb_ace_lite;
2008
2009 if (i >= nb_cci_ports)
2010 break;
2011
2012 if (of_property_read_string(cp, "interface-type",
2013 &match_str)) {
2014 WARN(1, "node %s missing interface-type property\n",
2015 cp->full_name);
2016 continue;
2017 }
2018 is_ace = strcmp(match_str, "ace") == 0;
2019 if (!is_ace && strcmp(match_str, "ace-lite")) {
2020 WARN(1, "node %s containing invalid interface-type property, skipping it\n",
2021 cp->full_name);
2022 continue;
2023 }
2024
Nicolas Pitre62158f82013-05-21 23:34:41 -04002025 ret = of_address_to_resource(cp, 0, &res);
2026 if (!ret) {
2027 ports[i].base = ioremap(res.start, resource_size(&res));
2028 ports[i].phys = res.start;
2029 }
2030 if (ret || !ports[i].base) {
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002031 WARN(1, "unable to ioremap CCI port %d\n", i);
2032 continue;
2033 }
2034
2035 if (is_ace) {
2036 if (WARN_ON(nb_ace >= cci_config->nb_ace))
2037 continue;
2038 ports[i].type = ACE_PORT;
2039 ++nb_ace;
2040 } else {
2041 if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
2042 continue;
2043 ports[i].type = ACE_LITE_PORT;
2044 ++nb_ace_lite;
2045 }
2046 ports[i].dn = cp;
2047 }
2048
2049 /* initialize a stashed array of ACE ports to speed-up look-up */
2050 cci_ace_init_ports();
2051
2052 /*
2053 * Multi-cluster systems may need this data when non-coherent, during
2054 * cluster power-up/power-down. Make sure it reaches main memory.
2055 */
2056 sync_cache_w(&cci_ctrl_base);
Nicolas Pitre62158f82013-05-21 23:34:41 -04002057 sync_cache_w(&cci_ctrl_phys);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002058 sync_cache_w(&ports);
2059 sync_cache_w(&cpu_port);
2060 __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
2061 pr_info("ARM CCI driver probed\n");
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002062
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002063 return 0;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002064}
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +00002065#else /* !CONFIG_ARM_CCI400_PORT_CTRL */
2066static inline int cci_probe_ports(struct device_node *np)
2067{
2068 return 0;
2069}
2070#endif /* CONFIG_ARM_CCI400_PORT_CTRL */
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002071
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002072static int cci_probe(void)
2073{
2074 int ret;
2075 struct device_node *np;
2076 struct resource res;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002077
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002078 np = of_find_matching_node(NULL, arm_cci_matches);
2079 if(!np || !of_device_is_available(np))
2080 return -ENODEV;
2081
2082 ret = of_address_to_resource(np, 0, &res);
2083 if (!ret) {
2084 cci_ctrl_base = ioremap(res.start, resource_size(&res));
2085 cci_ctrl_phys = res.start;
2086 }
2087 if (ret || !cci_ctrl_base) {
2088 WARN(1, "unable to ioremap CCI ctrl\n");
2089 return -ENXIO;
2090 }
2091
2092 return cci_probe_ports(np);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002093}
2094
2095static int cci_init_status = -EAGAIN;
2096static DEFINE_MUTEX(cci_probing);
2097
Punit Agrawalb91c8f22013-08-22 14:41:51 +01002098static int cci_init(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002099{
2100 if (cci_init_status != -EAGAIN)
2101 return cci_init_status;
2102
2103 mutex_lock(&cci_probing);
2104 if (cci_init_status == -EAGAIN)
2105 cci_init_status = cci_probe();
2106 mutex_unlock(&cci_probing);
2107 return cci_init_status;
2108}
2109
2110/*
2111 * To sort out early init calls ordering a helper function is provided to
2112 * check if the CCI driver has beed initialized. Function check if the driver
2113 * has been initialized, if not it calls the init function that probes
2114 * the driver and updates the return value.
2115 */
Punit Agrawalb91c8f22013-08-22 14:41:51 +01002116bool cci_probed(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002117{
2118 return cci_init() == 0;
2119}
2120EXPORT_SYMBOL_GPL(cci_probed);
2121
2122early_initcall(cci_init);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01002123core_initcall(cci_platform_init);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002124MODULE_LICENSE("GPL");
2125MODULE_DESCRIPTION("ARM CCI support");