blob: 602bf64fc6908d7f81f0d9dda1b45874144fad36 [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. Pouloseee8e5d52015-03-18 12:24:41 +000058#ifdef CONFIG_ARM_CCI400_PMU
Punit Agrawalb91c8f22013-08-22 14:41:51 +010059
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000060#define DRIVER_NAME "CCI-400"
61#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. Poulose874c5712015-03-18 12:24:42 +000085#define CCI_PMU_EVENT_MASK 0xffUL
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000086#define CCI_PMU_EVENT_SOURCE(event) ((event >> 5) & 0x7)
87#define CCI_PMU_EVENT_CODE(event) (event & 0x1f)
88
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +010089#define CCI_PMU_MAX_HW_CNTRS(model) \
90 ((model)->num_hw_cntrs + (model)->fixed_hw_cntrs)
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000091
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +000092/* Types of interfaces that can generate events */
93enum {
94 CCI_IF_SLAVE,
95 CCI_IF_MASTER,
96 CCI_IF_MAX,
97};
98
99struct event_range {
100 u32 min;
101 u32 max;
102};
103
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000104struct cci_pmu_hw_events {
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100105 struct perf_event **events;
106 unsigned long *used_mask;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000107 raw_spinlock_t pmu_lock;
108};
109
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100110/*
111 * struct cci_pmu_model:
112 * @fixed_hw_cntrs - Number of fixed event counters
113 * @num_hw_cntrs - Maximum number of programmable event counters
114 * @cntr_size - Size of an event counter mapping
115 */
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000116struct cci_pmu_model {
117 char *name;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100118 u32 fixed_hw_cntrs;
119 u32 num_hw_cntrs;
120 u32 cntr_size;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000121 struct event_range event_ranges[CCI_IF_MAX];
122};
123
124static struct cci_pmu_model cci_pmu_models[];
125
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000126struct cci_pmu {
127 void __iomem *base;
128 struct pmu pmu;
129 int nr_irqs;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100130 int *irqs;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000131 unsigned long active_irqs;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000132 const struct cci_pmu_model *model;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000133 struct cci_pmu_hw_events hw_events;
134 struct platform_device *plat_device;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100135 int num_cntrs;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000136 atomic_t active_events;
137 struct mutex reserve_mutex;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100138 struct notifier_block cpu_nb;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000139 cpumask_t cpus;
140};
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000141
142#define to_cci_pmu(c) (container_of(c, struct cci_pmu, pmu))
143
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100144/* Port ids */
145#define CCI_PORT_S0 0
146#define CCI_PORT_S1 1
147#define CCI_PORT_S2 2
148#define CCI_PORT_S3 3
149#define CCI_PORT_S4 4
150#define CCI_PORT_M0 5
151#define CCI_PORT_M1 6
152#define CCI_PORT_M2 7
153
154#define CCI_REV_R0 0
155#define CCI_REV_R1 1
Punit Agrawal6fb0c4a2014-02-19 12:17:02 +0000156#define CCI_REV_R1_PX 5
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100157
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100158/*
159 * Instead of an event id to monitor CCI cycles, a dedicated counter is
160 * provided. Use 0xff to represent CCI cycles and hope that no future revisions
161 * make use of this event in hardware.
162 */
163enum cci400_perf_events {
164 CCI_PMU_CYCLES = 0xff
165};
166
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100167#define CCI_PMU_CYCLE_CNTR_IDX 0
168#define CCI_PMU_CNTR0_IDX 1
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100169
170/*
171 * CCI PMU event id is an 8-bit value made of two parts - bits 7:5 for one of 8
172 * ports and bits 4:0 are event codes. There are different event codes
173 * associated with each port type.
174 *
175 * Additionally, the range of events associated with the port types changed
176 * between Rev0 and Rev1.
177 *
178 * The constants below define the range of valid codes for each port type for
179 * the different revisions and are used to validate the event to be monitored.
180 */
181
182#define CCI_REV_R0_SLAVE_PORT_MIN_EV 0x00
183#define CCI_REV_R0_SLAVE_PORT_MAX_EV 0x13
184#define CCI_REV_R0_MASTER_PORT_MIN_EV 0x14
185#define CCI_REV_R0_MASTER_PORT_MAX_EV 0x1a
186
187#define CCI_REV_R1_SLAVE_PORT_MIN_EV 0x00
188#define CCI_REV_R1_SLAVE_PORT_MAX_EV 0x14
189#define CCI_REV_R1_MASTER_PORT_MIN_EV 0x00
190#define CCI_REV_R1_MASTER_PORT_MAX_EV 0x11
191
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100192static int pmu_validate_hw_event(struct cci_pmu *cci_pmu, unsigned long hw_event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100193{
194 u8 ev_source = CCI_PMU_EVENT_SOURCE(hw_event);
195 u8 ev_code = CCI_PMU_EVENT_CODE(hw_event);
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000196 int if_type;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100197
Suzuki K. Poulose874c5712015-03-18 12:24:42 +0000198 if (hw_event & ~CCI_PMU_EVENT_MASK)
199 return -ENOENT;
200
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100201 switch (ev_source) {
202 case CCI_PORT_S0:
203 case CCI_PORT_S1:
204 case CCI_PORT_S2:
205 case CCI_PORT_S3:
206 case CCI_PORT_S4:
207 /* Slave Interface */
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000208 if_type = CCI_IF_SLAVE;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100209 break;
210 case CCI_PORT_M0:
211 case CCI_PORT_M1:
212 case CCI_PORT_M2:
213 /* Master Interface */
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000214 if_type = CCI_IF_MASTER;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100215 break;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000216 default:
217 return -ENOENT;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100218 }
219
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100220 if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
221 ev_code <= cci_pmu->model->event_ranges[if_type].max)
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000222 return hw_event;
223
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100224 return -ENOENT;
225}
226
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000227static int probe_cci_revision(void)
228{
229 int rev;
230 rev = readl_relaxed(cci_ctrl_base + CCI_PID2) & CCI_PID2_REV_MASK;
231 rev >>= CCI_PID2_REV_SHIFT;
232
233 if (rev < CCI_REV_R1_PX)
234 return CCI_REV_R0;
235 else
236 return CCI_REV_R1;
237}
238
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000239static const struct cci_pmu_model *probe_cci_model(struct platform_device *pdev)
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000240{
Suzuki K. Poulose772742a2015-03-18 12:24:40 +0000241 if (platform_has_secure_cci_access())
242 return &cci_pmu_models[probe_cci_revision()];
243 return NULL;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000244}
245
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100246static int pmu_is_valid_counter(struct cci_pmu *cci_pmu, int idx)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100247{
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100248 return 0 <= idx && idx <= CCI_PMU_CNTR_LAST(cci_pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100249}
250
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100251static u32 pmu_read_register(struct cci_pmu *cci_pmu, int idx, unsigned int offset)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100252{
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100253 return readl_relaxed(cci_pmu->base +
254 CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100255}
256
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100257static void pmu_write_register(struct cci_pmu *cci_pmu, u32 value,
258 int idx, unsigned int offset)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100259{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100260 return writel_relaxed(value, cci_pmu->base +
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100261 CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100262}
263
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100264static void pmu_disable_counter(struct cci_pmu *cci_pmu, int idx)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100265{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100266 pmu_write_register(cci_pmu, 0, idx, CCI_PMU_CNTR_CTRL);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100267}
268
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100269static void pmu_enable_counter(struct cci_pmu *cci_pmu, int idx)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100270{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100271 pmu_write_register(cci_pmu, 1, idx, CCI_PMU_CNTR_CTRL);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100272}
273
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100274static void pmu_set_event(struct cci_pmu *cci_pmu, int idx, unsigned long event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100275{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100276 pmu_write_register(cci_pmu, event, idx, CCI_PMU_EVT_SEL);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100277}
278
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100279/*
280 * Returns the number of programmable counters actually implemented
281 * by the cci
282 */
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100283static u32 pmu_get_max_counters(void)
284{
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100285 return (readl_relaxed(cci_ctrl_base + CCI_PMCR) &
286 CCI_PMCR_NCNT_MASK) >> CCI_PMCR_NCNT_SHIFT;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100287}
288
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100289static int pmu_get_event_idx(struct cci_pmu_hw_events *hw, struct perf_event *event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100290{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100291 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100292 struct hw_perf_event *hw_event = &event->hw;
Suzuki K. Poulose874c5712015-03-18 12:24:42 +0000293 unsigned long cci_event = hw_event->config_base;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100294 int idx;
295
296 if (cci_event == CCI_PMU_CYCLES) {
297 if (test_and_set_bit(CCI_PMU_CYCLE_CNTR_IDX, hw->used_mask))
298 return -EAGAIN;
299
300 return CCI_PMU_CYCLE_CNTR_IDX;
301 }
302
303 for (idx = CCI_PMU_CNTR0_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); ++idx)
304 if (!test_and_set_bit(idx, hw->used_mask))
305 return idx;
306
307 /* No counters available */
308 return -EAGAIN;
309}
310
311static int pmu_map_event(struct perf_event *event)
312{
313 int mapping;
Suzuki K. Poulose874c5712015-03-18 12:24:42 +0000314 unsigned long config = event->attr.config;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100315
316 if (event->attr.type < PERF_TYPE_MAX)
317 return -ENOENT;
318
319 if (config == CCI_PMU_CYCLES)
320 mapping = config;
321 else
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100322 mapping = pmu_validate_hw_event(to_cci_pmu(event->pmu),
323 config);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100324
325 return mapping;
326}
327
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100328static int pmu_request_irq(struct cci_pmu *cci_pmu, irq_handler_t handler)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100329{
330 int i;
331 struct platform_device *pmu_device = cci_pmu->plat_device;
332
333 if (unlikely(!pmu_device))
334 return -ENODEV;
335
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100336 if (cci_pmu->nr_irqs < 1) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100337 dev_err(&pmu_device->dev, "no irqs for CCI PMUs defined\n");
338 return -ENODEV;
339 }
340
341 /*
342 * Register all available CCI PMU interrupts. In the interrupt handler
343 * we iterate over the counters checking for interrupt source (the
344 * overflowing counter) and clear it.
345 *
346 * This should allow handling of non-unique interrupt for the counters.
347 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100348 for (i = 0; i < cci_pmu->nr_irqs; i++) {
349 int err = request_irq(cci_pmu->irqs[i], handler, IRQF_SHARED,
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100350 "arm-cci-pmu", cci_pmu);
351 if (err) {
352 dev_err(&pmu_device->dev, "unable to request IRQ%d for ARM CCI PMU counters\n",
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100353 cci_pmu->irqs[i]);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100354 return err;
355 }
356
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100357 set_bit(i, &cci_pmu->active_irqs);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100358 }
359
360 return 0;
361}
362
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100363static void pmu_free_irq(struct cci_pmu *cci_pmu)
364{
365 int i;
366
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100367 for (i = 0; i < cci_pmu->nr_irqs; i++) {
368 if (!test_and_clear_bit(i, &cci_pmu->active_irqs))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100369 continue;
370
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100371 free_irq(cci_pmu->irqs[i], cci_pmu);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100372 }
373}
374
375static u32 pmu_read_counter(struct perf_event *event)
376{
377 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
378 struct hw_perf_event *hw_counter = &event->hw;
379 int idx = hw_counter->idx;
380 u32 value;
381
382 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
383 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
384 return 0;
385 }
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100386 value = pmu_read_register(cci_pmu, idx, CCI_PMU_CNTR);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100387
388 return value;
389}
390
391static void pmu_write_counter(struct perf_event *event, u32 value)
392{
393 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
394 struct hw_perf_event *hw_counter = &event->hw;
395 int idx = hw_counter->idx;
396
397 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx)))
398 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
399 else
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100400 pmu_write_register(cci_pmu, value, idx, CCI_PMU_CNTR);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100401}
402
403static u64 pmu_event_update(struct perf_event *event)
404{
405 struct hw_perf_event *hwc = &event->hw;
406 u64 delta, prev_raw_count, new_raw_count;
407
408 do {
409 prev_raw_count = local64_read(&hwc->prev_count);
410 new_raw_count = pmu_read_counter(event);
411 } while (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
412 new_raw_count) != prev_raw_count);
413
414 delta = (new_raw_count - prev_raw_count) & CCI_PMU_CNTR_MASK;
415
416 local64_add(delta, &event->count);
417
418 return new_raw_count;
419}
420
421static void pmu_read(struct perf_event *event)
422{
423 pmu_event_update(event);
424}
425
426void pmu_event_set_period(struct perf_event *event)
427{
428 struct hw_perf_event *hwc = &event->hw;
429 /*
430 * The CCI PMU counters have a period of 2^32. To account for the
431 * possiblity of extreme interrupt latency we program for a period of
432 * half that. Hopefully we can handle the interrupt before another 2^31
433 * events occur and the counter overtakes its previous value.
434 */
435 u64 val = 1ULL << 31;
436 local64_set(&hwc->prev_count, val);
437 pmu_write_counter(event, val);
438}
439
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100440static irqreturn_t pmu_handle_irq(int irq_num, void *dev)
441{
442 unsigned long flags;
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100443 struct cci_pmu *cci_pmu = dev;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100444 struct cci_pmu_hw_events *events = &cci_pmu->hw_events;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100445 int idx, handled = IRQ_NONE;
446
447 raw_spin_lock_irqsave(&events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100448 /*
449 * Iterate over counters and update the corresponding perf events.
450 * This should work regardless of whether we have per-counter overflow
451 * interrupt or a combined overflow interrupt.
452 */
453 for (idx = CCI_PMU_CYCLE_CNTR_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++) {
454 struct perf_event *event = events->events[idx];
455 struct hw_perf_event *hw_counter;
456
457 if (!event)
458 continue;
459
460 hw_counter = &event->hw;
461
462 /* Did this counter overflow? */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100463 if (!(pmu_read_register(cci_pmu, idx, CCI_PMU_OVRFLW) &
Himangi Saraogifc5130d2014-07-30 11:37:35 +0100464 CCI_PMU_OVRFLW_FLAG))
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100465 continue;
466
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100467 pmu_write_register(cci_pmu, CCI_PMU_OVRFLW_FLAG, idx,
468 CCI_PMU_OVRFLW);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100469
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100470 pmu_event_update(event);
471 pmu_event_set_period(event);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100472 handled = IRQ_HANDLED;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100473 }
474 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
475
476 return IRQ_RETVAL(handled);
477}
478
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100479static int cci_pmu_get_hw(struct cci_pmu *cci_pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100480{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100481 int ret = pmu_request_irq(cci_pmu, pmu_handle_irq);
482 if (ret) {
483 pmu_free_irq(cci_pmu);
484 return ret;
485 }
486 return 0;
487}
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100488
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100489static void cci_pmu_put_hw(struct cci_pmu *cci_pmu)
490{
491 pmu_free_irq(cci_pmu);
492}
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100493
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100494static void hw_perf_event_destroy(struct perf_event *event)
495{
496 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
497 atomic_t *active_events = &cci_pmu->active_events;
498 struct mutex *reserve_mutex = &cci_pmu->reserve_mutex;
499
500 if (atomic_dec_and_mutex_lock(active_events, reserve_mutex)) {
501 cci_pmu_put_hw(cci_pmu);
502 mutex_unlock(reserve_mutex);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100503 }
504}
505
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100506static void cci_pmu_enable(struct pmu *pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100507{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100508 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
509 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100510 int enabled = bitmap_weight(hw_events->used_mask, cci_pmu->num_cntrs);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100511 unsigned long flags;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100512 u32 val;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100513
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100514 if (!enabled)
515 return;
516
517 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100518
519 /* Enable all the PMU counters. */
520 val = readl_relaxed(cci_ctrl_base + CCI_PMCR) | CCI_PMCR_CEN;
521 writel(val, cci_ctrl_base + CCI_PMCR);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100522 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100523
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100524}
525
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100526static void cci_pmu_disable(struct pmu *pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100527{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100528 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
529 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100530 unsigned long flags;
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100531 u32 val;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100532
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100533 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100534
535 /* Disable all the PMU counters. */
536 val = readl_relaxed(cci_ctrl_base + CCI_PMCR) & ~CCI_PMCR_CEN;
537 writel(val, cci_ctrl_base + CCI_PMCR);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100538 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100539}
540
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100541static void cci_pmu_start(struct perf_event *event, int pmu_flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100542{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100543 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
544 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
545 struct hw_perf_event *hwc = &event->hw;
546 int idx = hwc->idx;
547 unsigned long flags;
548
549 /*
550 * To handle interrupt latency, we always reprogram the period
551 * regardlesss of PERF_EF_RELOAD.
552 */
553 if (pmu_flags & PERF_EF_RELOAD)
554 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
555
556 hwc->state = 0;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100557
558 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
559 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100560 return;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100561 }
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100562
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100563 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
564
565 /* Configure the event to count, unless you are counting cycles */
566 if (idx != CCI_PMU_CYCLE_CNTR_IDX)
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100567 pmu_set_event(cci_pmu, idx, hwc->config_base);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100568
569 pmu_event_set_period(event);
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100570 pmu_enable_counter(cci_pmu, idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100571
572 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100573}
574
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100575static void cci_pmu_stop(struct perf_event *event, int pmu_flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100576{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100577 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
578 struct hw_perf_event *hwc = &event->hw;
579 int idx = hwc->idx;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100580
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100581 if (hwc->state & PERF_HES_STOPPED)
582 return;
583
584 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100585 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100586 return;
587 }
588
589 /*
590 * We always reprogram the counter, so ignore PERF_EF_UPDATE. See
591 * cci_pmu_start()
592 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100593 pmu_disable_counter(cci_pmu, idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100594 pmu_event_update(event);
595 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100596}
597
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100598static int cci_pmu_add(struct perf_event *event, int flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100599{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100600 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
601 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
602 struct hw_perf_event *hwc = &event->hw;
603 int idx;
604 int err = 0;
605
606 perf_pmu_disable(event->pmu);
607
608 /* If we don't have a space for the counter then finish early. */
609 idx = pmu_get_event_idx(hw_events, event);
610 if (idx < 0) {
611 err = idx;
612 goto out;
613 }
614
615 event->hw.idx = idx;
616 hw_events->events[idx] = event;
617
618 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
619 if (flags & PERF_EF_START)
620 cci_pmu_start(event, PERF_EF_RELOAD);
621
622 /* Propagate our changes to the userspace mapping. */
623 perf_event_update_userpage(event);
624
625out:
626 perf_pmu_enable(event->pmu);
627 return err;
628}
629
630static void cci_pmu_del(struct perf_event *event, int flags)
631{
632 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
633 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
634 struct hw_perf_event *hwc = &event->hw;
635 int idx = hwc->idx;
636
637 cci_pmu_stop(event, PERF_EF_UPDATE);
638 hw_events->events[idx] = NULL;
639 clear_bit(idx, hw_events->used_mask);
640
641 perf_event_update_userpage(event);
642}
643
644static int
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +0000645validate_event(struct pmu *cci_pmu,
646 struct cci_pmu_hw_events *hw_events,
647 struct perf_event *event)
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100648{
649 if (is_software_event(event))
650 return 1;
651
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +0000652 /*
653 * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
654 * core perf code won't check that the pmu->ctx == leader->ctx
655 * until after pmu->event_init(event).
656 */
657 if (event->pmu != cci_pmu)
658 return 0;
659
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100660 if (event->state < PERF_EVENT_STATE_OFF)
661 return 1;
662
663 if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
664 return 1;
665
666 return pmu_get_event_idx(hw_events, event) >= 0;
667}
668
669static int
670validate_group(struct perf_event *event)
671{
672 struct perf_event *sibling, *leader = event->group_leader;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100673 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
674 unsigned long mask[BITS_TO_LONGS(cci_pmu->num_cntrs)];
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100675 struct cci_pmu_hw_events fake_pmu = {
676 /*
677 * Initialise the fake PMU. We only need to populate the
678 * used_mask for the purposes of validation.
679 */
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100680 .used_mask = mask,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100681 };
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100682 memset(mask, 0, BITS_TO_LONGS(cci_pmu->num_cntrs) * sizeof(unsigned long));
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100683
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +0000684 if (!validate_event(event->pmu, &fake_pmu, leader))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100685 return -EINVAL;
686
687 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +0000688 if (!validate_event(event->pmu, &fake_pmu, sibling))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100689 return -EINVAL;
690 }
691
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +0000692 if (!validate_event(event->pmu, &fake_pmu, event))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100693 return -EINVAL;
694
695 return 0;
696}
697
698static int
699__hw_perf_event_init(struct perf_event *event)
700{
701 struct hw_perf_event *hwc = &event->hw;
702 int mapping;
703
704 mapping = pmu_map_event(event);
705
706 if (mapping < 0) {
707 pr_debug("event %x:%llx not supported\n", event->attr.type,
708 event->attr.config);
709 return mapping;
710 }
711
712 /*
713 * We don't assign an index until we actually place the event onto
714 * hardware. Use -1 to signify that we haven't decided where to put it
715 * yet.
716 */
717 hwc->idx = -1;
718 hwc->config_base = 0;
719 hwc->config = 0;
720 hwc->event_base = 0;
721
722 /*
723 * Store the event encoding into the config_base field.
724 */
725 hwc->config_base |= (unsigned long)mapping;
726
727 /*
728 * Limit the sample_period to half of the counter width. That way, the
729 * new counter value is far less likely to overtake the previous one
730 * unless you have some serious IRQ latency issues.
731 */
732 hwc->sample_period = CCI_PMU_CNTR_MASK >> 1;
733 hwc->last_period = hwc->sample_period;
734 local64_set(&hwc->period_left, hwc->sample_period);
735
736 if (event->group_leader != event) {
737 if (validate_group(event) != 0)
738 return -EINVAL;
739 }
740
741 return 0;
742}
743
744static int cci_pmu_event_init(struct perf_event *event)
745{
746 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
747 atomic_t *active_events = &cci_pmu->active_events;
748 int err = 0;
749 int cpu;
750
751 if (event->attr.type != event->pmu->type)
752 return -ENOENT;
753
754 /* Shared by all CPUs, no meaningful state to sample */
755 if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
756 return -EOPNOTSUPP;
757
758 /* We have no filtering of any kind */
759 if (event->attr.exclude_user ||
760 event->attr.exclude_kernel ||
761 event->attr.exclude_hv ||
762 event->attr.exclude_idle ||
763 event->attr.exclude_host ||
764 event->attr.exclude_guest)
765 return -EINVAL;
766
767 /*
768 * Following the example set by other "uncore" PMUs, we accept any CPU
769 * and rewrite its affinity dynamically rather than having perf core
770 * handle cpu == -1 and pid == -1 for this case.
771 *
772 * The perf core will pin online CPUs for the duration of this call and
773 * the event being installed into its context, so the PMU's CPU can't
774 * change under our feet.
775 */
776 cpu = cpumask_first(&cci_pmu->cpus);
777 if (event->cpu < 0 || cpu < 0)
778 return -EINVAL;
779 event->cpu = cpu;
780
781 event->destroy = hw_perf_event_destroy;
782 if (!atomic_inc_not_zero(active_events)) {
783 mutex_lock(&cci_pmu->reserve_mutex);
784 if (atomic_read(active_events) == 0)
785 err = cci_pmu_get_hw(cci_pmu);
786 if (!err)
787 atomic_inc(active_events);
788 mutex_unlock(&cci_pmu->reserve_mutex);
789 }
790 if (err)
791 return err;
792
793 err = __hw_perf_event_init(event);
794 if (err)
795 hw_perf_event_destroy(event);
796
797 return err;
798}
799
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100800static ssize_t pmu_cpumask_attr_show(struct device *dev,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100801 struct device_attribute *attr, char *buf)
802{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100803 struct dev_ext_attribute *eattr = container_of(attr,
804 struct dev_ext_attribute, attr);
805 struct cci_pmu *cci_pmu = eattr->var;
806
Tejun Heo660e5ec2015-02-13 14:37:20 -0800807 int n = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100808 cpumask_pr_args(&cci_pmu->cpus));
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100809 buf[n++] = '\n';
810 buf[n] = '\0';
811 return n;
812}
813
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100814static struct dev_ext_attribute pmu_cpumask_attr = {
815 __ATTR(cpumask, S_IRUGO, pmu_cpumask_attr_show, NULL),
816 NULL, /* Populated in cci_pmu_init */
817};
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100818
819static struct attribute *pmu_attrs[] = {
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100820 &pmu_cpumask_attr.attr.attr,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100821 NULL,
822};
823
824static struct attribute_group pmu_attr_group = {
825 .attrs = pmu_attrs,
826};
827
828static const struct attribute_group *pmu_attr_groups[] = {
829 &pmu_attr_group,
830 NULL
831};
832
833static int cci_pmu_init(struct cci_pmu *cci_pmu, struct platform_device *pdev)
834{
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000835 char *name = cci_pmu->model->name;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100836 u32 num_cntrs;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100837
838 pmu_cpumask_attr.var = cci_pmu;
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100839 cci_pmu->pmu = (struct pmu) {
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000840 .name = cci_pmu->model->name,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100841 .task_ctx_nr = perf_invalid_context,
842 .pmu_enable = cci_pmu_enable,
843 .pmu_disable = cci_pmu_disable,
844 .event_init = cci_pmu_event_init,
845 .add = cci_pmu_add,
846 .del = cci_pmu_del,
847 .start = cci_pmu_start,
848 .stop = cci_pmu_stop,
849 .read = pmu_read,
850 .attr_groups = pmu_attr_groups,
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100851 };
852
853 cci_pmu->plat_device = pdev;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100854 num_cntrs = pmu_get_max_counters();
855 if (num_cntrs > cci_pmu->model->num_hw_cntrs) {
856 dev_warn(&pdev->dev,
857 "PMU implements more counters(%d) than supported by"
858 " the model(%d), truncated.",
859 num_cntrs, cci_pmu->model->num_hw_cntrs);
860 num_cntrs = cci_pmu->model->num_hw_cntrs;
861 }
862 cci_pmu->num_cntrs = num_cntrs + cci_pmu->model->fixed_hw_cntrs;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100863
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100864 return perf_pmu_register(&cci_pmu->pmu, name, -1);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100865}
866
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100867static int cci_pmu_cpu_notifier(struct notifier_block *self,
868 unsigned long action, void *hcpu)
869{
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100870 struct cci_pmu *cci_pmu = container_of(self,
871 struct cci_pmu, cpu_nb);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100872 unsigned int cpu = (long)hcpu;
873 unsigned int target;
874
875 switch (action & ~CPU_TASKS_FROZEN) {
876 case CPU_DOWN_PREPARE:
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100877 if (!cpumask_test_and_clear_cpu(cpu, &cci_pmu->cpus))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100878 break;
879 target = cpumask_any_but(cpu_online_mask, cpu);
880 if (target < 0) // UP, last CPU
881 break;
882 /*
883 * TODO: migrate context once core races on event->ctx have
884 * been fixed.
885 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +0100886 cpumask_set_cpu(target, &cci_pmu->cpus);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100887 default:
888 break;
889 }
890
891 return NOTIFY_OK;
892}
893
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000894static struct cci_pmu_model cci_pmu_models[] = {
895 [CCI_REV_R0] = {
896 .name = "CCI_400",
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100897 .fixed_hw_cntrs = 1, /* Cycle counter */
898 .num_hw_cntrs = 4,
899 .cntr_size = SZ_4K,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000900 .event_ranges = {
901 [CCI_IF_SLAVE] = {
902 CCI_REV_R0_SLAVE_PORT_MIN_EV,
903 CCI_REV_R0_SLAVE_PORT_MAX_EV,
904 },
905 [CCI_IF_MASTER] = {
906 CCI_REV_R0_MASTER_PORT_MIN_EV,
907 CCI_REV_R0_MASTER_PORT_MAX_EV,
908 },
909 },
910 },
911 [CCI_REV_R1] = {
912 .name = "CCI_400_r1",
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100913 .fixed_hw_cntrs = 1, /* Cycle counter */
914 .num_hw_cntrs = 4,
915 .cntr_size = SZ_4K,
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000916 .event_ranges = {
917 [CCI_IF_SLAVE] = {
918 CCI_REV_R1_SLAVE_PORT_MIN_EV,
919 CCI_REV_R1_SLAVE_PORT_MAX_EV,
920 },
921 [CCI_IF_MASTER] = {
922 CCI_REV_R1_MASTER_PORT_MIN_EV,
923 CCI_REV_R1_MASTER_PORT_MAX_EV,
924 },
925 },
926 },
927};
928
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100929static const struct of_device_id arm_cci_pmu_matches[] = {
930 {
931 .compatible = "arm,cci-400-pmu",
Suzuki K. Poulose772742a2015-03-18 12:24:40 +0000932 .data = NULL,
933 },
934 {
935 .compatible = "arm,cci-400-pmu,r0",
936 .data = &cci_pmu_models[CCI_REV_R0],
937 },
938 {
939 .compatible = "arm,cci-400-pmu,r1",
940 .data = &cci_pmu_models[CCI_REV_R1],
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100941 },
942 {},
943};
944
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000945static inline const struct cci_pmu_model *get_cci_model(struct platform_device *pdev)
946{
947 const struct of_device_id *match = of_match_node(arm_cci_pmu_matches,
948 pdev->dev.of_node);
949 if (!match)
950 return NULL;
Suzuki K. Poulose772742a2015-03-18 12:24:40 +0000951 if (match->data)
952 return match->data;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000953
Suzuki K. Poulose772742a2015-03-18 12:24:40 +0000954 dev_warn(&pdev->dev, "DEPRECATED compatible property,"
955 "requires secure access to CCI registers");
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000956 return probe_cci_model(pdev);
957}
958
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000959static bool is_duplicate_irq(int irq, int *irqs, int nr_irqs)
960{
961 int i;
962
963 for (i = 0; i < nr_irqs; i++)
964 if (irq == irqs[i])
965 return true;
966
967 return false;
968}
969
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +0100970static struct cci_pmu *cci_pmu_alloc(struct platform_device *pdev)
971{
972 struct cci_pmu *cci_pmu;
973 const struct cci_pmu_model *model;
974
975 /*
976 * All allocations are devm_* hence we don't have to free
977 * them explicitly on an error, as it would end up in driver
978 * detach.
979 */
980 model = get_cci_model(pdev);
981 if (!model) {
982 dev_warn(&pdev->dev, "CCI PMU version not supported\n");
983 return ERR_PTR(-ENODEV);
984 }
985
986 cci_pmu = devm_kzalloc(&pdev->dev, sizeof(*cci_pmu), GFP_KERNEL);
987 if (!cci_pmu)
988 return ERR_PTR(-ENOMEM);
989
990 cci_pmu->model = model;
991 cci_pmu->irqs = devm_kcalloc(&pdev->dev, CCI_PMU_MAX_HW_CNTRS(model),
992 sizeof(*cci_pmu->irqs), GFP_KERNEL);
993 if (!cci_pmu->irqs)
994 return ERR_PTR(-ENOMEM);
995 cci_pmu->hw_events.events = devm_kcalloc(&pdev->dev,
996 CCI_PMU_MAX_HW_CNTRS(model),
997 sizeof(*cci_pmu->hw_events.events),
998 GFP_KERNEL);
999 if (!cci_pmu->hw_events.events)
1000 return ERR_PTR(-ENOMEM);
1001 cci_pmu->hw_events.used_mask = devm_kcalloc(&pdev->dev,
1002 BITS_TO_LONGS(CCI_PMU_MAX_HW_CNTRS(model)),
1003 sizeof(*cci_pmu->hw_events.used_mask),
1004 GFP_KERNEL);
1005 if (!cci_pmu->hw_events.used_mask)
1006 return ERR_PTR(-ENOMEM);
1007
1008 return cci_pmu;
1009}
1010
1011
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001012static int cci_pmu_probe(struct platform_device *pdev)
1013{
1014 struct resource *res;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001015 struct cci_pmu *cci_pmu;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001016 int i, ret, irq;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +00001017
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001018 cci_pmu = cci_pmu_alloc(pdev);
1019 if (IS_ERR(cci_pmu))
1020 return PTR_ERR(cci_pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001021
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001022 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001023 cci_pmu->base = devm_ioremap_resource(&pdev->dev, res);
1024 if (IS_ERR(cci_pmu->base))
Wei Yongjunfee4f2c2013-09-22 06:04:23 +01001025 return -ENOMEM;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001026
1027 /*
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001028 * CCI PMU has one overflow interrupt per counter; but some may be tied
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001029 * together to a common interrupt.
1030 */
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001031 cci_pmu->nr_irqs = 0;
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001032 for (i = 0; i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model); i++) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001033 irq = platform_get_irq(pdev, i);
1034 if (irq < 0)
1035 break;
1036
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001037 if (is_duplicate_irq(irq, cci_pmu->irqs, cci_pmu->nr_irqs))
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001038 continue;
1039
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001040 cci_pmu->irqs[cci_pmu->nr_irqs++] = irq;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001041 }
1042
1043 /*
1044 * Ensure that the device tree has as many interrupts as the number
1045 * of counters.
1046 */
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001047 if (i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model)) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001048 dev_warn(&pdev->dev, "In-correct number of interrupts: %d, should be %d\n",
Suzuki K. Pouloseab5b3162015-05-26 10:53:12 +01001049 i, CCI_PMU_MAX_HW_CNTRS(cci_pmu->model));
Wei Yongjunfee4f2c2013-09-22 06:04:23 +01001050 return -EINVAL;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001051 }
1052
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001053 raw_spin_lock_init(&cci_pmu->hw_events.pmu_lock);
1054 mutex_init(&cci_pmu->reserve_mutex);
1055 atomic_set(&cci_pmu->active_events, 0);
1056 cpumask_set_cpu(smp_processor_id(), &cci_pmu->cpus);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001057
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001058 cci_pmu->cpu_nb = (struct notifier_block) {
1059 .notifier_call = cci_pmu_cpu_notifier,
1060 /*
1061 * to migrate uncore events, our notifier should be executed
1062 * before perf core's notifier.
1063 */
1064 .priority = CPU_PRI_PERF + 1,
1065 };
1066
1067 ret = register_cpu_notifier(&cci_pmu->cpu_nb);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +01001068 if (ret)
1069 return ret;
1070
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001071 ret = cci_pmu_init(cci_pmu, pdev);
1072 if (ret) {
1073 unregister_cpu_notifier(&cci_pmu->cpu_nb);
Wei Yongjunfee4f2c2013-09-22 06:04:23 +01001074 return ret;
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001075 }
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001076
Suzuki K. Poulosea1a076d2015-05-26 10:53:11 +01001077 pr_info("ARM %s PMU driver probed", cci_pmu->model->name);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001078 return 0;
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001079}
1080
1081static int cci_platform_probe(struct platform_device *pdev)
1082{
1083 if (!cci_probed())
1084 return -ENODEV;
1085
1086 return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
1087}
1088
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001089static struct platform_driver cci_pmu_driver = {
1090 .driver = {
1091 .name = DRIVER_NAME_PMU,
1092 .of_match_table = arm_cci_pmu_matches,
1093 },
1094 .probe = cci_pmu_probe,
1095};
1096
1097static struct platform_driver cci_platform_driver = {
1098 .driver = {
1099 .name = DRIVER_NAME,
1100 .of_match_table = arm_cci_matches,
1101 },
1102 .probe = cci_platform_probe,
1103};
1104
1105static int __init cci_platform_init(void)
1106{
1107 int ret;
1108
1109 ret = platform_driver_register(&cci_pmu_driver);
1110 if (ret)
1111 return ret;
1112
1113 return platform_driver_register(&cci_platform_driver);
1114}
1115
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +00001116#else /* !CONFIG_ARM_CCI400_PMU */
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001117
1118static int __init cci_platform_init(void)
1119{
1120 return 0;
1121}
1122
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +00001123#endif /* CONFIG_ARM_CCI400_PMU */
1124
1125#ifdef CONFIG_ARM_CCI400_PORT_CTRL
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001126
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001127#define CCI_PORT_CTRL 0x0
1128#define CCI_CTRL_STATUS 0xc
1129
1130#define CCI_ENABLE_SNOOP_REQ 0x1
1131#define CCI_ENABLE_DVM_REQ 0x2
1132#define CCI_ENABLE_REQ (CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ)
1133
1134enum cci_ace_port_type {
1135 ACE_INVALID_PORT = 0x0,
1136 ACE_PORT,
1137 ACE_LITE_PORT,
1138};
1139
1140struct cci_ace_port {
1141 void __iomem *base;
1142 unsigned long phys;
1143 enum cci_ace_port_type type;
1144 struct device_node *dn;
1145};
1146
1147static struct cci_ace_port *ports;
1148static unsigned int nb_cci_ports;
1149
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001150struct cpu_port {
1151 u64 mpidr;
1152 u32 port;
1153};
Nicolas Pitre62158f82013-05-21 23:34:41 -04001154
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001155/*
1156 * Use the port MSB as valid flag, shift can be made dynamic
1157 * by computing number of bits required for port indexes.
1158 * Code disabling CCI cpu ports runs with D-cache invalidated
1159 * and SCTLR bit clear so data accesses must be kept to a minimum
1160 * to improve performance; for now shift is left static to
1161 * avoid one more data access while disabling the CCI port.
1162 */
1163#define PORT_VALID_SHIFT 31
1164#define PORT_VALID (0x1 << PORT_VALID_SHIFT)
1165
1166static inline void init_cpu_port(struct cpu_port *port, u32 index, u64 mpidr)
1167{
1168 port->port = PORT_VALID | index;
1169 port->mpidr = mpidr;
1170}
1171
1172static inline bool cpu_port_is_valid(struct cpu_port *port)
1173{
1174 return !!(port->port & PORT_VALID);
1175}
1176
1177static inline bool cpu_port_match(struct cpu_port *port, u64 mpidr)
1178{
1179 return port->mpidr == (mpidr & MPIDR_HWID_BITMASK);
1180}
1181
1182static struct cpu_port cpu_port[NR_CPUS];
1183
1184/**
1185 * __cci_ace_get_port - Function to retrieve the port index connected to
1186 * a cpu or device.
1187 *
1188 * @dn: device node of the device to look-up
1189 * @type: port type
1190 *
1191 * Return value:
1192 * - CCI port index if success
1193 * - -ENODEV if failure
1194 */
1195static int __cci_ace_get_port(struct device_node *dn, int type)
1196{
1197 int i;
1198 bool ace_match;
1199 struct device_node *cci_portn;
1200
1201 cci_portn = of_parse_phandle(dn, "cci-control-port", 0);
1202 for (i = 0; i < nb_cci_ports; i++) {
1203 ace_match = ports[i].type == type;
1204 if (ace_match && cci_portn == ports[i].dn)
1205 return i;
1206 }
1207 return -ENODEV;
1208}
1209
1210int cci_ace_get_port(struct device_node *dn)
1211{
1212 return __cci_ace_get_port(dn, ACE_LITE_PORT);
1213}
1214EXPORT_SYMBOL_GPL(cci_ace_get_port);
1215
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001216static void cci_ace_init_ports(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001217{
Sudeep KarkadaNagesha78b4d6e2013-06-17 14:51:48 +01001218 int port, cpu;
1219 struct device_node *cpun;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001220
1221 /*
1222 * Port index look-up speeds up the function disabling ports by CPU,
1223 * since the logical to port index mapping is done once and does
1224 * not change after system boot.
1225 * The stashed index array is initialized for all possible CPUs
1226 * at probe time.
1227 */
Sudeep KarkadaNagesha78b4d6e2013-06-17 14:51:48 +01001228 for_each_possible_cpu(cpu) {
1229 /* too early to use cpu->of_node */
1230 cpun = of_get_cpu_node(cpu, NULL);
1231
1232 if (WARN(!cpun, "Missing cpu device node\n"))
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001233 continue;
1234
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001235 port = __cci_ace_get_port(cpun, ACE_PORT);
1236 if (port < 0)
1237 continue;
1238
1239 init_cpu_port(&cpu_port[cpu], port, cpu_logical_map(cpu));
1240 }
1241
1242 for_each_possible_cpu(cpu) {
1243 WARN(!cpu_port_is_valid(&cpu_port[cpu]),
1244 "CPU %u does not have an associated CCI port\n",
1245 cpu);
1246 }
1247}
1248/*
1249 * Functions to enable/disable a CCI interconnect slave port
1250 *
1251 * They are called by low-level power management code to disable slave
1252 * interfaces snoops and DVM broadcast.
1253 * Since they may execute with cache data allocation disabled and
1254 * after the caches have been cleaned and invalidated the functions provide
1255 * no explicit locking since they may run with D-cache disabled, so normal
1256 * cacheable kernel locks based on ldrex/strex may not work.
1257 * Locking has to be provided by BSP implementations to ensure proper
1258 * operations.
1259 */
1260
1261/**
1262 * cci_port_control() - function to control a CCI port
1263 *
1264 * @port: index of the port to setup
1265 * @enable: if true enables the port, if false disables it
1266 */
1267static void notrace cci_port_control(unsigned int port, bool enable)
1268{
1269 void __iomem *base = ports[port].base;
1270
1271 writel_relaxed(enable ? CCI_ENABLE_REQ : 0, base + CCI_PORT_CTRL);
1272 /*
1273 * This function is called from power down procedures
1274 * and must not execute any instruction that might
1275 * cause the processor to be put in a quiescent state
1276 * (eg wfi). Hence, cpu_relax() can not be added to this
1277 * read loop to optimize power, since it might hide possibly
1278 * disruptive operations.
1279 */
1280 while (readl_relaxed(cci_ctrl_base + CCI_CTRL_STATUS) & 0x1)
1281 ;
1282}
1283
1284/**
1285 * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
1286 * reference
1287 *
1288 * @mpidr: mpidr of the CPU whose CCI port should be disabled
1289 *
1290 * Disabling a CCI port for a CPU implies disabling the CCI port
1291 * controlling that CPU cluster. Code disabling CPU CCI ports
1292 * must make sure that the CPU running the code is the last active CPU
1293 * in the cluster ie all other CPUs are quiescent in a low power state.
1294 *
1295 * Return:
1296 * 0 on success
1297 * -ENODEV on port look-up failure
1298 */
1299int notrace cci_disable_port_by_cpu(u64 mpidr)
1300{
1301 int cpu;
1302 bool is_valid;
1303 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
1304 is_valid = cpu_port_is_valid(&cpu_port[cpu]);
1305 if (is_valid && cpu_port_match(&cpu_port[cpu], mpidr)) {
1306 cci_port_control(cpu_port[cpu].port, false);
1307 return 0;
1308 }
1309 }
1310 return -ENODEV;
1311}
1312EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu);
1313
1314/**
Nicolas Pitre62158f82013-05-21 23:34:41 -04001315 * cci_enable_port_for_self() - enable a CCI port for calling CPU
1316 *
1317 * Enabling a CCI port for the calling CPU implies enabling the CCI
1318 * port controlling that CPU's cluster. Caller must make sure that the
1319 * CPU running the code is the first active CPU in the cluster and all
1320 * other CPUs are quiescent in a low power state or waiting for this CPU
1321 * to complete the CCI initialization.
1322 *
1323 * Because this is called when the MMU is still off and with no stack,
1324 * the code must be position independent and ideally rely on callee
1325 * clobbered registers only. To achieve this we must code this function
1326 * entirely in assembler.
1327 *
1328 * On success this returns with the proper CCI port enabled. In case of
1329 * any failure this never returns as the inability to enable the CCI is
1330 * fatal and there is no possible recovery at this stage.
1331 */
1332asmlinkage void __naked cci_enable_port_for_self(void)
1333{
1334 asm volatile ("\n"
Arnd Bergmannf4902492013-06-03 15:15:36 +02001335" .arch armv7-a\n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04001336" mrc p15, 0, r0, c0, c0, 5 @ get MPIDR value \n"
1337" and r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n"
1338" adr r1, 5f \n"
1339" ldr r2, [r1] \n"
1340" add r1, r1, r2 @ &cpu_port \n"
1341" add ip, r1, %[sizeof_cpu_port] \n"
1342
1343 /* Loop over the cpu_port array looking for a matching MPIDR */
1344"1: ldr r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
1345" cmp r2, r0 @ compare MPIDR \n"
1346" bne 2f \n"
1347
1348 /* Found a match, now test port validity */
1349" ldr r3, [r1, %[offsetof_cpu_port_port]] \n"
1350" tst r3, #"__stringify(PORT_VALID)" \n"
1351" bne 3f \n"
1352
1353 /* no match, loop with the next cpu_port entry */
1354"2: add r1, r1, %[sizeof_struct_cpu_port] \n"
1355" cmp r1, ip @ done? \n"
1356" blo 1b \n"
1357
1358 /* CCI port not found -- cheaply try to stall this CPU */
1359"cci_port_not_found: \n"
1360" wfi \n"
1361" wfe \n"
1362" b cci_port_not_found \n"
1363
1364 /* Use matched port index to look up the corresponding ports entry */
1365"3: bic r3, r3, #"__stringify(PORT_VALID)" \n"
1366" adr r0, 6f \n"
1367" ldmia r0, {r1, r2} \n"
1368" sub r1, r1, r0 @ virt - phys \n"
1369" ldr r0, [r0, r2] @ *(&ports) \n"
1370" mov r2, %[sizeof_struct_ace_port] \n"
1371" mla r0, r2, r3, r0 @ &ports[index] \n"
1372" sub r0, r0, r1 @ virt_to_phys() \n"
1373
1374 /* Enable the CCI port */
1375" ldr r0, [r0, %[offsetof_port_phys]] \n"
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07001376" mov r3, %[cci_enable_req]\n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04001377" str r3, [r0, #"__stringify(CCI_PORT_CTRL)"] \n"
1378
1379 /* poll the status reg for completion */
1380" adr r1, 7f \n"
1381" ldr r0, [r1] \n"
1382" ldr r0, [r0, r1] @ cci_ctrl_base \n"
1383"4: ldr r1, [r0, #"__stringify(CCI_CTRL_STATUS)"] \n"
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07001384" tst r1, %[cci_control_status_bits] \n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04001385" bne 4b \n"
1386
1387" mov r0, #0 \n"
1388" bx lr \n"
1389
1390" .align 2 \n"
1391"5: .word cpu_port - . \n"
1392"6: .word . \n"
1393" .word ports - 6b \n"
1394"7: .word cci_ctrl_phys - . \n"
1395 : :
1396 [sizeof_cpu_port] "i" (sizeof(cpu_port)),
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07001397 [cci_enable_req] "i" cpu_to_le32(CCI_ENABLE_REQ),
1398 [cci_control_status_bits] "i" cpu_to_le32(1),
Nicolas Pitre62158f82013-05-21 23:34:41 -04001399#ifndef __ARMEB__
1400 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
1401#else
1402 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
1403#endif
1404 [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
1405 [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
1406 [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
1407 [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
1408
1409 unreachable();
1410}
1411
1412/**
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001413 * __cci_control_port_by_device() - function to control a CCI port by device
1414 * reference
1415 *
1416 * @dn: device node pointer of the device whose CCI port should be
1417 * controlled
1418 * @enable: if true enables the port, if false disables it
1419 *
1420 * Return:
1421 * 0 on success
1422 * -ENODEV on port look-up failure
1423 */
1424int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
1425{
1426 int port;
1427
1428 if (!dn)
1429 return -ENODEV;
1430
1431 port = __cci_ace_get_port(dn, ACE_LITE_PORT);
1432 if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n",
1433 dn->full_name))
1434 return -ENODEV;
1435 cci_port_control(port, enable);
1436 return 0;
1437}
1438EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
1439
1440/**
1441 * __cci_control_port_by_index() - function to control a CCI port by port index
1442 *
1443 * @port: port index previously retrieved with cci_ace_get_port()
1444 * @enable: if true enables the port, if false disables it
1445 *
1446 * Return:
1447 * 0 on success
1448 * -ENODEV on port index out of range
1449 * -EPERM if operation carried out on an ACE PORT
1450 */
1451int notrace __cci_control_port_by_index(u32 port, bool enable)
1452{
1453 if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
1454 return -ENODEV;
1455 /*
1456 * CCI control for ports connected to CPUS is extremely fragile
1457 * and must be made to go through a specific and controlled
1458 * interface (ie cci_disable_port_by_cpu(); control by general purpose
1459 * indexing is therefore disabled for ACE ports.
1460 */
1461 if (ports[port].type == ACE_PORT)
1462 return -EPERM;
1463
1464 cci_port_control(port, enable);
1465 return 0;
1466}
1467EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
1468
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001469static const struct of_device_id arm_cci_ctrl_if_matches[] = {
1470 {.compatible = "arm,cci-400-ctrl-if", },
1471 {},
1472};
1473
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001474static int cci_probe_ports(struct device_node *np)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001475{
1476 struct cci_nb_ports const *cci_config;
1477 int ret, i, nb_ace = 0, nb_ace_lite = 0;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001478 struct device_node *cp;
Nicolas Pitre62158f82013-05-21 23:34:41 -04001479 struct resource res;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001480 const char *match_str;
1481 bool is_ace;
1482
Abhilash Kesavan896ddd62015-01-10 08:41:35 +05301483
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001484 cci_config = of_match_node(arm_cci_matches, np)->data;
1485 if (!cci_config)
1486 return -ENODEV;
1487
1488 nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
1489
Lorenzo Pieralisi7c762032014-01-27 10:50:37 +00001490 ports = kcalloc(nb_cci_ports, sizeof(*ports), GFP_KERNEL);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001491 if (!ports)
1492 return -ENOMEM;
1493
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001494 for_each_child_of_node(np, cp) {
1495 if (!of_match_node(arm_cci_ctrl_if_matches, cp))
1496 continue;
1497
1498 i = nb_ace + nb_ace_lite;
1499
1500 if (i >= nb_cci_ports)
1501 break;
1502
1503 if (of_property_read_string(cp, "interface-type",
1504 &match_str)) {
1505 WARN(1, "node %s missing interface-type property\n",
1506 cp->full_name);
1507 continue;
1508 }
1509 is_ace = strcmp(match_str, "ace") == 0;
1510 if (!is_ace && strcmp(match_str, "ace-lite")) {
1511 WARN(1, "node %s containing invalid interface-type property, skipping it\n",
1512 cp->full_name);
1513 continue;
1514 }
1515
Nicolas Pitre62158f82013-05-21 23:34:41 -04001516 ret = of_address_to_resource(cp, 0, &res);
1517 if (!ret) {
1518 ports[i].base = ioremap(res.start, resource_size(&res));
1519 ports[i].phys = res.start;
1520 }
1521 if (ret || !ports[i].base) {
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001522 WARN(1, "unable to ioremap CCI port %d\n", i);
1523 continue;
1524 }
1525
1526 if (is_ace) {
1527 if (WARN_ON(nb_ace >= cci_config->nb_ace))
1528 continue;
1529 ports[i].type = ACE_PORT;
1530 ++nb_ace;
1531 } else {
1532 if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
1533 continue;
1534 ports[i].type = ACE_LITE_PORT;
1535 ++nb_ace_lite;
1536 }
1537 ports[i].dn = cp;
1538 }
1539
1540 /* initialize a stashed array of ACE ports to speed-up look-up */
1541 cci_ace_init_ports();
1542
1543 /*
1544 * Multi-cluster systems may need this data when non-coherent, during
1545 * cluster power-up/power-down. Make sure it reaches main memory.
1546 */
1547 sync_cache_w(&cci_ctrl_base);
Nicolas Pitre62158f82013-05-21 23:34:41 -04001548 sync_cache_w(&cci_ctrl_phys);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001549 sync_cache_w(&ports);
1550 sync_cache_w(&cpu_port);
1551 __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
1552 pr_info("ARM CCI driver probed\n");
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001553
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001554 return 0;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001555}
Suzuki K. Pouloseee8e5d52015-03-18 12:24:41 +00001556#else /* !CONFIG_ARM_CCI400_PORT_CTRL */
1557static inline int cci_probe_ports(struct device_node *np)
1558{
1559 return 0;
1560}
1561#endif /* CONFIG_ARM_CCI400_PORT_CTRL */
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001562
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001563static int cci_probe(void)
1564{
1565 int ret;
1566 struct device_node *np;
1567 struct resource res;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001568
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001569 np = of_find_matching_node(NULL, arm_cci_matches);
1570 if(!np || !of_device_is_available(np))
1571 return -ENODEV;
1572
1573 ret = of_address_to_resource(np, 0, &res);
1574 if (!ret) {
1575 cci_ctrl_base = ioremap(res.start, resource_size(&res));
1576 cci_ctrl_phys = res.start;
1577 }
1578 if (ret || !cci_ctrl_base) {
1579 WARN(1, "unable to ioremap CCI ctrl\n");
1580 return -ENXIO;
1581 }
1582
1583 return cci_probe_ports(np);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001584}
1585
1586static int cci_init_status = -EAGAIN;
1587static DEFINE_MUTEX(cci_probing);
1588
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001589static int cci_init(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001590{
1591 if (cci_init_status != -EAGAIN)
1592 return cci_init_status;
1593
1594 mutex_lock(&cci_probing);
1595 if (cci_init_status == -EAGAIN)
1596 cci_init_status = cci_probe();
1597 mutex_unlock(&cci_probing);
1598 return cci_init_status;
1599}
1600
1601/*
1602 * To sort out early init calls ordering a helper function is provided to
1603 * check if the CCI driver has beed initialized. Function check if the driver
1604 * has been initialized, if not it calls the init function that probes
1605 * the driver and updates the return value.
1606 */
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001607bool cci_probed(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001608{
1609 return cci_init() == 0;
1610}
1611EXPORT_SYMBOL_GPL(cci_probed);
1612
1613early_initcall(cci_init);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001614core_initcall(cci_platform_init);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001615MODULE_LICENSE("GPL");
1616MODULE_DESCRIPTION("ARM CCI support");