blob: 629c9e069c3b5b76d0b9c73791c220c2848005e7 [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. Poulosea1a076d2015-05-26 10:53:11 +0100710static void pmu_set_event(struct cci_pmu *cci_pmu, int idx, unsigned long event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100711{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100712 pmu_write_register(cci_pmu, event, idx, CCI_PMU_EVT_SEL);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100713}
714
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100715/*
716 * Returns the number of programmable counters actually implemented
717 * by the cci
718 */
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100719static u32 pmu_get_max_counters(void)
720{
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100721 return (readl_relaxed(cci_ctrl_base + CCI_PMCR) &
722 CCI_PMCR_NCNT_MASK) >> CCI_PMCR_NCNT_SHIFT;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100723}
724
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100725static int pmu_get_event_idx(struct cci_pmu_hw_events *hw, struct perf_event *event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100726{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100727 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100728 unsigned long cci_event = event->hw.config_base;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100729 int idx;
730
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100731 if (cci_pmu->model->get_event_idx)
732 return cci_pmu->model->get_event_idx(cci_pmu, hw, cci_event);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100733
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100734 /* Generic code to find an unused idx from the mask */
735 for(idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100736 if (!test_and_set_bit(idx, hw->used_mask))
737 return idx;
738
739 /* No counters available */
740 return -EAGAIN;
741}
742
743static int pmu_map_event(struct perf_event *event)
744{
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100745 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100746
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100747 if (event->attr.type < PERF_TYPE_MAX ||
748 !cci_pmu->model->validate_hw_event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100749 return -ENOENT;
750
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100751 return cci_pmu->model->validate_hw_event(cci_pmu, event->attr.config);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100752}
753
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100754static int pmu_request_irq(struct cci_pmu *cci_pmu, irq_handler_t handler)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100755{
756 int i;
757 struct platform_device *pmu_device = cci_pmu->plat_device;
758
759 if (unlikely(!pmu_device))
760 return -ENODEV;
761
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100762 if (cci_pmu->nr_irqs < 1) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100763 dev_err(&pmu_device->dev, "no irqs for CCI PMUs defined\n");
764 return -ENODEV;
765 }
766
767 /*
768 * Register all available CCI PMU interrupts. In the interrupt handler
769 * we iterate over the counters checking for interrupt source (the
770 * overflowing counter) and clear it.
771 *
772 * This should allow handling of non-unique interrupt for the counters.
773 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100774 for (i = 0; i < cci_pmu->nr_irqs; i++) {
775 int err = request_irq(cci_pmu->irqs[i], handler, IRQF_SHARED,
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100776 "arm-cci-pmu", cci_pmu);
777 if (err) {
778 dev_err(&pmu_device->dev, "unable to request IRQ%d for ARM CCI PMU counters\n",
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100779 cci_pmu->irqs[i]);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100780 return err;
781 }
782
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100783 set_bit(i, &cci_pmu->active_irqs);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100784 }
785
786 return 0;
787}
788
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100789static void pmu_free_irq(struct cci_pmu *cci_pmu)
790{
791 int i;
792
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100793 for (i = 0; i < cci_pmu->nr_irqs; i++) {
794 if (!test_and_clear_bit(i, &cci_pmu->active_irqs))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100795 continue;
796
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100797 free_irq(cci_pmu->irqs[i], cci_pmu);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100798 }
799}
800
801static u32 pmu_read_counter(struct perf_event *event)
802{
803 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
804 struct hw_perf_event *hw_counter = &event->hw;
805 int idx = hw_counter->idx;
806 u32 value;
807
808 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
809 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
810 return 0;
811 }
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100812 value = pmu_read_register(cci_pmu, idx, CCI_PMU_CNTR);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100813
814 return value;
815}
816
817static void pmu_write_counter(struct perf_event *event, u32 value)
818{
819 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
820 struct hw_perf_event *hw_counter = &event->hw;
821 int idx = hw_counter->idx;
822
823 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx)))
824 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
825 else
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100826 pmu_write_register(cci_pmu, value, idx, CCI_PMU_CNTR);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100827}
828
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000829static void pmu_write_counters(struct cci_pmu *cci_pmu, unsigned long *mask)
Suzuki K Poulosea53eb5c2016-02-23 10:49:45 +0000830{
831 int i;
832 struct cci_pmu_hw_events *cci_hw = &cci_pmu->hw_events;
833
834 for_each_set_bit(i, mask, cci_pmu->num_cntrs) {
835 struct perf_event *event = cci_hw->events[i];
836
837 if (WARN_ON(!event))
838 continue;
839 pmu_write_counter(event, local64_read(&event->hw.prev_count));
840 }
841}
842
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100843static u64 pmu_event_update(struct perf_event *event)
844{
845 struct hw_perf_event *hwc = &event->hw;
846 u64 delta, prev_raw_count, new_raw_count;
847
848 do {
849 prev_raw_count = local64_read(&hwc->prev_count);
850 new_raw_count = pmu_read_counter(event);
851 } while (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
852 new_raw_count) != prev_raw_count);
853
854 delta = (new_raw_count - prev_raw_count) & CCI_PMU_CNTR_MASK;
855
856 local64_add(delta, &event->count);
857
858 return new_raw_count;
859}
860
861static void pmu_read(struct perf_event *event)
862{
863 pmu_event_update(event);
864}
865
866void pmu_event_set_period(struct perf_event *event)
867{
868 struct hw_perf_event *hwc = &event->hw;
869 /*
870 * The CCI PMU counters have a period of 2^32. To account for the
871 * possiblity of extreme interrupt latency we program for a period of
872 * half that. Hopefully we can handle the interrupt before another 2^31
873 * events occur and the counter overtakes its previous value.
874 */
875 u64 val = 1ULL << 31;
876 local64_set(&hwc->prev_count, val);
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000877
878 /*
879 * CCI PMU uses PERF_HES_ARCH to keep track of the counters, whose
880 * values needs to be sync-ed with the s/w state before the PMU is
881 * enabled.
882 * Mark this counter for sync.
883 */
884 hwc->state |= PERF_HES_ARCH;
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100885}
886
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100887static irqreturn_t pmu_handle_irq(int irq_num, void *dev)
888{
889 unsigned long flags;
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100890 struct cci_pmu *cci_pmu = dev;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100891 struct cci_pmu_hw_events *events = &cci_pmu->hw_events;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100892 int idx, handled = IRQ_NONE;
893
894 raw_spin_lock_irqsave(&events->pmu_lock, flags);
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000895
896 /* Disable the PMU while we walk through the counters */
897 __cci_pmu_disable();
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100898 /*
899 * Iterate over counters and update the corresponding perf events.
900 * This should work regardless of whether we have per-counter overflow
901 * interrupt or a combined overflow interrupt.
902 */
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100903 for (idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100904 struct perf_event *event = events->events[idx];
905 struct hw_perf_event *hw_counter;
906
907 if (!event)
908 continue;
909
910 hw_counter = &event->hw;
911
912 /* Did this counter overflow? */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100913 if (!(pmu_read_register(cci_pmu, idx, CCI_PMU_OVRFLW) &
Himangi Saraogifc5130d2014-07-30 11:37:35 +0100914 CCI_PMU_OVRFLW_FLAG))
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100915 continue;
916
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100917 pmu_write_register(cci_pmu, CCI_PMU_OVRFLW_FLAG, idx,
918 CCI_PMU_OVRFLW);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100919
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100920 pmu_event_update(event);
921 pmu_event_set_period(event);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100922 handled = IRQ_HANDLED;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100923 }
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000924
925 /* Enable the PMU and sync possibly overflowed counters */
926 __cci_pmu_enable(cci_pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100927 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
928
929 return IRQ_RETVAL(handled);
930}
931
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100932static int cci_pmu_get_hw(struct cci_pmu *cci_pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100933{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100934 int ret = pmu_request_irq(cci_pmu, pmu_handle_irq);
935 if (ret) {
936 pmu_free_irq(cci_pmu);
937 return ret;
938 }
939 return 0;
940}
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100941
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100942static void cci_pmu_put_hw(struct cci_pmu *cci_pmu)
943{
944 pmu_free_irq(cci_pmu);
945}
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100946
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100947static void hw_perf_event_destroy(struct perf_event *event)
948{
949 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
950 atomic_t *active_events = &cci_pmu->active_events;
951 struct mutex *reserve_mutex = &cci_pmu->reserve_mutex;
952
953 if (atomic_dec_and_mutex_lock(active_events, reserve_mutex)) {
954 cci_pmu_put_hw(cci_pmu);
955 mutex_unlock(reserve_mutex);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100956 }
957}
958
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100959static void cci_pmu_enable(struct pmu *pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100960{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100961 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
962 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100963 int enabled = bitmap_weight(hw_events->used_mask, cci_pmu->num_cntrs);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100964 unsigned long flags;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100965
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100966 if (!enabled)
967 return;
968
969 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
Suzuki K Poulosec66eea52016-02-23 10:49:47 +0000970 __cci_pmu_enable(cci_pmu);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100971 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100972
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100973}
974
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100975static void cci_pmu_disable(struct pmu *pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100976{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100977 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
978 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100979 unsigned long flags;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100980
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100981 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
Suzuki K Poulosea077c522016-02-23 10:49:46 +0000982 __cci_pmu_disable();
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100983 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100984}
985
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100986/*
987 * Check if the idx represents a non-programmable counter.
988 * All the fixed event counters are mapped before the programmable
989 * counters.
990 */
991static bool pmu_fixed_hw_idx(struct cci_pmu *cci_pmu, int idx)
992{
993 return (idx >= 0) && (idx < cci_pmu->model->fixed_hw_cntrs);
994}
995
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100996static void cci_pmu_start(struct perf_event *event, int pmu_flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100997{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100998 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
999 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
1000 struct hw_perf_event *hwc = &event->hw;
1001 int idx = hwc->idx;
1002 unsigned long flags;
1003
1004 /*
1005 * To handle interrupt latency, we always reprogram the period
1006 * regardlesss of PERF_EF_RELOAD.
1007 */
1008 if (pmu_flags & PERF_EF_RELOAD)
1009 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
1010
1011 hwc->state = 0;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001012
1013 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
1014 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001015 return;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001016 }
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001017
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001018 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
1019
Suzuki K. Poulose31216292015-05-26 10:53:13 +01001020 /* Configure the counter unless you are counting a fixed event */
1021 if (!pmu_fixed_hw_idx(cci_pmu, idx))
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001022 pmu_set_event(cci_pmu, idx, hwc->config_base);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001023
1024 pmu_event_set_period(event);
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001025 pmu_enable_counter(cci_pmu, idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001026
1027 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001028}
1029
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001030static void cci_pmu_stop(struct perf_event *event, int pmu_flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001031{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001032 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1033 struct hw_perf_event *hwc = &event->hw;
1034 int idx = hwc->idx;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001035
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001036 if (hwc->state & PERF_HES_STOPPED)
1037 return;
1038
1039 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001040 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001041 return;
1042 }
1043
1044 /*
1045 * We always reprogram the counter, so ignore PERF_EF_UPDATE. See
1046 * cci_pmu_start()
1047 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001048 pmu_disable_counter(cci_pmu, idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001049 pmu_event_update(event);
1050 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001051}
1052
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001053static int cci_pmu_add(struct perf_event *event, int flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001054{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001055 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1056 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
1057 struct hw_perf_event *hwc = &event->hw;
1058 int idx;
1059 int err = 0;
1060
1061 perf_pmu_disable(event->pmu);
1062
1063 /* If we don't have a space for the counter then finish early. */
1064 idx = pmu_get_event_idx(hw_events, event);
1065 if (idx < 0) {
1066 err = idx;
1067 goto out;
1068 }
1069
1070 event->hw.idx = idx;
1071 hw_events->events[idx] = event;
1072
1073 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
1074 if (flags & PERF_EF_START)
1075 cci_pmu_start(event, PERF_EF_RELOAD);
1076
1077 /* Propagate our changes to the userspace mapping. */
1078 perf_event_update_userpage(event);
1079
1080out:
1081 perf_pmu_enable(event->pmu);
1082 return err;
1083}
1084
1085static void cci_pmu_del(struct perf_event *event, int flags)
1086{
1087 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1088 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
1089 struct hw_perf_event *hwc = &event->hw;
1090 int idx = hwc->idx;
1091
1092 cci_pmu_stop(event, PERF_EF_UPDATE);
1093 hw_events->events[idx] = NULL;
1094 clear_bit(idx, hw_events->used_mask);
1095
1096 perf_event_update_userpage(event);
1097}
1098
1099static int
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001100validate_event(struct pmu *cci_pmu,
1101 struct cci_pmu_hw_events *hw_events,
1102 struct perf_event *event)
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001103{
1104 if (is_software_event(event))
1105 return 1;
1106
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001107 /*
1108 * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
1109 * core perf code won't check that the pmu->ctx == leader->ctx
1110 * until after pmu->event_init(event).
1111 */
1112 if (event->pmu != cci_pmu)
1113 return 0;
1114
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001115 if (event->state < PERF_EVENT_STATE_OFF)
1116 return 1;
1117
1118 if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
1119 return 1;
1120
1121 return pmu_get_event_idx(hw_events, event) >= 0;
1122}
1123
1124static int
1125validate_group(struct perf_event *event)
1126{
1127 struct perf_event *sibling, *leader = event->group_leader;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001128 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1129 unsigned long mask[BITS_TO_LONGS(cci_pmu->num_cntrs)];
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001130 struct cci_pmu_hw_events fake_pmu = {
1131 /*
1132 * Initialise the fake PMU. We only need to populate the
1133 * used_mask for the purposes of validation.
1134 */
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001135 .used_mask = mask,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001136 };
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001137 memset(mask, 0, BITS_TO_LONGS(cci_pmu->num_cntrs) * sizeof(unsigned long));
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001138
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001139 if (!validate_event(event->pmu, &fake_pmu, leader))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001140 return -EINVAL;
1141
1142 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001143 if (!validate_event(event->pmu, &fake_pmu, sibling))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001144 return -EINVAL;
1145 }
1146
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +00001147 if (!validate_event(event->pmu, &fake_pmu, event))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001148 return -EINVAL;
1149
1150 return 0;
1151}
1152
1153static int
1154__hw_perf_event_init(struct perf_event *event)
1155{
1156 struct hw_perf_event *hwc = &event->hw;
1157 int mapping;
1158
1159 mapping = pmu_map_event(event);
1160
1161 if (mapping < 0) {
1162 pr_debug("event %x:%llx not supported\n", event->attr.type,
1163 event->attr.config);
1164 return mapping;
1165 }
1166
1167 /*
1168 * We don't assign an index until we actually place the event onto
1169 * hardware. Use -1 to signify that we haven't decided where to put it
1170 * yet.
1171 */
1172 hwc->idx = -1;
1173 hwc->config_base = 0;
1174 hwc->config = 0;
1175 hwc->event_base = 0;
1176
1177 /*
1178 * Store the event encoding into the config_base field.
1179 */
1180 hwc->config_base |= (unsigned long)mapping;
1181
1182 /*
1183 * Limit the sample_period to half of the counter width. That way, the
1184 * new counter value is far less likely to overtake the previous one
1185 * unless you have some serious IRQ latency issues.
1186 */
1187 hwc->sample_period = CCI_PMU_CNTR_MASK >> 1;
1188 hwc->last_period = hwc->sample_period;
1189 local64_set(&hwc->period_left, hwc->sample_period);
1190
1191 if (event->group_leader != event) {
1192 if (validate_group(event) != 0)
1193 return -EINVAL;
1194 }
1195
1196 return 0;
1197}
1198
1199static int cci_pmu_event_init(struct perf_event *event)
1200{
1201 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
1202 atomic_t *active_events = &cci_pmu->active_events;
1203 int err = 0;
1204 int cpu;
1205
1206 if (event->attr.type != event->pmu->type)
1207 return -ENOENT;
1208
1209 /* Shared by all CPUs, no meaningful state to sample */
1210 if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
1211 return -EOPNOTSUPP;
1212
1213 /* We have no filtering of any kind */
1214 if (event->attr.exclude_user ||
1215 event->attr.exclude_kernel ||
1216 event->attr.exclude_hv ||
1217 event->attr.exclude_idle ||
1218 event->attr.exclude_host ||
1219 event->attr.exclude_guest)
1220 return -EINVAL;
1221
1222 /*
1223 * Following the example set by other "uncore" PMUs, we accept any CPU
1224 * and rewrite its affinity dynamically rather than having perf core
1225 * handle cpu == -1 and pid == -1 for this case.
1226 *
1227 * The perf core will pin online CPUs for the duration of this call and
1228 * the event being installed into its context, so the PMU's CPU can't
1229 * change under our feet.
1230 */
1231 cpu = cpumask_first(&cci_pmu->cpus);
1232 if (event->cpu < 0 || cpu < 0)
1233 return -EINVAL;
1234 event->cpu = cpu;
1235
1236 event->destroy = hw_perf_event_destroy;
1237 if (!atomic_inc_not_zero(active_events)) {
1238 mutex_lock(&cci_pmu->reserve_mutex);
1239 if (atomic_read(active_events) == 0)
1240 err = cci_pmu_get_hw(cci_pmu);
1241 if (!err)
1242 atomic_inc(active_events);
1243 mutex_unlock(&cci_pmu->reserve_mutex);
1244 }
1245 if (err)
1246 return err;
1247
1248 err = __hw_perf_event_init(event);
1249 if (err)
1250 hw_perf_event_destroy(event);
1251
1252 return err;
1253}
1254
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001255static ssize_t pmu_cpumask_attr_show(struct device *dev,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001256 struct device_attribute *attr, char *buf)
1257{
Mark Rutland5e442eb2016-02-23 10:49:43 +00001258 struct pmu *pmu = dev_get_drvdata(dev);
1259 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001260
Tejun Heo660e5ec2015-02-13 14:37:20 -08001261 int n = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001262 cpumask_pr_args(&cci_pmu->cpus));
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001263 buf[n++] = '\n';
1264 buf[n] = '\0';
1265 return n;
1266}
1267
Mark Rutland5e442eb2016-02-23 10:49:43 +00001268static struct device_attribute pmu_cpumask_attr =
1269 __ATTR(cpumask, S_IRUGO, pmu_cpumask_attr_show, NULL);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001270
1271static struct attribute *pmu_attrs[] = {
Mark Rutland5e442eb2016-02-23 10:49:43 +00001272 &pmu_cpumask_attr.attr,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001273 NULL,
1274};
1275
1276static struct attribute_group pmu_attr_group = {
1277 .attrs = pmu_attrs,
1278};
1279
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001280static struct attribute_group pmu_format_attr_group = {
1281 .name = "format",
1282 .attrs = NULL, /* Filled in cci_pmu_init_attrs */
1283};
1284
1285static struct attribute_group pmu_event_attr_group = {
1286 .name = "events",
1287 .attrs = NULL, /* Filled in cci_pmu_init_attrs */
1288};
1289
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001290static const struct attribute_group *pmu_attr_groups[] = {
1291 &pmu_attr_group,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001292 &pmu_format_attr_group,
1293 &pmu_event_attr_group,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001294 NULL
1295};
1296
1297static int cci_pmu_init(struct cci_pmu *cci_pmu, struct platform_device *pdev)
1298{
Mark Rutland5e442eb2016-02-23 10:49:43 +00001299 const struct cci_pmu_model *model = cci_pmu->model;
1300 char *name = model->name;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001301 u32 num_cntrs;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001302
Mark Rutland5e442eb2016-02-23 10:49:43 +00001303 pmu_event_attr_group.attrs = model->event_attrs;
1304 pmu_format_attr_group.attrs = model->format_attrs;
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001305
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001306 cci_pmu->pmu = (struct pmu) {
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001307 .name = cci_pmu->model->name,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001308 .task_ctx_nr = perf_invalid_context,
1309 .pmu_enable = cci_pmu_enable,
1310 .pmu_disable = cci_pmu_disable,
1311 .event_init = cci_pmu_event_init,
1312 .add = cci_pmu_add,
1313 .del = cci_pmu_del,
1314 .start = cci_pmu_start,
1315 .stop = cci_pmu_stop,
1316 .read = pmu_read,
1317 .attr_groups = pmu_attr_groups,
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001318 };
1319
1320 cci_pmu->plat_device = pdev;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001321 num_cntrs = pmu_get_max_counters();
1322 if (num_cntrs > cci_pmu->model->num_hw_cntrs) {
1323 dev_warn(&pdev->dev,
1324 "PMU implements more counters(%d) than supported by"
1325 " the model(%d), truncated.",
1326 num_cntrs, cci_pmu->model->num_hw_cntrs);
1327 num_cntrs = cci_pmu->model->num_hw_cntrs;
1328 }
1329 cci_pmu->num_cntrs = num_cntrs + cci_pmu->model->fixed_hw_cntrs;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001330
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001331 return perf_pmu_register(&cci_pmu->pmu, name, -1);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001332}
1333
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001334static int cci_pmu_cpu_notifier(struct notifier_block *self,
1335 unsigned long action, void *hcpu)
1336{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001337 struct cci_pmu *cci_pmu = container_of(self,
1338 struct cci_pmu, cpu_nb);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001339 unsigned int cpu = (long)hcpu;
1340 unsigned int target;
1341
1342 switch (action & ~CPU_TASKS_FROZEN) {
1343 case CPU_DOWN_PREPARE:
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001344 if (!cpumask_test_and_clear_cpu(cpu, &cci_pmu->cpus))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001345 break;
1346 target = cpumask_any_but(cpu_online_mask, cpu);
Andrzej Hajda0f173802016-02-23 10:49:44 +00001347 if (target >= nr_cpu_ids) // UP, last CPU
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001348 break;
1349 /*
1350 * TODO: migrate context once core races on event->ctx have
1351 * been fixed.
1352 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001353 cpumask_set_cpu(target, &cci_pmu->cpus);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001354 default:
1355 break;
1356 }
1357
1358 return NOTIFY_OK;
1359}
1360
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001361static struct cci_pmu_model cci_pmu_models[] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001362#ifdef CONFIG_ARM_CCI400_PMU
1363 [CCI400_R0] = {
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001364 .name = "CCI_400",
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001365 .fixed_hw_cntrs = 1, /* Cycle counter */
1366 .num_hw_cntrs = 4,
1367 .cntr_size = SZ_4K,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001368 .format_attrs = cci400_pmu_format_attrs,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001369 .event_attrs = cci400_r0_pmu_event_attrs,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001370 .event_ranges = {
1371 [CCI_IF_SLAVE] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001372 CCI400_R0_SLAVE_PORT_MIN_EV,
1373 CCI400_R0_SLAVE_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001374 },
1375 [CCI_IF_MASTER] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001376 CCI400_R0_MASTER_PORT_MIN_EV,
1377 CCI400_R0_MASTER_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001378 },
1379 },
Suzuki K. Poulose31216292015-05-26 10:53:13 +01001380 .validate_hw_event = cci400_validate_hw_event,
1381 .get_event_idx = cci400_get_event_idx,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001382 },
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001383 [CCI400_R1] = {
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001384 .name = "CCI_400_r1",
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001385 .fixed_hw_cntrs = 1, /* Cycle counter */
1386 .num_hw_cntrs = 4,
1387 .cntr_size = SZ_4K,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001388 .format_attrs = cci400_pmu_format_attrs,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001389 .event_attrs = cci400_r1_pmu_event_attrs,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001390 .event_ranges = {
1391 [CCI_IF_SLAVE] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001392 CCI400_R1_SLAVE_PORT_MIN_EV,
1393 CCI400_R1_SLAVE_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001394 },
1395 [CCI_IF_MASTER] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001396 CCI400_R1_MASTER_PORT_MIN_EV,
1397 CCI400_R1_MASTER_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001398 },
1399 },
Suzuki K. Poulose31216292015-05-26 10:53:13 +01001400 .validate_hw_event = cci400_validate_hw_event,
1401 .get_event_idx = cci400_get_event_idx,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001402 },
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001403#endif
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001404#ifdef CONFIG_ARM_CCI500_PMU
1405 [CCI500_R0] = {
1406 .name = "CCI_500",
1407 .fixed_hw_cntrs = 0,
1408 .num_hw_cntrs = 8,
1409 .cntr_size = SZ_64K,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001410 .format_attrs = cci500_pmu_format_attrs,
Suzuki K. Poulosee14cfad2015-05-26 10:53:16 +01001411 .event_attrs = cci500_pmu_event_attrs,
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001412 .event_ranges = {
1413 [CCI_IF_SLAVE] = {
1414 CCI500_SLAVE_PORT_MIN_EV,
1415 CCI500_SLAVE_PORT_MAX_EV,
1416 },
1417 [CCI_IF_MASTER] = {
1418 CCI500_MASTER_PORT_MIN_EV,
1419 CCI500_MASTER_PORT_MAX_EV,
1420 },
1421 [CCI_IF_GLOBAL] = {
1422 CCI500_GLOBAL_PORT_MIN_EV,
1423 CCI500_GLOBAL_PORT_MAX_EV,
1424 },
1425 },
1426 .validate_hw_event = cci500_validate_hw_event,
1427 },
1428#endif
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001429};
1430
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001431static const struct of_device_id arm_cci_pmu_matches[] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001432#ifdef CONFIG_ARM_CCI400_PMU
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001433 {
1434 .compatible = "arm,cci-400-pmu",
Suzuki K. Poulose772742a2015-03-18 12:24:40 +00001435 .data = NULL,
1436 },
1437 {
1438 .compatible = "arm,cci-400-pmu,r0",
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001439 .data = &cci_pmu_models[CCI400_R0],
Suzuki K. Poulose772742a2015-03-18 12:24:40 +00001440 },
1441 {
1442 .compatible = "arm,cci-400-pmu,r1",
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001443 .data = &cci_pmu_models[CCI400_R1],
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001444 },
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001445#endif
Suzuki K. Poulosea95791e2015-05-26 10:53:15 +01001446#ifdef CONFIG_ARM_CCI500_PMU
1447 {
1448 .compatible = "arm,cci-500-pmu,r0",
1449 .data = &cci_pmu_models[CCI500_R0],
1450 },
1451#endif
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001452 {},
1453};
1454
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001455static inline const struct cci_pmu_model *get_cci_model(struct platform_device *pdev)
1456{
1457 const struct of_device_id *match = of_match_node(arm_cci_pmu_matches,
1458 pdev->dev.of_node);
1459 if (!match)
1460 return NULL;
Suzuki K. Poulose772742a2015-03-18 12:24:40 +00001461 if (match->data)
1462 return match->data;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001463
Suzuki K. Poulose772742a2015-03-18 12:24:40 +00001464 dev_warn(&pdev->dev, "DEPRECATED compatible property,"
1465 "requires secure access to CCI registers");
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001466 return probe_cci_model(pdev);
1467}
1468
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001469static bool is_duplicate_irq(int irq, int *irqs, int nr_irqs)
1470{
1471 int i;
1472
1473 for (i = 0; i < nr_irqs; i++)
1474 if (irq == irqs[i])
1475 return true;
1476
1477 return false;
1478}
1479
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001480static struct cci_pmu *cci_pmu_alloc(struct platform_device *pdev)
1481{
1482 struct cci_pmu *cci_pmu;
1483 const struct cci_pmu_model *model;
1484
1485 /*
1486 * All allocations are devm_* hence we don't have to free
1487 * them explicitly on an error, as it would end up in driver
1488 * detach.
1489 */
1490 model = get_cci_model(pdev);
1491 if (!model) {
1492 dev_warn(&pdev->dev, "CCI PMU version not supported\n");
1493 return ERR_PTR(-ENODEV);
1494 }
1495
1496 cci_pmu = devm_kzalloc(&pdev->dev, sizeof(*cci_pmu), GFP_KERNEL);
1497 if (!cci_pmu)
1498 return ERR_PTR(-ENOMEM);
1499
1500 cci_pmu->model = model;
1501 cci_pmu->irqs = devm_kcalloc(&pdev->dev, CCI_PMU_MAX_HW_CNTRS(model),
1502 sizeof(*cci_pmu->irqs), GFP_KERNEL);
1503 if (!cci_pmu->irqs)
1504 return ERR_PTR(-ENOMEM);
1505 cci_pmu->hw_events.events = devm_kcalloc(&pdev->dev,
1506 CCI_PMU_MAX_HW_CNTRS(model),
1507 sizeof(*cci_pmu->hw_events.events),
1508 GFP_KERNEL);
1509 if (!cci_pmu->hw_events.events)
1510 return ERR_PTR(-ENOMEM);
1511 cci_pmu->hw_events.used_mask = devm_kcalloc(&pdev->dev,
1512 BITS_TO_LONGS(CCI_PMU_MAX_HW_CNTRS(model)),
1513 sizeof(*cci_pmu->hw_events.used_mask),
1514 GFP_KERNEL);
1515 if (!cci_pmu->hw_events.used_mask)
1516 return ERR_PTR(-ENOMEM);
1517
1518 return cci_pmu;
1519}
1520
1521
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001522static int cci_pmu_probe(struct platform_device *pdev)
1523{
1524 struct resource *res;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001525 struct cci_pmu *cci_pmu;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001526 int i, ret, irq;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001527
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001528 cci_pmu = cci_pmu_alloc(pdev);
1529 if (IS_ERR(cci_pmu))
1530 return PTR_ERR(cci_pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001531
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001532 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001533 cci_pmu->base = devm_ioremap_resource(&pdev->dev, res);
1534 if (IS_ERR(cci_pmu->base))
Wei Yongjunfee4f2c2013-09-22 06:04:23 +01001535 return -ENOMEM;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001536
1537 /*
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001538 * CCI PMU has one overflow interrupt per counter; but some may be tied
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001539 * together to a common interrupt.
1540 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001541 cci_pmu->nr_irqs = 0;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001542 for (i = 0; i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model); i++) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001543 irq = platform_get_irq(pdev, i);
1544 if (irq < 0)
1545 break;
1546
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001547 if (is_duplicate_irq(irq, cci_pmu->irqs, cci_pmu->nr_irqs))
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001548 continue;
1549
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001550 cci_pmu->irqs[cci_pmu->nr_irqs++] = irq;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001551 }
1552
1553 /*
1554 * Ensure that the device tree has as many interrupts as the number
1555 * of counters.
1556 */
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001557 if (i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model)) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001558 dev_warn(&pdev->dev, "In-correct number of interrupts: %d, should be %d\n",
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001559 i, CCI_PMU_MAX_HW_CNTRS(cci_pmu->model));
Wei Yongjunfee4f2c2013-09-22 06:04:23 +01001560 return -EINVAL;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001561 }
1562
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001563 raw_spin_lock_init(&cci_pmu->hw_events.pmu_lock);
1564 mutex_init(&cci_pmu->reserve_mutex);
1565 atomic_set(&cci_pmu->active_events, 0);
1566 cpumask_set_cpu(smp_processor_id(), &cci_pmu->cpus);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001567
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001568 cci_pmu->cpu_nb = (struct notifier_block) {
1569 .notifier_call = cci_pmu_cpu_notifier,
1570 /*
1571 * to migrate uncore events, our notifier should be executed
1572 * before perf core's notifier.
1573 */
1574 .priority = CPU_PRI_PERF + 1,
1575 };
1576
1577 ret = register_cpu_notifier(&cci_pmu->cpu_nb);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001578 if (ret)
1579 return ret;
1580
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001581 ret = cci_pmu_init(cci_pmu, pdev);
1582 if (ret) {
1583 unregister_cpu_notifier(&cci_pmu->cpu_nb);
Wei Yongjunfee4f2c2013-09-22 06:04:23 +01001584 return ret;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001585 }
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001586
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001587 pr_info("ARM %s PMU driver probed", cci_pmu->model->name);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001588 return 0;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001589}
1590
1591static int cci_platform_probe(struct platform_device *pdev)
1592{
1593 if (!cci_probed())
1594 return -ENODEV;
1595
1596 return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
1597}
1598
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001599static struct platform_driver cci_pmu_driver = {
1600 .driver = {
1601 .name = DRIVER_NAME_PMU,
1602 .of_match_table = arm_cci_pmu_matches,
1603 },
1604 .probe = cci_pmu_probe,
1605};
1606
1607static struct platform_driver cci_platform_driver = {
1608 .driver = {
1609 .name = DRIVER_NAME,
1610 .of_match_table = arm_cci_matches,
1611 },
1612 .probe = cci_platform_probe,
1613};
1614
1615static int __init cci_platform_init(void)
1616{
1617 int ret;
1618
1619 ret = platform_driver_register(&cci_pmu_driver);
1620 if (ret)
1621 return ret;
1622
1623 return platform_driver_register(&cci_platform_driver);
1624}
1625
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001626#else /* !CONFIG_ARM_CCI_PMU */
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001627
1628static int __init cci_platform_init(void)
1629{
1630 return 0;
1631}
1632
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001633#endif /* CONFIG_ARM_CCI_PMU */
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +00001634
1635#ifdef CONFIG_ARM_CCI400_PORT_CTRL
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001636
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001637#define CCI_PORT_CTRL 0x0
1638#define CCI_CTRL_STATUS 0xc
1639
1640#define CCI_ENABLE_SNOOP_REQ 0x1
1641#define CCI_ENABLE_DVM_REQ 0x2
1642#define CCI_ENABLE_REQ (CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ)
1643
1644enum cci_ace_port_type {
1645 ACE_INVALID_PORT = 0x0,
1646 ACE_PORT,
1647 ACE_LITE_PORT,
1648};
1649
1650struct cci_ace_port {
1651 void __iomem *base;
1652 unsigned long phys;
1653 enum cci_ace_port_type type;
1654 struct device_node *dn;
1655};
1656
1657static struct cci_ace_port *ports;
1658static unsigned int nb_cci_ports;
1659
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001660struct cpu_port {
1661 u64 mpidr;
1662 u32 port;
1663};
Nicolas Pitre62158f82013-05-21 23:34:41 -04001664
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001665/*
1666 * Use the port MSB as valid flag, shift can be made dynamic
1667 * by computing number of bits required for port indexes.
1668 * Code disabling CCI cpu ports runs with D-cache invalidated
1669 * and SCTLR bit clear so data accesses must be kept to a minimum
1670 * to improve performance; for now shift is left static to
1671 * avoid one more data access while disabling the CCI port.
1672 */
1673#define PORT_VALID_SHIFT 31
1674#define PORT_VALID (0x1 << PORT_VALID_SHIFT)
1675
1676static inline void init_cpu_port(struct cpu_port *port, u32 index, u64 mpidr)
1677{
1678 port->port = PORT_VALID | index;
1679 port->mpidr = mpidr;
1680}
1681
1682static inline bool cpu_port_is_valid(struct cpu_port *port)
1683{
1684 return !!(port->port & PORT_VALID);
1685}
1686
1687static inline bool cpu_port_match(struct cpu_port *port, u64 mpidr)
1688{
1689 return port->mpidr == (mpidr & MPIDR_HWID_BITMASK);
1690}
1691
1692static struct cpu_port cpu_port[NR_CPUS];
1693
1694/**
1695 * __cci_ace_get_port - Function to retrieve the port index connected to
1696 * a cpu or device.
1697 *
1698 * @dn: device node of the device to look-up
1699 * @type: port type
1700 *
1701 * Return value:
1702 * - CCI port index if success
1703 * - -ENODEV if failure
1704 */
1705static int __cci_ace_get_port(struct device_node *dn, int type)
1706{
1707 int i;
1708 bool ace_match;
1709 struct device_node *cci_portn;
1710
1711 cci_portn = of_parse_phandle(dn, "cci-control-port", 0);
1712 for (i = 0; i < nb_cci_ports; i++) {
1713 ace_match = ports[i].type == type;
1714 if (ace_match && cci_portn == ports[i].dn)
1715 return i;
1716 }
1717 return -ENODEV;
1718}
1719
1720int cci_ace_get_port(struct device_node *dn)
1721{
1722 return __cci_ace_get_port(dn, ACE_LITE_PORT);
1723}
1724EXPORT_SYMBOL_GPL(cci_ace_get_port);
1725
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001726static void cci_ace_init_ports(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001727{
Sudeep KarkadaNagesha78b4d6e2013-06-17 14:51:48 +01001728 int port, cpu;
1729 struct device_node *cpun;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001730
1731 /*
1732 * Port index look-up speeds up the function disabling ports by CPU,
1733 * since the logical to port index mapping is done once and does
1734 * not change after system boot.
1735 * The stashed index array is initialized for all possible CPUs
1736 * at probe time.
1737 */
Sudeep KarkadaNagesha78b4d6e2013-06-17 14:51:48 +01001738 for_each_possible_cpu(cpu) {
1739 /* too early to use cpu->of_node */
1740 cpun = of_get_cpu_node(cpu, NULL);
1741
1742 if (WARN(!cpun, "Missing cpu device node\n"))
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001743 continue;
1744
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001745 port = __cci_ace_get_port(cpun, ACE_PORT);
1746 if (port < 0)
1747 continue;
1748
1749 init_cpu_port(&cpu_port[cpu], port, cpu_logical_map(cpu));
1750 }
1751
1752 for_each_possible_cpu(cpu) {
1753 WARN(!cpu_port_is_valid(&cpu_port[cpu]),
1754 "CPU %u does not have an associated CCI port\n",
1755 cpu);
1756 }
1757}
1758/*
1759 * Functions to enable/disable a CCI interconnect slave port
1760 *
1761 * They are called by low-level power management code to disable slave
1762 * interfaces snoops and DVM broadcast.
1763 * Since they may execute with cache data allocation disabled and
1764 * after the caches have been cleaned and invalidated the functions provide
1765 * no explicit locking since they may run with D-cache disabled, so normal
1766 * cacheable kernel locks based on ldrex/strex may not work.
1767 * Locking has to be provided by BSP implementations to ensure proper
1768 * operations.
1769 */
1770
1771/**
1772 * cci_port_control() - function to control a CCI port
1773 *
1774 * @port: index of the port to setup
1775 * @enable: if true enables the port, if false disables it
1776 */
1777static void notrace cci_port_control(unsigned int port, bool enable)
1778{
1779 void __iomem *base = ports[port].base;
1780
1781 writel_relaxed(enable ? CCI_ENABLE_REQ : 0, base + CCI_PORT_CTRL);
1782 /*
1783 * This function is called from power down procedures
1784 * and must not execute any instruction that might
1785 * cause the processor to be put in a quiescent state
1786 * (eg wfi). Hence, cpu_relax() can not be added to this
1787 * read loop to optimize power, since it might hide possibly
1788 * disruptive operations.
1789 */
1790 while (readl_relaxed(cci_ctrl_base + CCI_CTRL_STATUS) & 0x1)
1791 ;
1792}
1793
1794/**
1795 * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
1796 * reference
1797 *
1798 * @mpidr: mpidr of the CPU whose CCI port should be disabled
1799 *
1800 * Disabling a CCI port for a CPU implies disabling the CCI port
1801 * controlling that CPU cluster. Code disabling CPU CCI ports
1802 * must make sure that the CPU running the code is the last active CPU
1803 * in the cluster ie all other CPUs are quiescent in a low power state.
1804 *
1805 * Return:
1806 * 0 on success
1807 * -ENODEV on port look-up failure
1808 */
1809int notrace cci_disable_port_by_cpu(u64 mpidr)
1810{
1811 int cpu;
1812 bool is_valid;
1813 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
1814 is_valid = cpu_port_is_valid(&cpu_port[cpu]);
1815 if (is_valid && cpu_port_match(&cpu_port[cpu], mpidr)) {
1816 cci_port_control(cpu_port[cpu].port, false);
1817 return 0;
1818 }
1819 }
1820 return -ENODEV;
1821}
1822EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu);
1823
1824/**
Nicolas Pitre62158f82013-05-21 23:34:41 -04001825 * cci_enable_port_for_self() - enable a CCI port for calling CPU
1826 *
1827 * Enabling a CCI port for the calling CPU implies enabling the CCI
1828 * port controlling that CPU's cluster. Caller must make sure that the
1829 * CPU running the code is the first active CPU in the cluster and all
1830 * other CPUs are quiescent in a low power state or waiting for this CPU
1831 * to complete the CCI initialization.
1832 *
1833 * Because this is called when the MMU is still off and with no stack,
1834 * the code must be position independent and ideally rely on callee
1835 * clobbered registers only. To achieve this we must code this function
1836 * entirely in assembler.
1837 *
1838 * On success this returns with the proper CCI port enabled. In case of
1839 * any failure this never returns as the inability to enable the CCI is
1840 * fatal and there is no possible recovery at this stage.
1841 */
1842asmlinkage void __naked cci_enable_port_for_self(void)
1843{
1844 asm volatile ("\n"
Arnd Bergmannf4902492013-06-03 15:15:36 +02001845" .arch armv7-a\n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04001846" mrc p15, 0, r0, c0, c0, 5 @ get MPIDR value \n"
1847" and r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n"
1848" adr r1, 5f \n"
1849" ldr r2, [r1] \n"
1850" add r1, r1, r2 @ &cpu_port \n"
1851" add ip, r1, %[sizeof_cpu_port] \n"
1852
1853 /* Loop over the cpu_port array looking for a matching MPIDR */
1854"1: ldr r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
1855" cmp r2, r0 @ compare MPIDR \n"
1856" bne 2f \n"
1857
1858 /* Found a match, now test port validity */
1859" ldr r3, [r1, %[offsetof_cpu_port_port]] \n"
1860" tst r3, #"__stringify(PORT_VALID)" \n"
1861" bne 3f \n"
1862
1863 /* no match, loop with the next cpu_port entry */
1864"2: add r1, r1, %[sizeof_struct_cpu_port] \n"
1865" cmp r1, ip @ done? \n"
1866" blo 1b \n"
1867
1868 /* CCI port not found -- cheaply try to stall this CPU */
1869"cci_port_not_found: \n"
1870" wfi \n"
1871" wfe \n"
1872" b cci_port_not_found \n"
1873
1874 /* Use matched port index to look up the corresponding ports entry */
1875"3: bic r3, r3, #"__stringify(PORT_VALID)" \n"
1876" adr r0, 6f \n"
1877" ldmia r0, {r1, r2} \n"
1878" sub r1, r1, r0 @ virt - phys \n"
1879" ldr r0, [r0, r2] @ *(&ports) \n"
1880" mov r2, %[sizeof_struct_ace_port] \n"
1881" mla r0, r2, r3, r0 @ &ports[index] \n"
1882" sub r0, r0, r1 @ virt_to_phys() \n"
1883
1884 /* Enable the CCI port */
1885" ldr r0, [r0, %[offsetof_port_phys]] \n"
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07001886" mov r3, %[cci_enable_req]\n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04001887" str r3, [r0, #"__stringify(CCI_PORT_CTRL)"] \n"
1888
1889 /* poll the status reg for completion */
1890" adr r1, 7f \n"
1891" ldr r0, [r1] \n"
1892" ldr r0, [r0, r1] @ cci_ctrl_base \n"
1893"4: ldr r1, [r0, #"__stringify(CCI_CTRL_STATUS)"] \n"
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07001894" tst r1, %[cci_control_status_bits] \n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04001895" bne 4b \n"
1896
1897" mov r0, #0 \n"
1898" bx lr \n"
1899
1900" .align 2 \n"
1901"5: .word cpu_port - . \n"
1902"6: .word . \n"
1903" .word ports - 6b \n"
1904"7: .word cci_ctrl_phys - . \n"
1905 : :
1906 [sizeof_cpu_port] "i" (sizeof(cpu_port)),
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07001907 [cci_enable_req] "i" cpu_to_le32(CCI_ENABLE_REQ),
1908 [cci_control_status_bits] "i" cpu_to_le32(1),
Nicolas Pitre62158f82013-05-21 23:34:41 -04001909#ifndef __ARMEB__
1910 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
1911#else
1912 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
1913#endif
1914 [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
1915 [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
1916 [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
1917 [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
1918
1919 unreachable();
1920}
1921
1922/**
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001923 * __cci_control_port_by_device() - function to control a CCI port by device
1924 * reference
1925 *
1926 * @dn: device node pointer of the device whose CCI port should be
1927 * controlled
1928 * @enable: if true enables the port, if false disables it
1929 *
1930 * Return:
1931 * 0 on success
1932 * -ENODEV on port look-up failure
1933 */
1934int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
1935{
1936 int port;
1937
1938 if (!dn)
1939 return -ENODEV;
1940
1941 port = __cci_ace_get_port(dn, ACE_LITE_PORT);
1942 if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n",
1943 dn->full_name))
1944 return -ENODEV;
1945 cci_port_control(port, enable);
1946 return 0;
1947}
1948EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
1949
1950/**
1951 * __cci_control_port_by_index() - function to control a CCI port by port index
1952 *
1953 * @port: port index previously retrieved with cci_ace_get_port()
1954 * @enable: if true enables the port, if false disables it
1955 *
1956 * Return:
1957 * 0 on success
1958 * -ENODEV on port index out of range
1959 * -EPERM if operation carried out on an ACE PORT
1960 */
1961int notrace __cci_control_port_by_index(u32 port, bool enable)
1962{
1963 if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
1964 return -ENODEV;
1965 /*
1966 * CCI control for ports connected to CPUS is extremely fragile
1967 * and must be made to go through a specific and controlled
1968 * interface (ie cci_disable_port_by_cpu(); control by general purpose
1969 * indexing is therefore disabled for ACE ports.
1970 */
1971 if (ports[port].type == ACE_PORT)
1972 return -EPERM;
1973
1974 cci_port_control(port, enable);
1975 return 0;
1976}
1977EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
1978
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001979static const struct of_device_id arm_cci_ctrl_if_matches[] = {
1980 {.compatible = "arm,cci-400-ctrl-if", },
1981 {},
1982};
1983
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001984static int cci_probe_ports(struct device_node *np)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001985{
1986 struct cci_nb_ports const *cci_config;
1987 int ret, i, nb_ace = 0, nb_ace_lite = 0;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001988 struct device_node *cp;
Nicolas Pitre62158f82013-05-21 23:34:41 -04001989 struct resource res;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001990 const char *match_str;
1991 bool is_ace;
1992
Abhilash Kesavan896ddd62015-01-10 08:41:35 +05301993
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001994 cci_config = of_match_node(arm_cci_matches, np)->data;
1995 if (!cci_config)
1996 return -ENODEV;
1997
1998 nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
1999
Lorenzo Pieralisi7c762032014-01-27 10:50:37 +00002000 ports = kcalloc(nb_cci_ports, sizeof(*ports), GFP_KERNEL);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002001 if (!ports)
2002 return -ENOMEM;
2003
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002004 for_each_child_of_node(np, cp) {
2005 if (!of_match_node(arm_cci_ctrl_if_matches, cp))
2006 continue;
2007
2008 i = nb_ace + nb_ace_lite;
2009
2010 if (i >= nb_cci_ports)
2011 break;
2012
2013 if (of_property_read_string(cp, "interface-type",
2014 &match_str)) {
2015 WARN(1, "node %s missing interface-type property\n",
2016 cp->full_name);
2017 continue;
2018 }
2019 is_ace = strcmp(match_str, "ace") == 0;
2020 if (!is_ace && strcmp(match_str, "ace-lite")) {
2021 WARN(1, "node %s containing invalid interface-type property, skipping it\n",
2022 cp->full_name);
2023 continue;
2024 }
2025
Nicolas Pitre62158f82013-05-21 23:34:41 -04002026 ret = of_address_to_resource(cp, 0, &res);
2027 if (!ret) {
2028 ports[i].base = ioremap(res.start, resource_size(&res));
2029 ports[i].phys = res.start;
2030 }
2031 if (ret || !ports[i].base) {
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002032 WARN(1, "unable to ioremap CCI port %d\n", i);
2033 continue;
2034 }
2035
2036 if (is_ace) {
2037 if (WARN_ON(nb_ace >= cci_config->nb_ace))
2038 continue;
2039 ports[i].type = ACE_PORT;
2040 ++nb_ace;
2041 } else {
2042 if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
2043 continue;
2044 ports[i].type = ACE_LITE_PORT;
2045 ++nb_ace_lite;
2046 }
2047 ports[i].dn = cp;
2048 }
2049
2050 /* initialize a stashed array of ACE ports to speed-up look-up */
2051 cci_ace_init_ports();
2052
2053 /*
2054 * Multi-cluster systems may need this data when non-coherent, during
2055 * cluster power-up/power-down. Make sure it reaches main memory.
2056 */
2057 sync_cache_w(&cci_ctrl_base);
Nicolas Pitre62158f82013-05-21 23:34:41 -04002058 sync_cache_w(&cci_ctrl_phys);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002059 sync_cache_w(&ports);
2060 sync_cache_w(&cpu_port);
2061 __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
2062 pr_info("ARM CCI driver probed\n");
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002063
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002064 return 0;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002065}
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +00002066#else /* !CONFIG_ARM_CCI400_PORT_CTRL */
2067static inline int cci_probe_ports(struct device_node *np)
2068{
2069 return 0;
2070}
2071#endif /* CONFIG_ARM_CCI400_PORT_CTRL */
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002072
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002073static int cci_probe(void)
2074{
2075 int ret;
2076 struct device_node *np;
2077 struct resource res;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002078
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00002079 np = of_find_matching_node(NULL, arm_cci_matches);
2080 if(!np || !of_device_is_available(np))
2081 return -ENODEV;
2082
2083 ret = of_address_to_resource(np, 0, &res);
2084 if (!ret) {
2085 cci_ctrl_base = ioremap(res.start, resource_size(&res));
2086 cci_ctrl_phys = res.start;
2087 }
2088 if (ret || !cci_ctrl_base) {
2089 WARN(1, "unable to ioremap CCI ctrl\n");
2090 return -ENXIO;
2091 }
2092
2093 return cci_probe_ports(np);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002094}
2095
2096static int cci_init_status = -EAGAIN;
2097static DEFINE_MUTEX(cci_probing);
2098
Punit Agrawalb91c8f22013-08-22 14:41:51 +01002099static int cci_init(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002100{
2101 if (cci_init_status != -EAGAIN)
2102 return cci_init_status;
2103
2104 mutex_lock(&cci_probing);
2105 if (cci_init_status == -EAGAIN)
2106 cci_init_status = cci_probe();
2107 mutex_unlock(&cci_probing);
2108 return cci_init_status;
2109}
2110
2111/*
2112 * To sort out early init calls ordering a helper function is provided to
2113 * check if the CCI driver has beed initialized. Function check if the driver
2114 * has been initialized, if not it calls the init function that probes
2115 * the driver and updates the return value.
2116 */
Punit Agrawalb91c8f22013-08-22 14:41:51 +01002117bool cci_probed(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002118{
2119 return cci_init() == 0;
2120}
2121EXPORT_SYMBOL_GPL(cci_probed);
2122
2123early_initcall(cci_init);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01002124core_initcall(cci_platform_init);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01002125MODULE_LICENSE("GPL");
2126MODULE_DESCRIPTION("ARM CCI support");