blob: df1c2c61d304004c0fc6c4f7de0d04cd29c11cd3 [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. Poulosef6b9e832015-03-18 12:24:38 +000055 {},
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010056};
57
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +010058#ifdef CONFIG_ARM_CCI_PMU
Punit Agrawalb91c8f22013-08-22 14:41:51 +010059
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +010060#define DRIVER_NAME "ARM-CCI"
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000061#define DRIVER_NAME_PMU DRIVER_NAME " PMU"
62
Punit Agrawalb91c8f22013-08-22 14:41:51 +010063#define CCI_PMCR 0x0100
64#define CCI_PID2 0x0fe8
65
66#define CCI_PMCR_CEN 0x00000001
67#define CCI_PMCR_NCNT_MASK 0x0000f800
68#define CCI_PMCR_NCNT_SHIFT 11
69
70#define CCI_PID2_REV_MASK 0xf0
71#define CCI_PID2_REV_SHIFT 4
72
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000073#define CCI_PMU_EVT_SEL 0x000
74#define CCI_PMU_CNTR 0x004
75#define CCI_PMU_CNTR_CTRL 0x008
76#define CCI_PMU_OVRFLW 0x00c
77
78#define CCI_PMU_OVRFLW_FLAG 1
79
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +010080#define CCI_PMU_CNTR_SIZE(model) ((model)->cntr_size)
81#define CCI_PMU_CNTR_BASE(model, idx) ((idx) * CCI_PMU_CNTR_SIZE(model))
82#define CCI_PMU_CNTR_MASK ((1ULL << 32) -1)
83#define CCI_PMU_CNTR_LAST(cci_pmu) (cci_pmu->num_cntrs - 1)
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000084
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +010085#define CCI_PMU_MAX_HW_CNTRS(model) \
86 ((model)->num_hw_cntrs + (model)->fixed_hw_cntrs)
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000087
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +000088/* Types of interfaces that can generate events */
89enum {
90 CCI_IF_SLAVE,
91 CCI_IF_MASTER,
92 CCI_IF_MAX,
93};
94
95struct event_range {
96 u32 min;
97 u32 max;
98};
99
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000100struct cci_pmu_hw_events {
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100101 struct perf_event **events;
102 unsigned long *used_mask;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000103 raw_spinlock_t pmu_lock;
104};
105
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100106struct cci_pmu;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100107/*
108 * struct cci_pmu_model:
109 * @fixed_hw_cntrs - Number of fixed event counters
110 * @num_hw_cntrs - Maximum number of programmable event counters
111 * @cntr_size - Size of an event counter mapping
112 */
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000113struct cci_pmu_model {
114 char *name;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100115 u32 fixed_hw_cntrs;
116 u32 num_hw_cntrs;
117 u32 cntr_size;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000118 struct event_range event_ranges[CCI_IF_MAX];
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100119 int (*validate_hw_event)(struct cci_pmu *, unsigned long);
120 int (*get_event_idx)(struct cci_pmu *, struct cci_pmu_hw_events *, unsigned long);
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000121};
122
123static struct cci_pmu_model cci_pmu_models[];
124
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000125struct cci_pmu {
126 void __iomem *base;
127 struct pmu pmu;
128 int nr_irqs;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100129 int *irqs;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000130 unsigned long active_irqs;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000131 const struct cci_pmu_model *model;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000132 struct cci_pmu_hw_events hw_events;
133 struct platform_device *plat_device;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100134 int num_cntrs;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000135 atomic_t active_events;
136 struct mutex reserve_mutex;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100137 struct notifier_block cpu_nb;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000138 cpumask_t cpus;
139};
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000140
141#define to_cci_pmu(c) (container_of(c, struct cci_pmu, pmu))
142
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100143enum cci_models {
144#ifdef CONFIG_ARM_CCI400_PMU
145 CCI400_R0,
146 CCI400_R1,
147#endif
148 CCI_MODEL_MAX
149};
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100150
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100151/* CCI400 PMU Specific definitions */
152
153#ifdef CONFIG_ARM_CCI400_PMU
154
155/* Port ids */
156#define CCI400_PORT_S0 0
157#define CCI400_PORT_S1 1
158#define CCI400_PORT_S2 2
159#define CCI400_PORT_S3 3
160#define CCI400_PORT_S4 4
161#define CCI400_PORT_M0 5
162#define CCI400_PORT_M1 6
163#define CCI400_PORT_M2 7
164
165#define CCI400_R1_PX 5
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100166
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100167/*
168 * Instead of an event id to monitor CCI cycles, a dedicated counter is
169 * provided. Use 0xff to represent CCI cycles and hope that no future revisions
170 * make use of this event in hardware.
171 */
172enum cci400_perf_events {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100173 CCI400_PMU_CYCLES = 0xff
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100174};
175
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100176#define CCI400_PMU_CYCLE_CNTR_IDX 0
177#define CCI400_PMU_CNTR0_IDX 1
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100178
179/*
180 * CCI PMU event id is an 8-bit value made of two parts - bits 7:5 for one of 8
181 * ports and bits 4:0 are event codes. There are different event codes
182 * associated with each port type.
183 *
184 * Additionally, the range of events associated with the port types changed
185 * between Rev0 and Rev1.
186 *
187 * The constants below define the range of valid codes for each port type for
188 * the different revisions and are used to validate the event to be monitored.
189 */
190
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100191#define CCI400_PMU_EVENT_MASK 0xffUL
192#define CCI400_PMU_EVENT_SOURCE_SHIFT 5
193#define CCI400_PMU_EVENT_SOURCE_MASK 0x7
194#define CCI400_PMU_EVENT_CODE_SHIFT 0
195#define CCI400_PMU_EVENT_CODE_MASK 0x1f
196#define CCI400_PMU_EVENT_SOURCE(event) \
197 ((event >> CCI400_PMU_EVENT_SOURCE_SHIFT) & \
198 CCI400_PMU_EVENT_SOURCE_MASK)
199#define CCI400_PMU_EVENT_CODE(event) \
200 ((event >> CCI400_PMU_EVENT_CODE_SHIFT) & CCI400_PMU_EVENT_CODE_MASK)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100201
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100202#define CCI400_R0_SLAVE_PORT_MIN_EV 0x00
203#define CCI400_R0_SLAVE_PORT_MAX_EV 0x13
204#define CCI400_R0_MASTER_PORT_MIN_EV 0x14
205#define CCI400_R0_MASTER_PORT_MAX_EV 0x1a
206
207#define CCI400_R1_SLAVE_PORT_MIN_EV 0x00
208#define CCI400_R1_SLAVE_PORT_MAX_EV 0x14
209#define CCI400_R1_MASTER_PORT_MIN_EV 0x00
210#define CCI400_R1_MASTER_PORT_MAX_EV 0x11
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100211
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100212static int cci400_get_event_idx(struct cci_pmu *cci_pmu,
213 struct cci_pmu_hw_events *hw,
214 unsigned long cci_event)
215{
216 int idx;
217
218 /* cycles event idx is fixed */
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100219 if (cci_event == CCI400_PMU_CYCLES) {
220 if (test_and_set_bit(CCI400_PMU_CYCLE_CNTR_IDX, hw->used_mask))
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100221 return -EAGAIN;
222
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100223 return CCI400_PMU_CYCLE_CNTR_IDX;
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100224 }
225
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100226 for (idx = CCI400_PMU_CNTR0_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); ++idx)
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100227 if (!test_and_set_bit(idx, hw->used_mask))
228 return idx;
229
230 /* No counters available */
231 return -EAGAIN;
232}
233
234static int cci400_validate_hw_event(struct cci_pmu *cci_pmu, unsigned long hw_event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100235{
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100236 u8 ev_source = CCI400_PMU_EVENT_SOURCE(hw_event);
237 u8 ev_code = CCI400_PMU_EVENT_CODE(hw_event);
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000238 int if_type;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100239
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100240 if (hw_event & ~CCI400_PMU_EVENT_MASK)
Suzuki K. Poulose874c5712015-03-18 12:24:42 +0000241 return -ENOENT;
242
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100243 if (hw_event == CCI400_PMU_CYCLES)
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100244 return hw_event;
245
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100246 switch (ev_source) {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100247 case CCI400_PORT_S0:
248 case CCI400_PORT_S1:
249 case CCI400_PORT_S2:
250 case CCI400_PORT_S3:
251 case CCI400_PORT_S4:
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100252 /* Slave Interface */
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000253 if_type = CCI_IF_SLAVE;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100254 break;
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100255 case CCI400_PORT_M0:
256 case CCI400_PORT_M1:
257 case CCI400_PORT_M2:
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100258 /* Master Interface */
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000259 if_type = CCI_IF_MASTER;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100260 break;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000261 default:
262 return -ENOENT;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100263 }
264
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100265 if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
266 ev_code <= cci_pmu->model->event_ranges[if_type].max)
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000267 return hw_event;
268
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100269 return -ENOENT;
270}
271
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100272static int probe_cci400_revision(void)
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000273{
274 int rev;
275 rev = readl_relaxed(cci_ctrl_base + CCI_PID2) & CCI_PID2_REV_MASK;
276 rev >>= CCI_PID2_REV_SHIFT;
277
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100278 if (rev < CCI400_R1_PX)
279 return CCI400_R0;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000280 else
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100281 return CCI400_R1;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000282}
283
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000284static const struct cci_pmu_model *probe_cci_model(struct platform_device *pdev)
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000285{
Suzuki K. Poulose772742a2015-03-18 12:24:40 +0000286 if (platform_has_secure_cci_access())
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100287 return &cci_pmu_models[probe_cci400_revision()];
Suzuki K. Poulose772742a2015-03-18 12:24:40 +0000288 return NULL;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000289}
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100290#else /* !CONFIG_ARM_CCI400_PMU */
291static inline struct cci_pmu_model *probe_cci_model(struct platform_device *pdev)
292{
293 return NULL;
294}
295#endif /* CONFIG_ARM_CCI400_PMU */
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000296
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100297static int pmu_is_valid_counter(struct cci_pmu *cci_pmu, int idx)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100298{
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100299 return 0 <= idx && idx <= CCI_PMU_CNTR_LAST(cci_pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100300}
301
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100302static u32 pmu_read_register(struct cci_pmu *cci_pmu, int idx, unsigned int offset)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100303{
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100304 return readl_relaxed(cci_pmu->base +
305 CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100306}
307
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100308static void pmu_write_register(struct cci_pmu *cci_pmu, u32 value,
309 int idx, unsigned int offset)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100310{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100311 return writel_relaxed(value, cci_pmu->base +
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100312 CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100313}
314
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100315static void pmu_disable_counter(struct cci_pmu *cci_pmu, int idx)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100316{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100317 pmu_write_register(cci_pmu, 0, idx, CCI_PMU_CNTR_CTRL);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100318}
319
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100320static void pmu_enable_counter(struct cci_pmu *cci_pmu, int idx)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100321{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100322 pmu_write_register(cci_pmu, 1, idx, CCI_PMU_CNTR_CTRL);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100323}
324
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100325static void pmu_set_event(struct cci_pmu *cci_pmu, int idx, unsigned long event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100326{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100327 pmu_write_register(cci_pmu, event, idx, CCI_PMU_EVT_SEL);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100328}
329
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100330/*
331 * Returns the number of programmable counters actually implemented
332 * by the cci
333 */
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100334static u32 pmu_get_max_counters(void)
335{
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100336 return (readl_relaxed(cci_ctrl_base + CCI_PMCR) &
337 CCI_PMCR_NCNT_MASK) >> CCI_PMCR_NCNT_SHIFT;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100338}
339
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100340static int pmu_get_event_idx(struct cci_pmu_hw_events *hw, struct perf_event *event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100341{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100342 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100343 unsigned long cci_event = event->hw.config_base;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100344 int idx;
345
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100346 if (cci_pmu->model->get_event_idx)
347 return cci_pmu->model->get_event_idx(cci_pmu, hw, cci_event);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100348
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100349 /* Generic code to find an unused idx from the mask */
350 for(idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100351 if (!test_and_set_bit(idx, hw->used_mask))
352 return idx;
353
354 /* No counters available */
355 return -EAGAIN;
356}
357
358static int pmu_map_event(struct perf_event *event)
359{
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100360 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100361
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100362 if (event->attr.type < PERF_TYPE_MAX ||
363 !cci_pmu->model->validate_hw_event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100364 return -ENOENT;
365
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100366 return cci_pmu->model->validate_hw_event(cci_pmu, event->attr.config);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100367}
368
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100369static int pmu_request_irq(struct cci_pmu *cci_pmu, irq_handler_t handler)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100370{
371 int i;
372 struct platform_device *pmu_device = cci_pmu->plat_device;
373
374 if (unlikely(!pmu_device))
375 return -ENODEV;
376
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100377 if (cci_pmu->nr_irqs < 1) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100378 dev_err(&pmu_device->dev, "no irqs for CCI PMUs defined\n");
379 return -ENODEV;
380 }
381
382 /*
383 * Register all available CCI PMU interrupts. In the interrupt handler
384 * we iterate over the counters checking for interrupt source (the
385 * overflowing counter) and clear it.
386 *
387 * This should allow handling of non-unique interrupt for the counters.
388 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100389 for (i = 0; i < cci_pmu->nr_irqs; i++) {
390 int err = request_irq(cci_pmu->irqs[i], handler, IRQF_SHARED,
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100391 "arm-cci-pmu", cci_pmu);
392 if (err) {
393 dev_err(&pmu_device->dev, "unable to request IRQ%d for ARM CCI PMU counters\n",
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100394 cci_pmu->irqs[i]);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100395 return err;
396 }
397
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100398 set_bit(i, &cci_pmu->active_irqs);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100399 }
400
401 return 0;
402}
403
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100404static void pmu_free_irq(struct cci_pmu *cci_pmu)
405{
406 int i;
407
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100408 for (i = 0; i < cci_pmu->nr_irqs; i++) {
409 if (!test_and_clear_bit(i, &cci_pmu->active_irqs))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100410 continue;
411
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100412 free_irq(cci_pmu->irqs[i], cci_pmu);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100413 }
414}
415
416static u32 pmu_read_counter(struct perf_event *event)
417{
418 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
419 struct hw_perf_event *hw_counter = &event->hw;
420 int idx = hw_counter->idx;
421 u32 value;
422
423 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
424 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
425 return 0;
426 }
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100427 value = pmu_read_register(cci_pmu, idx, CCI_PMU_CNTR);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100428
429 return value;
430}
431
432static void pmu_write_counter(struct perf_event *event, u32 value)
433{
434 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
435 struct hw_perf_event *hw_counter = &event->hw;
436 int idx = hw_counter->idx;
437
438 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx)))
439 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
440 else
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100441 pmu_write_register(cci_pmu, value, idx, CCI_PMU_CNTR);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100442}
443
444static u64 pmu_event_update(struct perf_event *event)
445{
446 struct hw_perf_event *hwc = &event->hw;
447 u64 delta, prev_raw_count, new_raw_count;
448
449 do {
450 prev_raw_count = local64_read(&hwc->prev_count);
451 new_raw_count = pmu_read_counter(event);
452 } while (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
453 new_raw_count) != prev_raw_count);
454
455 delta = (new_raw_count - prev_raw_count) & CCI_PMU_CNTR_MASK;
456
457 local64_add(delta, &event->count);
458
459 return new_raw_count;
460}
461
462static void pmu_read(struct perf_event *event)
463{
464 pmu_event_update(event);
465}
466
467void pmu_event_set_period(struct perf_event *event)
468{
469 struct hw_perf_event *hwc = &event->hw;
470 /*
471 * The CCI PMU counters have a period of 2^32. To account for the
472 * possiblity of extreme interrupt latency we program for a period of
473 * half that. Hopefully we can handle the interrupt before another 2^31
474 * events occur and the counter overtakes its previous value.
475 */
476 u64 val = 1ULL << 31;
477 local64_set(&hwc->prev_count, val);
478 pmu_write_counter(event, val);
479}
480
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100481static irqreturn_t pmu_handle_irq(int irq_num, void *dev)
482{
483 unsigned long flags;
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100484 struct cci_pmu *cci_pmu = dev;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100485 struct cci_pmu_hw_events *events = &cci_pmu->hw_events;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100486 int idx, handled = IRQ_NONE;
487
488 raw_spin_lock_irqsave(&events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100489 /*
490 * Iterate over counters and update the corresponding perf events.
491 * This should work regardless of whether we have per-counter overflow
492 * interrupt or a combined overflow interrupt.
493 */
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100494 for (idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100495 struct perf_event *event = events->events[idx];
496 struct hw_perf_event *hw_counter;
497
498 if (!event)
499 continue;
500
501 hw_counter = &event->hw;
502
503 /* Did this counter overflow? */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100504 if (!(pmu_read_register(cci_pmu, idx, CCI_PMU_OVRFLW) &
Himangi Saraogifc5130d2014-07-30 11:37:35 +0100505 CCI_PMU_OVRFLW_FLAG))
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100506 continue;
507
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100508 pmu_write_register(cci_pmu, CCI_PMU_OVRFLW_FLAG, idx,
509 CCI_PMU_OVRFLW);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100510
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100511 pmu_event_update(event);
512 pmu_event_set_period(event);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100513 handled = IRQ_HANDLED;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100514 }
515 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
516
517 return IRQ_RETVAL(handled);
518}
519
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100520static int cci_pmu_get_hw(struct cci_pmu *cci_pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100521{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100522 int ret = pmu_request_irq(cci_pmu, pmu_handle_irq);
523 if (ret) {
524 pmu_free_irq(cci_pmu);
525 return ret;
526 }
527 return 0;
528}
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100529
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100530static void cci_pmu_put_hw(struct cci_pmu *cci_pmu)
531{
532 pmu_free_irq(cci_pmu);
533}
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100534
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100535static void hw_perf_event_destroy(struct perf_event *event)
536{
537 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
538 atomic_t *active_events = &cci_pmu->active_events;
539 struct mutex *reserve_mutex = &cci_pmu->reserve_mutex;
540
541 if (atomic_dec_and_mutex_lock(active_events, reserve_mutex)) {
542 cci_pmu_put_hw(cci_pmu);
543 mutex_unlock(reserve_mutex);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100544 }
545}
546
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100547static void cci_pmu_enable(struct pmu *pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100548{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100549 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
550 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100551 int enabled = bitmap_weight(hw_events->used_mask, cci_pmu->num_cntrs);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100552 unsigned long flags;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100553 u32 val;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100554
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100555 if (!enabled)
556 return;
557
558 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100559
560 /* Enable all the PMU counters. */
561 val = readl_relaxed(cci_ctrl_base + CCI_PMCR) | CCI_PMCR_CEN;
562 writel(val, cci_ctrl_base + CCI_PMCR);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100563 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100564
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100565}
566
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100567static void cci_pmu_disable(struct pmu *pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100568{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100569 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
570 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100571 unsigned long flags;
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100572 u32 val;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100573
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100574 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100575
576 /* Disable all the PMU counters. */
577 val = readl_relaxed(cci_ctrl_base + CCI_PMCR) & ~CCI_PMCR_CEN;
578 writel(val, cci_ctrl_base + CCI_PMCR);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100579 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100580}
581
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100582/*
583 * Check if the idx represents a non-programmable counter.
584 * All the fixed event counters are mapped before the programmable
585 * counters.
586 */
587static bool pmu_fixed_hw_idx(struct cci_pmu *cci_pmu, int idx)
588{
589 return (idx >= 0) && (idx < cci_pmu->model->fixed_hw_cntrs);
590}
591
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100592static void cci_pmu_start(struct perf_event *event, int pmu_flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100593{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100594 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
595 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
596 struct hw_perf_event *hwc = &event->hw;
597 int idx = hwc->idx;
598 unsigned long flags;
599
600 /*
601 * To handle interrupt latency, we always reprogram the period
602 * regardlesss of PERF_EF_RELOAD.
603 */
604 if (pmu_flags & PERF_EF_RELOAD)
605 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
606
607 hwc->state = 0;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100608
609 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
610 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100611 return;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100612 }
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100613
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100614 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
615
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100616 /* Configure the counter unless you are counting a fixed event */
617 if (!pmu_fixed_hw_idx(cci_pmu, idx))
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100618 pmu_set_event(cci_pmu, idx, hwc->config_base);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100619
620 pmu_event_set_period(event);
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100621 pmu_enable_counter(cci_pmu, idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100622
623 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100624}
625
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100626static void cci_pmu_stop(struct perf_event *event, int pmu_flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100627{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100628 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
629 struct hw_perf_event *hwc = &event->hw;
630 int idx = hwc->idx;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100631
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100632 if (hwc->state & PERF_HES_STOPPED)
633 return;
634
635 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100636 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100637 return;
638 }
639
640 /*
641 * We always reprogram the counter, so ignore PERF_EF_UPDATE. See
642 * cci_pmu_start()
643 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100644 pmu_disable_counter(cci_pmu, idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100645 pmu_event_update(event);
646 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100647}
648
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100649static int cci_pmu_add(struct perf_event *event, int flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100650{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100651 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
652 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
653 struct hw_perf_event *hwc = &event->hw;
654 int idx;
655 int err = 0;
656
657 perf_pmu_disable(event->pmu);
658
659 /* If we don't have a space for the counter then finish early. */
660 idx = pmu_get_event_idx(hw_events, event);
661 if (idx < 0) {
662 err = idx;
663 goto out;
664 }
665
666 event->hw.idx = idx;
667 hw_events->events[idx] = event;
668
669 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
670 if (flags & PERF_EF_START)
671 cci_pmu_start(event, PERF_EF_RELOAD);
672
673 /* Propagate our changes to the userspace mapping. */
674 perf_event_update_userpage(event);
675
676out:
677 perf_pmu_enable(event->pmu);
678 return err;
679}
680
681static void cci_pmu_del(struct perf_event *event, int flags)
682{
683 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
684 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
685 struct hw_perf_event *hwc = &event->hw;
686 int idx = hwc->idx;
687
688 cci_pmu_stop(event, PERF_EF_UPDATE);
689 hw_events->events[idx] = NULL;
690 clear_bit(idx, hw_events->used_mask);
691
692 perf_event_update_userpage(event);
693}
694
695static int
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +0000696validate_event(struct pmu *cci_pmu,
697 struct cci_pmu_hw_events *hw_events,
698 struct perf_event *event)
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100699{
700 if (is_software_event(event))
701 return 1;
702
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +0000703 /*
704 * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
705 * core perf code won't check that the pmu->ctx == leader->ctx
706 * until after pmu->event_init(event).
707 */
708 if (event->pmu != cci_pmu)
709 return 0;
710
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100711 if (event->state < PERF_EVENT_STATE_OFF)
712 return 1;
713
714 if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
715 return 1;
716
717 return pmu_get_event_idx(hw_events, event) >= 0;
718}
719
720static int
721validate_group(struct perf_event *event)
722{
723 struct perf_event *sibling, *leader = event->group_leader;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100724 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
725 unsigned long mask[BITS_TO_LONGS(cci_pmu->num_cntrs)];
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100726 struct cci_pmu_hw_events fake_pmu = {
727 /*
728 * Initialise the fake PMU. We only need to populate the
729 * used_mask for the purposes of validation.
730 */
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100731 .used_mask = mask,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100732 };
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100733 memset(mask, 0, BITS_TO_LONGS(cci_pmu->num_cntrs) * sizeof(unsigned long));
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100734
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +0000735 if (!validate_event(event->pmu, &fake_pmu, leader))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100736 return -EINVAL;
737
738 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +0000739 if (!validate_event(event->pmu, &fake_pmu, sibling))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100740 return -EINVAL;
741 }
742
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +0000743 if (!validate_event(event->pmu, &fake_pmu, event))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100744 return -EINVAL;
745
746 return 0;
747}
748
749static int
750__hw_perf_event_init(struct perf_event *event)
751{
752 struct hw_perf_event *hwc = &event->hw;
753 int mapping;
754
755 mapping = pmu_map_event(event);
756
757 if (mapping < 0) {
758 pr_debug("event %x:%llx not supported\n", event->attr.type,
759 event->attr.config);
760 return mapping;
761 }
762
763 /*
764 * We don't assign an index until we actually place the event onto
765 * hardware. Use -1 to signify that we haven't decided where to put it
766 * yet.
767 */
768 hwc->idx = -1;
769 hwc->config_base = 0;
770 hwc->config = 0;
771 hwc->event_base = 0;
772
773 /*
774 * Store the event encoding into the config_base field.
775 */
776 hwc->config_base |= (unsigned long)mapping;
777
778 /*
779 * Limit the sample_period to half of the counter width. That way, the
780 * new counter value is far less likely to overtake the previous one
781 * unless you have some serious IRQ latency issues.
782 */
783 hwc->sample_period = CCI_PMU_CNTR_MASK >> 1;
784 hwc->last_period = hwc->sample_period;
785 local64_set(&hwc->period_left, hwc->sample_period);
786
787 if (event->group_leader != event) {
788 if (validate_group(event) != 0)
789 return -EINVAL;
790 }
791
792 return 0;
793}
794
795static int cci_pmu_event_init(struct perf_event *event)
796{
797 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
798 atomic_t *active_events = &cci_pmu->active_events;
799 int err = 0;
800 int cpu;
801
802 if (event->attr.type != event->pmu->type)
803 return -ENOENT;
804
805 /* Shared by all CPUs, no meaningful state to sample */
806 if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
807 return -EOPNOTSUPP;
808
809 /* We have no filtering of any kind */
810 if (event->attr.exclude_user ||
811 event->attr.exclude_kernel ||
812 event->attr.exclude_hv ||
813 event->attr.exclude_idle ||
814 event->attr.exclude_host ||
815 event->attr.exclude_guest)
816 return -EINVAL;
817
818 /*
819 * Following the example set by other "uncore" PMUs, we accept any CPU
820 * and rewrite its affinity dynamically rather than having perf core
821 * handle cpu == -1 and pid == -1 for this case.
822 *
823 * The perf core will pin online CPUs for the duration of this call and
824 * the event being installed into its context, so the PMU's CPU can't
825 * change under our feet.
826 */
827 cpu = cpumask_first(&cci_pmu->cpus);
828 if (event->cpu < 0 || cpu < 0)
829 return -EINVAL;
830 event->cpu = cpu;
831
832 event->destroy = hw_perf_event_destroy;
833 if (!atomic_inc_not_zero(active_events)) {
834 mutex_lock(&cci_pmu->reserve_mutex);
835 if (atomic_read(active_events) == 0)
836 err = cci_pmu_get_hw(cci_pmu);
837 if (!err)
838 atomic_inc(active_events);
839 mutex_unlock(&cci_pmu->reserve_mutex);
840 }
841 if (err)
842 return err;
843
844 err = __hw_perf_event_init(event);
845 if (err)
846 hw_perf_event_destroy(event);
847
848 return err;
849}
850
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100851static ssize_t pmu_cpumask_attr_show(struct device *dev,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100852 struct device_attribute *attr, char *buf)
853{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100854 struct dev_ext_attribute *eattr = container_of(attr,
855 struct dev_ext_attribute, attr);
856 struct cci_pmu *cci_pmu = eattr->var;
857
Tejun Heo660e5ec2015-02-13 14:37:20 -0800858 int n = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100859 cpumask_pr_args(&cci_pmu->cpus));
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100860 buf[n++] = '\n';
861 buf[n] = '\0';
862 return n;
863}
864
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100865static struct dev_ext_attribute pmu_cpumask_attr = {
866 __ATTR(cpumask, S_IRUGO, pmu_cpumask_attr_show, NULL),
867 NULL, /* Populated in cci_pmu_init */
868};
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100869
870static struct attribute *pmu_attrs[] = {
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100871 &pmu_cpumask_attr.attr.attr,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100872 NULL,
873};
874
875static struct attribute_group pmu_attr_group = {
876 .attrs = pmu_attrs,
877};
878
879static const struct attribute_group *pmu_attr_groups[] = {
880 &pmu_attr_group,
881 NULL
882};
883
884static int cci_pmu_init(struct cci_pmu *cci_pmu, struct platform_device *pdev)
885{
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000886 char *name = cci_pmu->model->name;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100887 u32 num_cntrs;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100888
889 pmu_cpumask_attr.var = cci_pmu;
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100890 cci_pmu->pmu = (struct pmu) {
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000891 .name = cci_pmu->model->name,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100892 .task_ctx_nr = perf_invalid_context,
893 .pmu_enable = cci_pmu_enable,
894 .pmu_disable = cci_pmu_disable,
895 .event_init = cci_pmu_event_init,
896 .add = cci_pmu_add,
897 .del = cci_pmu_del,
898 .start = cci_pmu_start,
899 .stop = cci_pmu_stop,
900 .read = pmu_read,
901 .attr_groups = pmu_attr_groups,
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100902 };
903
904 cci_pmu->plat_device = pdev;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100905 num_cntrs = pmu_get_max_counters();
906 if (num_cntrs > cci_pmu->model->num_hw_cntrs) {
907 dev_warn(&pdev->dev,
908 "PMU implements more counters(%d) than supported by"
909 " the model(%d), truncated.",
910 num_cntrs, cci_pmu->model->num_hw_cntrs);
911 num_cntrs = cci_pmu->model->num_hw_cntrs;
912 }
913 cci_pmu->num_cntrs = num_cntrs + cci_pmu->model->fixed_hw_cntrs;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100914
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100915 return perf_pmu_register(&cci_pmu->pmu, name, -1);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100916}
917
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100918static int cci_pmu_cpu_notifier(struct notifier_block *self,
919 unsigned long action, void *hcpu)
920{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100921 struct cci_pmu *cci_pmu = container_of(self,
922 struct cci_pmu, cpu_nb);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100923 unsigned int cpu = (long)hcpu;
924 unsigned int target;
925
926 switch (action & ~CPU_TASKS_FROZEN) {
927 case CPU_DOWN_PREPARE:
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100928 if (!cpumask_test_and_clear_cpu(cpu, &cci_pmu->cpus))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100929 break;
930 target = cpumask_any_but(cpu_online_mask, cpu);
931 if (target < 0) // UP, last CPU
932 break;
933 /*
934 * TODO: migrate context once core races on event->ctx have
935 * been fixed.
936 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100937 cpumask_set_cpu(target, &cci_pmu->cpus);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100938 default:
939 break;
940 }
941
942 return NOTIFY_OK;
943}
944
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000945static struct cci_pmu_model cci_pmu_models[] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100946#ifdef CONFIG_ARM_CCI400_PMU
947 [CCI400_R0] = {
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000948 .name = "CCI_400",
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100949 .fixed_hw_cntrs = 1, /* Cycle counter */
950 .num_hw_cntrs = 4,
951 .cntr_size = SZ_4K,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000952 .event_ranges = {
953 [CCI_IF_SLAVE] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100954 CCI400_R0_SLAVE_PORT_MIN_EV,
955 CCI400_R0_SLAVE_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000956 },
957 [CCI_IF_MASTER] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100958 CCI400_R0_MASTER_PORT_MIN_EV,
959 CCI400_R0_MASTER_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000960 },
961 },
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100962 .validate_hw_event = cci400_validate_hw_event,
963 .get_event_idx = cci400_get_event_idx,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000964 },
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100965 [CCI400_R1] = {
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000966 .name = "CCI_400_r1",
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100967 .fixed_hw_cntrs = 1, /* Cycle counter */
968 .num_hw_cntrs = 4,
969 .cntr_size = SZ_4K,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000970 .event_ranges = {
971 [CCI_IF_SLAVE] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100972 CCI400_R1_SLAVE_PORT_MIN_EV,
973 CCI400_R1_SLAVE_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000974 },
975 [CCI_IF_MASTER] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100976 CCI400_R1_MASTER_PORT_MIN_EV,
977 CCI400_R1_MASTER_PORT_MAX_EV,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000978 },
979 },
Suzuki K. Poulose31216292015-05-26 10:53:13 +0100980 .validate_hw_event = cci400_validate_hw_event,
981 .get_event_idx = cci400_get_event_idx,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000982 },
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100983#endif
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000984};
985
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100986static const struct of_device_id arm_cci_pmu_matches[] = {
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100987#ifdef CONFIG_ARM_CCI400_PMU
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100988 {
989 .compatible = "arm,cci-400-pmu",
Suzuki K. Poulose772742a2015-03-18 12:24:40 +0000990 .data = NULL,
991 },
992 {
993 .compatible = "arm,cci-400-pmu,r0",
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100994 .data = &cci_pmu_models[CCI400_R0],
Suzuki K. Poulose772742a2015-03-18 12:24:40 +0000995 },
996 {
997 .compatible = "arm,cci-400-pmu,r1",
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +0100998 .data = &cci_pmu_models[CCI400_R1],
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100999 },
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001000#endif
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001001 {},
1002};
1003
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001004static inline const struct cci_pmu_model *get_cci_model(struct platform_device *pdev)
1005{
1006 const struct of_device_id *match = of_match_node(arm_cci_pmu_matches,
1007 pdev->dev.of_node);
1008 if (!match)
1009 return NULL;
Suzuki K. Poulose772742a2015-03-18 12:24:40 +00001010 if (match->data)
1011 return match->data;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001012
Suzuki K. Poulose772742a2015-03-18 12:24:40 +00001013 dev_warn(&pdev->dev, "DEPRECATED compatible property,"
1014 "requires secure access to CCI registers");
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001015 return probe_cci_model(pdev);
1016}
1017
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001018static bool is_duplicate_irq(int irq, int *irqs, int nr_irqs)
1019{
1020 int i;
1021
1022 for (i = 0; i < nr_irqs; i++)
1023 if (irq == irqs[i])
1024 return true;
1025
1026 return false;
1027}
1028
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001029static struct cci_pmu *cci_pmu_alloc(struct platform_device *pdev)
1030{
1031 struct cci_pmu *cci_pmu;
1032 const struct cci_pmu_model *model;
1033
1034 /*
1035 * All allocations are devm_* hence we don't have to free
1036 * them explicitly on an error, as it would end up in driver
1037 * detach.
1038 */
1039 model = get_cci_model(pdev);
1040 if (!model) {
1041 dev_warn(&pdev->dev, "CCI PMU version not supported\n");
1042 return ERR_PTR(-ENODEV);
1043 }
1044
1045 cci_pmu = devm_kzalloc(&pdev->dev, sizeof(*cci_pmu), GFP_KERNEL);
1046 if (!cci_pmu)
1047 return ERR_PTR(-ENOMEM);
1048
1049 cci_pmu->model = model;
1050 cci_pmu->irqs = devm_kcalloc(&pdev->dev, CCI_PMU_MAX_HW_CNTRS(model),
1051 sizeof(*cci_pmu->irqs), GFP_KERNEL);
1052 if (!cci_pmu->irqs)
1053 return ERR_PTR(-ENOMEM);
1054 cci_pmu->hw_events.events = devm_kcalloc(&pdev->dev,
1055 CCI_PMU_MAX_HW_CNTRS(model),
1056 sizeof(*cci_pmu->hw_events.events),
1057 GFP_KERNEL);
1058 if (!cci_pmu->hw_events.events)
1059 return ERR_PTR(-ENOMEM);
1060 cci_pmu->hw_events.used_mask = devm_kcalloc(&pdev->dev,
1061 BITS_TO_LONGS(CCI_PMU_MAX_HW_CNTRS(model)),
1062 sizeof(*cci_pmu->hw_events.used_mask),
1063 GFP_KERNEL);
1064 if (!cci_pmu->hw_events.used_mask)
1065 return ERR_PTR(-ENOMEM);
1066
1067 return cci_pmu;
1068}
1069
1070
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001071static int cci_pmu_probe(struct platform_device *pdev)
1072{
1073 struct resource *res;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001074 struct cci_pmu *cci_pmu;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001075 int i, ret, irq;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001076
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001077 cci_pmu = cci_pmu_alloc(pdev);
1078 if (IS_ERR(cci_pmu))
1079 return PTR_ERR(cci_pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001080
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001081 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001082 cci_pmu->base = devm_ioremap_resource(&pdev->dev, res);
1083 if (IS_ERR(cci_pmu->base))
Wei Yongjunfee4f2c2013-09-22 06:04:23 +01001084 return -ENOMEM;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001085
1086 /*
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001087 * CCI PMU has one overflow interrupt per counter; but some may be tied
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001088 * together to a common interrupt.
1089 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001090 cci_pmu->nr_irqs = 0;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001091 for (i = 0; i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model); i++) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001092 irq = platform_get_irq(pdev, i);
1093 if (irq < 0)
1094 break;
1095
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001096 if (is_duplicate_irq(irq, cci_pmu->irqs, cci_pmu->nr_irqs))
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001097 continue;
1098
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001099 cci_pmu->irqs[cci_pmu->nr_irqs++] = irq;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001100 }
1101
1102 /*
1103 * Ensure that the device tree has as many interrupts as the number
1104 * of counters.
1105 */
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001106 if (i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model)) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001107 dev_warn(&pdev->dev, "In-correct number of interrupts: %d, should be %d\n",
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001108 i, CCI_PMU_MAX_HW_CNTRS(cci_pmu->model));
Wei Yongjunfee4f2c2013-09-22 06:04:23 +01001109 return -EINVAL;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001110 }
1111
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001112 raw_spin_lock_init(&cci_pmu->hw_events.pmu_lock);
1113 mutex_init(&cci_pmu->reserve_mutex);
1114 atomic_set(&cci_pmu->active_events, 0);
1115 cpumask_set_cpu(smp_processor_id(), &cci_pmu->cpus);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001116
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001117 cci_pmu->cpu_nb = (struct notifier_block) {
1118 .notifier_call = cci_pmu_cpu_notifier,
1119 /*
1120 * to migrate uncore events, our notifier should be executed
1121 * before perf core's notifier.
1122 */
1123 .priority = CPU_PRI_PERF + 1,
1124 };
1125
1126 ret = register_cpu_notifier(&cci_pmu->cpu_nb);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001127 if (ret)
1128 return ret;
1129
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001130 ret = cci_pmu_init(cci_pmu, pdev);
1131 if (ret) {
1132 unregister_cpu_notifier(&cci_pmu->cpu_nb);
Wei Yongjunfee4f2c2013-09-22 06:04:23 +01001133 return ret;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001134 }
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001135
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001136 pr_info("ARM %s PMU driver probed", cci_pmu->model->name);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001137 return 0;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001138}
1139
1140static int cci_platform_probe(struct platform_device *pdev)
1141{
1142 if (!cci_probed())
1143 return -ENODEV;
1144
1145 return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
1146}
1147
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001148static struct platform_driver cci_pmu_driver = {
1149 .driver = {
1150 .name = DRIVER_NAME_PMU,
1151 .of_match_table = arm_cci_pmu_matches,
1152 },
1153 .probe = cci_pmu_probe,
1154};
1155
1156static struct platform_driver cci_platform_driver = {
1157 .driver = {
1158 .name = DRIVER_NAME,
1159 .of_match_table = arm_cci_matches,
1160 },
1161 .probe = cci_platform_probe,
1162};
1163
1164static int __init cci_platform_init(void)
1165{
1166 int ret;
1167
1168 ret = platform_driver_register(&cci_pmu_driver);
1169 if (ret)
1170 return ret;
1171
1172 return platform_driver_register(&cci_platform_driver);
1173}
1174
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001175#else /* !CONFIG_ARM_CCI_PMU */
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001176
1177static int __init cci_platform_init(void)
1178{
1179 return 0;
1180}
1181
Suzuki K. Poulosef4d58932015-05-26 10:53:14 +01001182#endif /* CONFIG_ARM_CCI_PMU */
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +00001183
1184#ifdef CONFIG_ARM_CCI400_PORT_CTRL
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001185
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001186#define CCI_PORT_CTRL 0x0
1187#define CCI_CTRL_STATUS 0xc
1188
1189#define CCI_ENABLE_SNOOP_REQ 0x1
1190#define CCI_ENABLE_DVM_REQ 0x2
1191#define CCI_ENABLE_REQ (CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ)
1192
1193enum cci_ace_port_type {
1194 ACE_INVALID_PORT = 0x0,
1195 ACE_PORT,
1196 ACE_LITE_PORT,
1197};
1198
1199struct cci_ace_port {
1200 void __iomem *base;
1201 unsigned long phys;
1202 enum cci_ace_port_type type;
1203 struct device_node *dn;
1204};
1205
1206static struct cci_ace_port *ports;
1207static unsigned int nb_cci_ports;
1208
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001209struct cpu_port {
1210 u64 mpidr;
1211 u32 port;
1212};
Nicolas Pitre62158f82013-05-21 23:34:41 -04001213
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001214/*
1215 * Use the port MSB as valid flag, shift can be made dynamic
1216 * by computing number of bits required for port indexes.
1217 * Code disabling CCI cpu ports runs with D-cache invalidated
1218 * and SCTLR bit clear so data accesses must be kept to a minimum
1219 * to improve performance; for now shift is left static to
1220 * avoid one more data access while disabling the CCI port.
1221 */
1222#define PORT_VALID_SHIFT 31
1223#define PORT_VALID (0x1 << PORT_VALID_SHIFT)
1224
1225static inline void init_cpu_port(struct cpu_port *port, u32 index, u64 mpidr)
1226{
1227 port->port = PORT_VALID | index;
1228 port->mpidr = mpidr;
1229}
1230
1231static inline bool cpu_port_is_valid(struct cpu_port *port)
1232{
1233 return !!(port->port & PORT_VALID);
1234}
1235
1236static inline bool cpu_port_match(struct cpu_port *port, u64 mpidr)
1237{
1238 return port->mpidr == (mpidr & MPIDR_HWID_BITMASK);
1239}
1240
1241static struct cpu_port cpu_port[NR_CPUS];
1242
1243/**
1244 * __cci_ace_get_port - Function to retrieve the port index connected to
1245 * a cpu or device.
1246 *
1247 * @dn: device node of the device to look-up
1248 * @type: port type
1249 *
1250 * Return value:
1251 * - CCI port index if success
1252 * - -ENODEV if failure
1253 */
1254static int __cci_ace_get_port(struct device_node *dn, int type)
1255{
1256 int i;
1257 bool ace_match;
1258 struct device_node *cci_portn;
1259
1260 cci_portn = of_parse_phandle(dn, "cci-control-port", 0);
1261 for (i = 0; i < nb_cci_ports; i++) {
1262 ace_match = ports[i].type == type;
1263 if (ace_match && cci_portn == ports[i].dn)
1264 return i;
1265 }
1266 return -ENODEV;
1267}
1268
1269int cci_ace_get_port(struct device_node *dn)
1270{
1271 return __cci_ace_get_port(dn, ACE_LITE_PORT);
1272}
1273EXPORT_SYMBOL_GPL(cci_ace_get_port);
1274
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001275static void cci_ace_init_ports(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001276{
Sudeep KarkadaNagesha78b4d6e2013-06-17 14:51:48 +01001277 int port, cpu;
1278 struct device_node *cpun;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001279
1280 /*
1281 * Port index look-up speeds up the function disabling ports by CPU,
1282 * since the logical to port index mapping is done once and does
1283 * not change after system boot.
1284 * The stashed index array is initialized for all possible CPUs
1285 * at probe time.
1286 */
Sudeep KarkadaNagesha78b4d6e2013-06-17 14:51:48 +01001287 for_each_possible_cpu(cpu) {
1288 /* too early to use cpu->of_node */
1289 cpun = of_get_cpu_node(cpu, NULL);
1290
1291 if (WARN(!cpun, "Missing cpu device node\n"))
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001292 continue;
1293
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001294 port = __cci_ace_get_port(cpun, ACE_PORT);
1295 if (port < 0)
1296 continue;
1297
1298 init_cpu_port(&cpu_port[cpu], port, cpu_logical_map(cpu));
1299 }
1300
1301 for_each_possible_cpu(cpu) {
1302 WARN(!cpu_port_is_valid(&cpu_port[cpu]),
1303 "CPU %u does not have an associated CCI port\n",
1304 cpu);
1305 }
1306}
1307/*
1308 * Functions to enable/disable a CCI interconnect slave port
1309 *
1310 * They are called by low-level power management code to disable slave
1311 * interfaces snoops and DVM broadcast.
1312 * Since they may execute with cache data allocation disabled and
1313 * after the caches have been cleaned and invalidated the functions provide
1314 * no explicit locking since they may run with D-cache disabled, so normal
1315 * cacheable kernel locks based on ldrex/strex may not work.
1316 * Locking has to be provided by BSP implementations to ensure proper
1317 * operations.
1318 */
1319
1320/**
1321 * cci_port_control() - function to control a CCI port
1322 *
1323 * @port: index of the port to setup
1324 * @enable: if true enables the port, if false disables it
1325 */
1326static void notrace cci_port_control(unsigned int port, bool enable)
1327{
1328 void __iomem *base = ports[port].base;
1329
1330 writel_relaxed(enable ? CCI_ENABLE_REQ : 0, base + CCI_PORT_CTRL);
1331 /*
1332 * This function is called from power down procedures
1333 * and must not execute any instruction that might
1334 * cause the processor to be put in a quiescent state
1335 * (eg wfi). Hence, cpu_relax() can not be added to this
1336 * read loop to optimize power, since it might hide possibly
1337 * disruptive operations.
1338 */
1339 while (readl_relaxed(cci_ctrl_base + CCI_CTRL_STATUS) & 0x1)
1340 ;
1341}
1342
1343/**
1344 * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
1345 * reference
1346 *
1347 * @mpidr: mpidr of the CPU whose CCI port should be disabled
1348 *
1349 * Disabling a CCI port for a CPU implies disabling the CCI port
1350 * controlling that CPU cluster. Code disabling CPU CCI ports
1351 * must make sure that the CPU running the code is the last active CPU
1352 * in the cluster ie all other CPUs are quiescent in a low power state.
1353 *
1354 * Return:
1355 * 0 on success
1356 * -ENODEV on port look-up failure
1357 */
1358int notrace cci_disable_port_by_cpu(u64 mpidr)
1359{
1360 int cpu;
1361 bool is_valid;
1362 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
1363 is_valid = cpu_port_is_valid(&cpu_port[cpu]);
1364 if (is_valid && cpu_port_match(&cpu_port[cpu], mpidr)) {
1365 cci_port_control(cpu_port[cpu].port, false);
1366 return 0;
1367 }
1368 }
1369 return -ENODEV;
1370}
1371EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu);
1372
1373/**
Nicolas Pitre62158f82013-05-21 23:34:41 -04001374 * cci_enable_port_for_self() - enable a CCI port for calling CPU
1375 *
1376 * Enabling a CCI port for the calling CPU implies enabling the CCI
1377 * port controlling that CPU's cluster. Caller must make sure that the
1378 * CPU running the code is the first active CPU in the cluster and all
1379 * other CPUs are quiescent in a low power state or waiting for this CPU
1380 * to complete the CCI initialization.
1381 *
1382 * Because this is called when the MMU is still off and with no stack,
1383 * the code must be position independent and ideally rely on callee
1384 * clobbered registers only. To achieve this we must code this function
1385 * entirely in assembler.
1386 *
1387 * On success this returns with the proper CCI port enabled. In case of
1388 * any failure this never returns as the inability to enable the CCI is
1389 * fatal and there is no possible recovery at this stage.
1390 */
1391asmlinkage void __naked cci_enable_port_for_self(void)
1392{
1393 asm volatile ("\n"
Arnd Bergmannf4902492013-06-03 15:15:36 +02001394" .arch armv7-a\n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04001395" mrc p15, 0, r0, c0, c0, 5 @ get MPIDR value \n"
1396" and r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n"
1397" adr r1, 5f \n"
1398" ldr r2, [r1] \n"
1399" add r1, r1, r2 @ &cpu_port \n"
1400" add ip, r1, %[sizeof_cpu_port] \n"
1401
1402 /* Loop over the cpu_port array looking for a matching MPIDR */
1403"1: ldr r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
1404" cmp r2, r0 @ compare MPIDR \n"
1405" bne 2f \n"
1406
1407 /* Found a match, now test port validity */
1408" ldr r3, [r1, %[offsetof_cpu_port_port]] \n"
1409" tst r3, #"__stringify(PORT_VALID)" \n"
1410" bne 3f \n"
1411
1412 /* no match, loop with the next cpu_port entry */
1413"2: add r1, r1, %[sizeof_struct_cpu_port] \n"
1414" cmp r1, ip @ done? \n"
1415" blo 1b \n"
1416
1417 /* CCI port not found -- cheaply try to stall this CPU */
1418"cci_port_not_found: \n"
1419" wfi \n"
1420" wfe \n"
1421" b cci_port_not_found \n"
1422
1423 /* Use matched port index to look up the corresponding ports entry */
1424"3: bic r3, r3, #"__stringify(PORT_VALID)" \n"
1425" adr r0, 6f \n"
1426" ldmia r0, {r1, r2} \n"
1427" sub r1, r1, r0 @ virt - phys \n"
1428" ldr r0, [r0, r2] @ *(&ports) \n"
1429" mov r2, %[sizeof_struct_ace_port] \n"
1430" mla r0, r2, r3, r0 @ &ports[index] \n"
1431" sub r0, r0, r1 @ virt_to_phys() \n"
1432
1433 /* Enable the CCI port */
1434" ldr r0, [r0, %[offsetof_port_phys]] \n"
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07001435" mov r3, %[cci_enable_req]\n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04001436" str r3, [r0, #"__stringify(CCI_PORT_CTRL)"] \n"
1437
1438 /* poll the status reg for completion */
1439" adr r1, 7f \n"
1440" ldr r0, [r1] \n"
1441" ldr r0, [r0, r1] @ cci_ctrl_base \n"
1442"4: ldr r1, [r0, #"__stringify(CCI_CTRL_STATUS)"] \n"
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07001443" tst r1, %[cci_control_status_bits] \n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04001444" bne 4b \n"
1445
1446" mov r0, #0 \n"
1447" bx lr \n"
1448
1449" .align 2 \n"
1450"5: .word cpu_port - . \n"
1451"6: .word . \n"
1452" .word ports - 6b \n"
1453"7: .word cci_ctrl_phys - . \n"
1454 : :
1455 [sizeof_cpu_port] "i" (sizeof(cpu_port)),
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07001456 [cci_enable_req] "i" cpu_to_le32(CCI_ENABLE_REQ),
1457 [cci_control_status_bits] "i" cpu_to_le32(1),
Nicolas Pitre62158f82013-05-21 23:34:41 -04001458#ifndef __ARMEB__
1459 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
1460#else
1461 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
1462#endif
1463 [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
1464 [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
1465 [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
1466 [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
1467
1468 unreachable();
1469}
1470
1471/**
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001472 * __cci_control_port_by_device() - function to control a CCI port by device
1473 * reference
1474 *
1475 * @dn: device node pointer of the device whose CCI port should be
1476 * controlled
1477 * @enable: if true enables the port, if false disables it
1478 *
1479 * Return:
1480 * 0 on success
1481 * -ENODEV on port look-up failure
1482 */
1483int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
1484{
1485 int port;
1486
1487 if (!dn)
1488 return -ENODEV;
1489
1490 port = __cci_ace_get_port(dn, ACE_LITE_PORT);
1491 if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n",
1492 dn->full_name))
1493 return -ENODEV;
1494 cci_port_control(port, enable);
1495 return 0;
1496}
1497EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
1498
1499/**
1500 * __cci_control_port_by_index() - function to control a CCI port by port index
1501 *
1502 * @port: port index previously retrieved with cci_ace_get_port()
1503 * @enable: if true enables the port, if false disables it
1504 *
1505 * Return:
1506 * 0 on success
1507 * -ENODEV on port index out of range
1508 * -EPERM if operation carried out on an ACE PORT
1509 */
1510int notrace __cci_control_port_by_index(u32 port, bool enable)
1511{
1512 if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
1513 return -ENODEV;
1514 /*
1515 * CCI control for ports connected to CPUS is extremely fragile
1516 * and must be made to go through a specific and controlled
1517 * interface (ie cci_disable_port_by_cpu(); control by general purpose
1518 * indexing is therefore disabled for ACE ports.
1519 */
1520 if (ports[port].type == ACE_PORT)
1521 return -EPERM;
1522
1523 cci_port_control(port, enable);
1524 return 0;
1525}
1526EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
1527
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001528static const struct of_device_id arm_cci_ctrl_if_matches[] = {
1529 {.compatible = "arm,cci-400-ctrl-if", },
1530 {},
1531};
1532
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001533static int cci_probe_ports(struct device_node *np)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001534{
1535 struct cci_nb_ports const *cci_config;
1536 int ret, i, nb_ace = 0, nb_ace_lite = 0;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001537 struct device_node *cp;
Nicolas Pitre62158f82013-05-21 23:34:41 -04001538 struct resource res;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001539 const char *match_str;
1540 bool is_ace;
1541
Abhilash Kesavan896ddd62015-01-10 08:41:35 +05301542
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001543 cci_config = of_match_node(arm_cci_matches, np)->data;
1544 if (!cci_config)
1545 return -ENODEV;
1546
1547 nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
1548
Lorenzo Pieralisi7c762032014-01-27 10:50:37 +00001549 ports = kcalloc(nb_cci_ports, sizeof(*ports), GFP_KERNEL);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001550 if (!ports)
1551 return -ENOMEM;
1552
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001553 for_each_child_of_node(np, cp) {
1554 if (!of_match_node(arm_cci_ctrl_if_matches, cp))
1555 continue;
1556
1557 i = nb_ace + nb_ace_lite;
1558
1559 if (i >= nb_cci_ports)
1560 break;
1561
1562 if (of_property_read_string(cp, "interface-type",
1563 &match_str)) {
1564 WARN(1, "node %s missing interface-type property\n",
1565 cp->full_name);
1566 continue;
1567 }
1568 is_ace = strcmp(match_str, "ace") == 0;
1569 if (!is_ace && strcmp(match_str, "ace-lite")) {
1570 WARN(1, "node %s containing invalid interface-type property, skipping it\n",
1571 cp->full_name);
1572 continue;
1573 }
1574
Nicolas Pitre62158f82013-05-21 23:34:41 -04001575 ret = of_address_to_resource(cp, 0, &res);
1576 if (!ret) {
1577 ports[i].base = ioremap(res.start, resource_size(&res));
1578 ports[i].phys = res.start;
1579 }
1580 if (ret || !ports[i].base) {
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001581 WARN(1, "unable to ioremap CCI port %d\n", i);
1582 continue;
1583 }
1584
1585 if (is_ace) {
1586 if (WARN_ON(nb_ace >= cci_config->nb_ace))
1587 continue;
1588 ports[i].type = ACE_PORT;
1589 ++nb_ace;
1590 } else {
1591 if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
1592 continue;
1593 ports[i].type = ACE_LITE_PORT;
1594 ++nb_ace_lite;
1595 }
1596 ports[i].dn = cp;
1597 }
1598
1599 /* initialize a stashed array of ACE ports to speed-up look-up */
1600 cci_ace_init_ports();
1601
1602 /*
1603 * Multi-cluster systems may need this data when non-coherent, during
1604 * cluster power-up/power-down. Make sure it reaches main memory.
1605 */
1606 sync_cache_w(&cci_ctrl_base);
Nicolas Pitre62158f82013-05-21 23:34:41 -04001607 sync_cache_w(&cci_ctrl_phys);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001608 sync_cache_w(&ports);
1609 sync_cache_w(&cpu_port);
1610 __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
1611 pr_info("ARM CCI driver probed\n");
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001612
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001613 return 0;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001614}
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +00001615#else /* !CONFIG_ARM_CCI400_PORT_CTRL */
1616static inline int cci_probe_ports(struct device_node *np)
1617{
1618 return 0;
1619}
1620#endif /* CONFIG_ARM_CCI400_PORT_CTRL */
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001621
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001622static int cci_probe(void)
1623{
1624 int ret;
1625 struct device_node *np;
1626 struct resource res;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001627
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001628 np = of_find_matching_node(NULL, arm_cci_matches);
1629 if(!np || !of_device_is_available(np))
1630 return -ENODEV;
1631
1632 ret = of_address_to_resource(np, 0, &res);
1633 if (!ret) {
1634 cci_ctrl_base = ioremap(res.start, resource_size(&res));
1635 cci_ctrl_phys = res.start;
1636 }
1637 if (ret || !cci_ctrl_base) {
1638 WARN(1, "unable to ioremap CCI ctrl\n");
1639 return -ENXIO;
1640 }
1641
1642 return cci_probe_ports(np);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001643}
1644
1645static int cci_init_status = -EAGAIN;
1646static DEFINE_MUTEX(cci_probing);
1647
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001648static int cci_init(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001649{
1650 if (cci_init_status != -EAGAIN)
1651 return cci_init_status;
1652
1653 mutex_lock(&cci_probing);
1654 if (cci_init_status == -EAGAIN)
1655 cci_init_status = cci_probe();
1656 mutex_unlock(&cci_probing);
1657 return cci_init_status;
1658}
1659
1660/*
1661 * To sort out early init calls ordering a helper function is provided to
1662 * check if the CCI driver has beed initialized. Function check if the driver
1663 * has been initialized, if not it calls the init function that probes
1664 * the driver and updates the return value.
1665 */
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001666bool cci_probed(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001667{
1668 return cci_init() == 0;
1669}
1670EXPORT_SYMBOL_GPL(cci_probed);
1671
1672early_initcall(cci_init);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001673core_initcall(cci_platform_init);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001674MODULE_LICENSE("GPL");
1675MODULE_DESCRIPTION("ARM CCI support");