blob: a23663c7a306422b7373202be27fec17dae13b6e [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
35struct cci_nb_ports {
36 unsigned int nb_ace;
37 unsigned int nb_ace_lite;
38};
39
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000040static const struct cci_nb_ports cci400_ports = {
41 .nb_ace = 2,
42 .nb_ace_lite = 3
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010043};
44
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000045static const struct of_device_id arm_cci_matches[] = {
46 {.compatible = "arm,cci-400", .data = &cci400_ports },
47 {},
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010048};
49
Punit Agrawalb91c8f22013-08-22 14:41:51 +010050#ifdef CONFIG_HW_PERF_EVENTS
51
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000052#define DRIVER_NAME "CCI-400"
53#define DRIVER_NAME_PMU DRIVER_NAME " PMU"
54
Punit Agrawalb91c8f22013-08-22 14:41:51 +010055#define CCI_PMCR 0x0100
56#define CCI_PID2 0x0fe8
57
58#define CCI_PMCR_CEN 0x00000001
59#define CCI_PMCR_NCNT_MASK 0x0000f800
60#define CCI_PMCR_NCNT_SHIFT 11
61
62#define CCI_PID2_REV_MASK 0xf0
63#define CCI_PID2_REV_SHIFT 4
64
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000065#define CCI_PMU_EVT_SEL 0x000
66#define CCI_PMU_CNTR 0x004
67#define CCI_PMU_CNTR_CTRL 0x008
68#define CCI_PMU_OVRFLW 0x00c
69
70#define CCI_PMU_OVRFLW_FLAG 1
71
72#define CCI_PMU_CNTR_BASE(idx) ((idx) * SZ_4K)
73
74#define CCI_PMU_CNTR_MASK ((1ULL << 32) -1)
75
76#define CCI_PMU_EVENT_MASK 0xff
77#define CCI_PMU_EVENT_SOURCE(event) ((event >> 5) & 0x7)
78#define CCI_PMU_EVENT_CODE(event) (event & 0x1f)
79
80#define CCI_PMU_MAX_HW_EVENTS 5 /* CCI PMU has 4 counters + 1 cycle counter */
81
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +000082/* Types of interfaces that can generate events */
83enum {
84 CCI_IF_SLAVE,
85 CCI_IF_MASTER,
86 CCI_IF_MAX,
87};
88
89struct event_range {
90 u32 min;
91 u32 max;
92};
93
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +000094struct cci_pmu_hw_events {
95 struct perf_event *events[CCI_PMU_MAX_HW_EVENTS];
96 unsigned long used_mask[BITS_TO_LONGS(CCI_PMU_MAX_HW_EVENTS)];
97 raw_spinlock_t pmu_lock;
98};
99
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000100struct cci_pmu_model {
101 char *name;
102 struct event_range event_ranges[CCI_IF_MAX];
103};
104
105static struct cci_pmu_model cci_pmu_models[];
106
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000107struct cci_pmu {
108 void __iomem *base;
109 struct pmu pmu;
110 int nr_irqs;
111 int irqs[CCI_PMU_MAX_HW_EVENTS];
112 unsigned long active_irqs;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000113 const struct cci_pmu_model *model;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000114 struct cci_pmu_hw_events hw_events;
115 struct platform_device *plat_device;
116 int num_events;
117 atomic_t active_events;
118 struct mutex reserve_mutex;
119 cpumask_t cpus;
120};
121static struct cci_pmu *pmu;
122
123#define to_cci_pmu(c) (container_of(c, struct cci_pmu, pmu))
124
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100125/* Port ids */
126#define CCI_PORT_S0 0
127#define CCI_PORT_S1 1
128#define CCI_PORT_S2 2
129#define CCI_PORT_S3 3
130#define CCI_PORT_S4 4
131#define CCI_PORT_M0 5
132#define CCI_PORT_M1 6
133#define CCI_PORT_M2 7
134
135#define CCI_REV_R0 0
136#define CCI_REV_R1 1
Punit Agrawal6fb0c4a2014-02-19 12:17:02 +0000137#define CCI_REV_R1_PX 5
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100138
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100139/*
140 * Instead of an event id to monitor CCI cycles, a dedicated counter is
141 * provided. Use 0xff to represent CCI cycles and hope that no future revisions
142 * make use of this event in hardware.
143 */
144enum cci400_perf_events {
145 CCI_PMU_CYCLES = 0xff
146};
147
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100148#define CCI_PMU_CYCLE_CNTR_IDX 0
149#define CCI_PMU_CNTR0_IDX 1
150#define CCI_PMU_CNTR_LAST(cci_pmu) (CCI_PMU_CYCLE_CNTR_IDX + cci_pmu->num_events - 1)
151
152/*
153 * CCI PMU event id is an 8-bit value made of two parts - bits 7:5 for one of 8
154 * ports and bits 4:0 are event codes. There are different event codes
155 * associated with each port type.
156 *
157 * Additionally, the range of events associated with the port types changed
158 * between Rev0 and Rev1.
159 *
160 * The constants below define the range of valid codes for each port type for
161 * the different revisions and are used to validate the event to be monitored.
162 */
163
164#define CCI_REV_R0_SLAVE_PORT_MIN_EV 0x00
165#define CCI_REV_R0_SLAVE_PORT_MAX_EV 0x13
166#define CCI_REV_R0_MASTER_PORT_MIN_EV 0x14
167#define CCI_REV_R0_MASTER_PORT_MAX_EV 0x1a
168
169#define CCI_REV_R1_SLAVE_PORT_MIN_EV 0x00
170#define CCI_REV_R1_SLAVE_PORT_MAX_EV 0x14
171#define CCI_REV_R1_MASTER_PORT_MIN_EV 0x00
172#define CCI_REV_R1_MASTER_PORT_MAX_EV 0x11
173
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100174static int pmu_validate_hw_event(u8 hw_event)
175{
176 u8 ev_source = CCI_PMU_EVENT_SOURCE(hw_event);
177 u8 ev_code = CCI_PMU_EVENT_CODE(hw_event);
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000178 int if_type;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100179
180 switch (ev_source) {
181 case CCI_PORT_S0:
182 case CCI_PORT_S1:
183 case CCI_PORT_S2:
184 case CCI_PORT_S3:
185 case CCI_PORT_S4:
186 /* Slave Interface */
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000187 if_type = CCI_IF_SLAVE;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100188 break;
189 case CCI_PORT_M0:
190 case CCI_PORT_M1:
191 case CCI_PORT_M2:
192 /* Master Interface */
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000193 if_type = CCI_IF_MASTER;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100194 break;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000195 default:
196 return -ENOENT;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100197 }
198
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000199 if (ev_code >= pmu->model->event_ranges[if_type].min &&
200 ev_code <= pmu->model->event_ranges[if_type].max)
201 return hw_event;
202
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100203 return -ENOENT;
204}
205
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000206static int probe_cci_revision(void)
207{
208 int rev;
209 rev = readl_relaxed(cci_ctrl_base + CCI_PID2) & CCI_PID2_REV_MASK;
210 rev >>= CCI_PID2_REV_SHIFT;
211
212 if (rev < CCI_REV_R1_PX)
213 return CCI_REV_R0;
214 else
215 return CCI_REV_R1;
216}
217
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000218static const struct cci_pmu_model *probe_cci_model(struct platform_device *pdev)
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000219{
Suzuki K. Poulose772742a2015-03-18 12:24:40 +0000220 if (platform_has_secure_cci_access())
221 return &cci_pmu_models[probe_cci_revision()];
222 return NULL;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000223}
224
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100225static int pmu_is_valid_counter(struct cci_pmu *cci_pmu, int idx)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100226{
227 return CCI_PMU_CYCLE_CNTR_IDX <= idx &&
228 idx <= CCI_PMU_CNTR_LAST(cci_pmu);
229}
230
231static u32 pmu_read_register(int idx, unsigned int offset)
232{
233 return readl_relaxed(pmu->base + CCI_PMU_CNTR_BASE(idx) + offset);
234}
235
236static void pmu_write_register(u32 value, int idx, unsigned int offset)
237{
238 return writel_relaxed(value, pmu->base + CCI_PMU_CNTR_BASE(idx) + offset);
239}
240
241static void pmu_disable_counter(int idx)
242{
243 pmu_write_register(0, idx, CCI_PMU_CNTR_CTRL);
244}
245
246static void pmu_enable_counter(int idx)
247{
248 pmu_write_register(1, idx, CCI_PMU_CNTR_CTRL);
249}
250
251static void pmu_set_event(int idx, unsigned long event)
252{
253 event &= CCI_PMU_EVENT_MASK;
254 pmu_write_register(event, idx, CCI_PMU_EVT_SEL);
255}
256
257static u32 pmu_get_max_counters(void)
258{
259 u32 n_cnts = (readl_relaxed(cci_ctrl_base + CCI_PMCR) &
260 CCI_PMCR_NCNT_MASK) >> CCI_PMCR_NCNT_SHIFT;
261
262 /* add 1 for cycle counter */
263 return n_cnts + 1;
264}
265
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100266static int pmu_get_event_idx(struct cci_pmu_hw_events *hw, struct perf_event *event)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100267{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100268 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100269 struct hw_perf_event *hw_event = &event->hw;
270 unsigned long cci_event = hw_event->config_base & CCI_PMU_EVENT_MASK;
271 int idx;
272
273 if (cci_event == CCI_PMU_CYCLES) {
274 if (test_and_set_bit(CCI_PMU_CYCLE_CNTR_IDX, hw->used_mask))
275 return -EAGAIN;
276
277 return CCI_PMU_CYCLE_CNTR_IDX;
278 }
279
280 for (idx = CCI_PMU_CNTR0_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); ++idx)
281 if (!test_and_set_bit(idx, hw->used_mask))
282 return idx;
283
284 /* No counters available */
285 return -EAGAIN;
286}
287
288static int pmu_map_event(struct perf_event *event)
289{
290 int mapping;
291 u8 config = event->attr.config & CCI_PMU_EVENT_MASK;
292
293 if (event->attr.type < PERF_TYPE_MAX)
294 return -ENOENT;
295
296 if (config == CCI_PMU_CYCLES)
297 mapping = config;
298 else
299 mapping = pmu_validate_hw_event(config);
300
301 return mapping;
302}
303
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100304static int pmu_request_irq(struct cci_pmu *cci_pmu, irq_handler_t handler)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100305{
306 int i;
307 struct platform_device *pmu_device = cci_pmu->plat_device;
308
309 if (unlikely(!pmu_device))
310 return -ENODEV;
311
312 if (pmu->nr_irqs < 1) {
313 dev_err(&pmu_device->dev, "no irqs for CCI PMUs defined\n");
314 return -ENODEV;
315 }
316
317 /*
318 * Register all available CCI PMU interrupts. In the interrupt handler
319 * we iterate over the counters checking for interrupt source (the
320 * overflowing counter) and clear it.
321 *
322 * This should allow handling of non-unique interrupt for the counters.
323 */
324 for (i = 0; i < pmu->nr_irqs; i++) {
325 int err = request_irq(pmu->irqs[i], handler, IRQF_SHARED,
326 "arm-cci-pmu", cci_pmu);
327 if (err) {
328 dev_err(&pmu_device->dev, "unable to request IRQ%d for ARM CCI PMU counters\n",
329 pmu->irqs[i]);
330 return err;
331 }
332
333 set_bit(i, &pmu->active_irqs);
334 }
335
336 return 0;
337}
338
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100339static void pmu_free_irq(struct cci_pmu *cci_pmu)
340{
341 int i;
342
343 for (i = 0; i < pmu->nr_irqs; i++) {
344 if (!test_and_clear_bit(i, &pmu->active_irqs))
345 continue;
346
347 free_irq(pmu->irqs[i], cci_pmu);
348 }
349}
350
351static u32 pmu_read_counter(struct perf_event *event)
352{
353 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
354 struct hw_perf_event *hw_counter = &event->hw;
355 int idx = hw_counter->idx;
356 u32 value;
357
358 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
359 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
360 return 0;
361 }
362 value = pmu_read_register(idx, CCI_PMU_CNTR);
363
364 return value;
365}
366
367static void pmu_write_counter(struct perf_event *event, u32 value)
368{
369 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
370 struct hw_perf_event *hw_counter = &event->hw;
371 int idx = hw_counter->idx;
372
373 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx)))
374 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
375 else
376 pmu_write_register(value, idx, CCI_PMU_CNTR);
377}
378
379static u64 pmu_event_update(struct perf_event *event)
380{
381 struct hw_perf_event *hwc = &event->hw;
382 u64 delta, prev_raw_count, new_raw_count;
383
384 do {
385 prev_raw_count = local64_read(&hwc->prev_count);
386 new_raw_count = pmu_read_counter(event);
387 } while (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
388 new_raw_count) != prev_raw_count);
389
390 delta = (new_raw_count - prev_raw_count) & CCI_PMU_CNTR_MASK;
391
392 local64_add(delta, &event->count);
393
394 return new_raw_count;
395}
396
397static void pmu_read(struct perf_event *event)
398{
399 pmu_event_update(event);
400}
401
402void pmu_event_set_period(struct perf_event *event)
403{
404 struct hw_perf_event *hwc = &event->hw;
405 /*
406 * The CCI PMU counters have a period of 2^32. To account for the
407 * possiblity of extreme interrupt latency we program for a period of
408 * half that. Hopefully we can handle the interrupt before another 2^31
409 * events occur and the counter overtakes its previous value.
410 */
411 u64 val = 1ULL << 31;
412 local64_set(&hwc->prev_count, val);
413 pmu_write_counter(event, val);
414}
415
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100416static irqreturn_t pmu_handle_irq(int irq_num, void *dev)
417{
418 unsigned long flags;
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100419 struct cci_pmu *cci_pmu = dev;
420 struct cci_pmu_hw_events *events = &pmu->hw_events;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100421 int idx, handled = IRQ_NONE;
422
423 raw_spin_lock_irqsave(&events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100424 /*
425 * Iterate over counters and update the corresponding perf events.
426 * This should work regardless of whether we have per-counter overflow
427 * interrupt or a combined overflow interrupt.
428 */
429 for (idx = CCI_PMU_CYCLE_CNTR_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++) {
430 struct perf_event *event = events->events[idx];
431 struct hw_perf_event *hw_counter;
432
433 if (!event)
434 continue;
435
436 hw_counter = &event->hw;
437
438 /* Did this counter overflow? */
Himangi Saraogifc5130d2014-07-30 11:37:35 +0100439 if (!(pmu_read_register(idx, CCI_PMU_OVRFLW) &
440 CCI_PMU_OVRFLW_FLAG))
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100441 continue;
442
443 pmu_write_register(CCI_PMU_OVRFLW_FLAG, idx, CCI_PMU_OVRFLW);
444
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100445 pmu_event_update(event);
446 pmu_event_set_period(event);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100447 handled = IRQ_HANDLED;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100448 }
449 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
450
451 return IRQ_RETVAL(handled);
452}
453
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100454static int cci_pmu_get_hw(struct cci_pmu *cci_pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100455{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100456 int ret = pmu_request_irq(cci_pmu, pmu_handle_irq);
457 if (ret) {
458 pmu_free_irq(cci_pmu);
459 return ret;
460 }
461 return 0;
462}
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100463
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100464static void cci_pmu_put_hw(struct cci_pmu *cci_pmu)
465{
466 pmu_free_irq(cci_pmu);
467}
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100468
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100469static void hw_perf_event_destroy(struct perf_event *event)
470{
471 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
472 atomic_t *active_events = &cci_pmu->active_events;
473 struct mutex *reserve_mutex = &cci_pmu->reserve_mutex;
474
475 if (atomic_dec_and_mutex_lock(active_events, reserve_mutex)) {
476 cci_pmu_put_hw(cci_pmu);
477 mutex_unlock(reserve_mutex);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100478 }
479}
480
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100481static void cci_pmu_enable(struct pmu *pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100482{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100483 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
484 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
485 int enabled = bitmap_weight(hw_events->used_mask, cci_pmu->num_events);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100486 unsigned long flags;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100487 u32 val;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100488
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100489 if (!enabled)
490 return;
491
492 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100493
494 /* Enable all the PMU counters. */
495 val = readl_relaxed(cci_ctrl_base + CCI_PMCR) | CCI_PMCR_CEN;
496 writel(val, cci_ctrl_base + CCI_PMCR);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100497 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100498
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100499}
500
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100501static void cci_pmu_disable(struct pmu *pmu)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100502{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100503 struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
504 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100505 unsigned long flags;
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100506 u32 val;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100507
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100508 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100509
510 /* Disable all the PMU counters. */
511 val = readl_relaxed(cci_ctrl_base + CCI_PMCR) & ~CCI_PMCR_CEN;
512 writel(val, cci_ctrl_base + CCI_PMCR);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100513 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100514}
515
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100516static void cci_pmu_start(struct perf_event *event, int pmu_flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100517{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100518 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
519 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
520 struct hw_perf_event *hwc = &event->hw;
521 int idx = hwc->idx;
522 unsigned long flags;
523
524 /*
525 * To handle interrupt latency, we always reprogram the period
526 * regardlesss of PERF_EF_RELOAD.
527 */
528 if (pmu_flags & PERF_EF_RELOAD)
529 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
530
531 hwc->state = 0;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100532
533 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
534 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100535 return;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100536 }
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100537
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100538 raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
539
540 /* Configure the event to count, unless you are counting cycles */
541 if (idx != CCI_PMU_CYCLE_CNTR_IDX)
542 pmu_set_event(idx, hwc->config_base);
543
544 pmu_event_set_period(event);
545 pmu_enable_counter(idx);
546
547 raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100548}
549
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100550static void cci_pmu_stop(struct perf_event *event, int pmu_flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100551{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100552 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
553 struct hw_perf_event *hwc = &event->hw;
554 int idx = hwc->idx;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100555
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100556 if (hwc->state & PERF_HES_STOPPED)
557 return;
558
559 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100560 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100561 return;
562 }
563
564 /*
565 * We always reprogram the counter, so ignore PERF_EF_UPDATE. See
566 * cci_pmu_start()
567 */
568 pmu_disable_counter(idx);
569 pmu_event_update(event);
570 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100571}
572
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100573static int cci_pmu_add(struct perf_event *event, int flags)
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100574{
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100575 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
576 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
577 struct hw_perf_event *hwc = &event->hw;
578 int idx;
579 int err = 0;
580
581 perf_pmu_disable(event->pmu);
582
583 /* If we don't have a space for the counter then finish early. */
584 idx = pmu_get_event_idx(hw_events, event);
585 if (idx < 0) {
586 err = idx;
587 goto out;
588 }
589
590 event->hw.idx = idx;
591 hw_events->events[idx] = event;
592
593 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
594 if (flags & PERF_EF_START)
595 cci_pmu_start(event, PERF_EF_RELOAD);
596
597 /* Propagate our changes to the userspace mapping. */
598 perf_event_update_userpage(event);
599
600out:
601 perf_pmu_enable(event->pmu);
602 return err;
603}
604
605static void cci_pmu_del(struct perf_event *event, int flags)
606{
607 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
608 struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
609 struct hw_perf_event *hwc = &event->hw;
610 int idx = hwc->idx;
611
612 cci_pmu_stop(event, PERF_EF_UPDATE);
613 hw_events->events[idx] = NULL;
614 clear_bit(idx, hw_events->used_mask);
615
616 perf_event_update_userpage(event);
617}
618
619static int
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +0000620validate_event(struct pmu *cci_pmu,
621 struct cci_pmu_hw_events *hw_events,
622 struct perf_event *event)
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100623{
624 if (is_software_event(event))
625 return 1;
626
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +0000627 /*
628 * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
629 * core perf code won't check that the pmu->ctx == leader->ctx
630 * until after pmu->event_init(event).
631 */
632 if (event->pmu != cci_pmu)
633 return 0;
634
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100635 if (event->state < PERF_EVENT_STATE_OFF)
636 return 1;
637
638 if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
639 return 1;
640
641 return pmu_get_event_idx(hw_events, event) >= 0;
642}
643
644static int
645validate_group(struct perf_event *event)
646{
647 struct perf_event *sibling, *leader = event->group_leader;
648 struct cci_pmu_hw_events fake_pmu = {
649 /*
650 * Initialise the fake PMU. We only need to populate the
651 * used_mask for the purposes of validation.
652 */
653 .used_mask = CPU_BITS_NONE,
654 };
655
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +0000656 if (!validate_event(event->pmu, &fake_pmu, leader))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100657 return -EINVAL;
658
659 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +0000660 if (!validate_event(event->pmu, &fake_pmu, sibling))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100661 return -EINVAL;
662 }
663
Suzuki K. Pouloseb1862192015-03-17 18:15:00 +0000664 if (!validate_event(event->pmu, &fake_pmu, event))
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100665 return -EINVAL;
666
667 return 0;
668}
669
670static int
671__hw_perf_event_init(struct perf_event *event)
672{
673 struct hw_perf_event *hwc = &event->hw;
674 int mapping;
675
676 mapping = pmu_map_event(event);
677
678 if (mapping < 0) {
679 pr_debug("event %x:%llx not supported\n", event->attr.type,
680 event->attr.config);
681 return mapping;
682 }
683
684 /*
685 * We don't assign an index until we actually place the event onto
686 * hardware. Use -1 to signify that we haven't decided where to put it
687 * yet.
688 */
689 hwc->idx = -1;
690 hwc->config_base = 0;
691 hwc->config = 0;
692 hwc->event_base = 0;
693
694 /*
695 * Store the event encoding into the config_base field.
696 */
697 hwc->config_base |= (unsigned long)mapping;
698
699 /*
700 * Limit the sample_period to half of the counter width. That way, the
701 * new counter value is far less likely to overtake the previous one
702 * unless you have some serious IRQ latency issues.
703 */
704 hwc->sample_period = CCI_PMU_CNTR_MASK >> 1;
705 hwc->last_period = hwc->sample_period;
706 local64_set(&hwc->period_left, hwc->sample_period);
707
708 if (event->group_leader != event) {
709 if (validate_group(event) != 0)
710 return -EINVAL;
711 }
712
713 return 0;
714}
715
716static int cci_pmu_event_init(struct perf_event *event)
717{
718 struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
719 atomic_t *active_events = &cci_pmu->active_events;
720 int err = 0;
721 int cpu;
722
723 if (event->attr.type != event->pmu->type)
724 return -ENOENT;
725
726 /* Shared by all CPUs, no meaningful state to sample */
727 if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
728 return -EOPNOTSUPP;
729
730 /* We have no filtering of any kind */
731 if (event->attr.exclude_user ||
732 event->attr.exclude_kernel ||
733 event->attr.exclude_hv ||
734 event->attr.exclude_idle ||
735 event->attr.exclude_host ||
736 event->attr.exclude_guest)
737 return -EINVAL;
738
739 /*
740 * Following the example set by other "uncore" PMUs, we accept any CPU
741 * and rewrite its affinity dynamically rather than having perf core
742 * handle cpu == -1 and pid == -1 for this case.
743 *
744 * The perf core will pin online CPUs for the duration of this call and
745 * the event being installed into its context, so the PMU's CPU can't
746 * change under our feet.
747 */
748 cpu = cpumask_first(&cci_pmu->cpus);
749 if (event->cpu < 0 || cpu < 0)
750 return -EINVAL;
751 event->cpu = cpu;
752
753 event->destroy = hw_perf_event_destroy;
754 if (!atomic_inc_not_zero(active_events)) {
755 mutex_lock(&cci_pmu->reserve_mutex);
756 if (atomic_read(active_events) == 0)
757 err = cci_pmu_get_hw(cci_pmu);
758 if (!err)
759 atomic_inc(active_events);
760 mutex_unlock(&cci_pmu->reserve_mutex);
761 }
762 if (err)
763 return err;
764
765 err = __hw_perf_event_init(event);
766 if (err)
767 hw_perf_event_destroy(event);
768
769 return err;
770}
771
772static ssize_t pmu_attr_cpumask_show(struct device *dev,
773 struct device_attribute *attr, char *buf)
774{
Tejun Heo660e5ec2015-02-13 14:37:20 -0800775 int n = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
776 cpumask_pr_args(&pmu->cpus));
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100777 buf[n++] = '\n';
778 buf[n] = '\0';
779 return n;
780}
781
782static DEVICE_ATTR(cpumask, S_IRUGO, pmu_attr_cpumask_show, NULL);
783
784static struct attribute *pmu_attrs[] = {
785 &dev_attr_cpumask.attr,
786 NULL,
787};
788
789static struct attribute_group pmu_attr_group = {
790 .attrs = pmu_attrs,
791};
792
793static const struct attribute_group *pmu_attr_groups[] = {
794 &pmu_attr_group,
795 NULL
796};
797
798static int cci_pmu_init(struct cci_pmu *cci_pmu, struct platform_device *pdev)
799{
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000800 char *name = cci_pmu->model->name;
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100801 cci_pmu->pmu = (struct pmu) {
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000802 .name = cci_pmu->model->name,
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100803 .task_ctx_nr = perf_invalid_context,
804 .pmu_enable = cci_pmu_enable,
805 .pmu_disable = cci_pmu_disable,
806 .event_init = cci_pmu_event_init,
807 .add = cci_pmu_add,
808 .del = cci_pmu_del,
809 .start = cci_pmu_start,
810 .stop = cci_pmu_stop,
811 .read = pmu_read,
812 .attr_groups = pmu_attr_groups,
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100813 };
814
815 cci_pmu->plat_device = pdev;
816 cci_pmu->num_events = pmu_get_max_counters();
817
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100818 return perf_pmu_register(&cci_pmu->pmu, name, -1);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100819}
820
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100821static int cci_pmu_cpu_notifier(struct notifier_block *self,
822 unsigned long action, void *hcpu)
823{
824 unsigned int cpu = (long)hcpu;
825 unsigned int target;
826
827 switch (action & ~CPU_TASKS_FROZEN) {
828 case CPU_DOWN_PREPARE:
829 if (!cpumask_test_and_clear_cpu(cpu, &pmu->cpus))
830 break;
831 target = cpumask_any_but(cpu_online_mask, cpu);
832 if (target < 0) // UP, last CPU
833 break;
834 /*
835 * TODO: migrate context once core races on event->ctx have
836 * been fixed.
837 */
838 cpumask_set_cpu(target, &pmu->cpus);
839 default:
840 break;
841 }
842
843 return NOTIFY_OK;
844}
845
846static struct notifier_block cci_pmu_cpu_nb = {
847 .notifier_call = cci_pmu_cpu_notifier,
848 /*
849 * to migrate uncore events, our notifier should be executed
850 * before perf core's notifier.
851 */
852 .priority = CPU_PRI_PERF + 1,
853};
854
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000855static struct cci_pmu_model cci_pmu_models[] = {
856 [CCI_REV_R0] = {
857 .name = "CCI_400",
858 .event_ranges = {
859 [CCI_IF_SLAVE] = {
860 CCI_REV_R0_SLAVE_PORT_MIN_EV,
861 CCI_REV_R0_SLAVE_PORT_MAX_EV,
862 },
863 [CCI_IF_MASTER] = {
864 CCI_REV_R0_MASTER_PORT_MIN_EV,
865 CCI_REV_R0_MASTER_PORT_MAX_EV,
866 },
867 },
868 },
869 [CCI_REV_R1] = {
870 .name = "CCI_400_r1",
871 .event_ranges = {
872 [CCI_IF_SLAVE] = {
873 CCI_REV_R1_SLAVE_PORT_MIN_EV,
874 CCI_REV_R1_SLAVE_PORT_MAX_EV,
875 },
876 [CCI_IF_MASTER] = {
877 CCI_REV_R1_MASTER_PORT_MIN_EV,
878 CCI_REV_R1_MASTER_PORT_MAX_EV,
879 },
880 },
881 },
882};
883
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100884static const struct of_device_id arm_cci_pmu_matches[] = {
885 {
886 .compatible = "arm,cci-400-pmu",
Suzuki K. Poulose772742a2015-03-18 12:24:40 +0000887 .data = NULL,
888 },
889 {
890 .compatible = "arm,cci-400-pmu,r0",
891 .data = &cci_pmu_models[CCI_REV_R0],
892 },
893 {
894 .compatible = "arm,cci-400-pmu,r1",
895 .data = &cci_pmu_models[CCI_REV_R1],
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100896 },
897 {},
898};
899
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000900static inline const struct cci_pmu_model *get_cci_model(struct platform_device *pdev)
901{
902 const struct of_device_id *match = of_match_node(arm_cci_pmu_matches,
903 pdev->dev.of_node);
904 if (!match)
905 return NULL;
Suzuki K. Poulose772742a2015-03-18 12:24:40 +0000906 if (match->data)
907 return match->data;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000908
Suzuki K. Poulose772742a2015-03-18 12:24:40 +0000909 dev_warn(&pdev->dev, "DEPRECATED compatible property,"
910 "requires secure access to CCI registers");
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000911 return probe_cci_model(pdev);
912}
913
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000914static bool is_duplicate_irq(int irq, int *irqs, int nr_irqs)
915{
916 int i;
917
918 for (i = 0; i < nr_irqs; i++)
919 if (irq == irqs[i])
920 return true;
921
922 return false;
923}
924
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100925static int cci_pmu_probe(struct platform_device *pdev)
926{
927 struct resource *res;
928 int i, ret, irq;
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000929 const struct cci_pmu_model *model;
930
931 model = get_cci_model(pdev);
932 if (!model) {
933 dev_warn(&pdev->dev, "CCI PMU version not supported\n");
934 return -ENODEV;
935 }
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100936
937 pmu = devm_kzalloc(&pdev->dev, sizeof(*pmu), GFP_KERNEL);
938 if (!pmu)
939 return -ENOMEM;
940
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000941 pmu->model = model;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100942 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100943 pmu->base = devm_ioremap_resource(&pdev->dev, res);
Wei Yongjunfee4f2c2013-09-22 06:04:23 +0100944 if (IS_ERR(pmu->base))
945 return -ENOMEM;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100946
947 /*
948 * CCI PMU has 5 overflow signals - one per counter; but some may be tied
949 * together to a common interrupt.
950 */
951 pmu->nr_irqs = 0;
952 for (i = 0; i < CCI_PMU_MAX_HW_EVENTS; i++) {
953 irq = platform_get_irq(pdev, i);
954 if (irq < 0)
955 break;
956
957 if (is_duplicate_irq(irq, pmu->irqs, pmu->nr_irqs))
958 continue;
959
960 pmu->irqs[pmu->nr_irqs++] = irq;
961 }
962
963 /*
964 * Ensure that the device tree has as many interrupts as the number
965 * of counters.
966 */
967 if (i < CCI_PMU_MAX_HW_EVENTS) {
968 dev_warn(&pdev->dev, "In-correct number of interrupts: %d, should be %d\n",
969 i, CCI_PMU_MAX_HW_EVENTS);
Wei Yongjunfee4f2c2013-09-22 06:04:23 +0100970 return -EINVAL;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100971 }
972
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100973 raw_spin_lock_init(&pmu->hw_events.pmu_lock);
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100974 mutex_init(&pmu->reserve_mutex);
975 atomic_set(&pmu->active_events, 0);
976 cpumask_set_cpu(smp_processor_id(), &pmu->cpus);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100977
Mark Rutlandc6f85cb2014-06-30 12:20:21 +0100978 ret = register_cpu_notifier(&cci_pmu_cpu_nb);
979 if (ret)
980 return ret;
981
982 ret = cci_pmu_init(pmu, pdev);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100983 if (ret)
Wei Yongjunfee4f2c2013-09-22 06:04:23 +0100984 return ret;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100985
Suzuki K. Poulosefc17c832015-03-18 12:24:39 +0000986 pr_info("ARM %s PMU driver probed", pmu->model->name);
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100987 return 0;
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100988}
989
990static int cci_platform_probe(struct platform_device *pdev)
991{
992 if (!cci_probed())
993 return -ENODEV;
994
995 return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
996}
997
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +0000998static struct platform_driver cci_pmu_driver = {
999 .driver = {
1000 .name = DRIVER_NAME_PMU,
1001 .of_match_table = arm_cci_pmu_matches,
1002 },
1003 .probe = cci_pmu_probe,
1004};
1005
1006static struct platform_driver cci_platform_driver = {
1007 .driver = {
1008 .name = DRIVER_NAME,
1009 .of_match_table = arm_cci_matches,
1010 },
1011 .probe = cci_platform_probe,
1012};
1013
1014static int __init cci_platform_init(void)
1015{
1016 int ret;
1017
1018 ret = platform_driver_register(&cci_pmu_driver);
1019 if (ret)
1020 return ret;
1021
1022 return platform_driver_register(&cci_platform_driver);
1023}
1024
1025#else /* !CONFIG_HW_PERF_EVENTS */
1026
1027static int __init cci_platform_init(void)
1028{
1029 return 0;
1030}
1031
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001032#endif /* CONFIG_HW_PERF_EVENTS */
1033
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001034#define CCI_PORT_CTRL 0x0
1035#define CCI_CTRL_STATUS 0xc
1036
1037#define CCI_ENABLE_SNOOP_REQ 0x1
1038#define CCI_ENABLE_DVM_REQ 0x2
1039#define CCI_ENABLE_REQ (CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ)
1040
1041enum cci_ace_port_type {
1042 ACE_INVALID_PORT = 0x0,
1043 ACE_PORT,
1044 ACE_LITE_PORT,
1045};
1046
1047struct cci_ace_port {
1048 void __iomem *base;
1049 unsigned long phys;
1050 enum cci_ace_port_type type;
1051 struct device_node *dn;
1052};
1053
1054static struct cci_ace_port *ports;
1055static unsigned int nb_cci_ports;
1056
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001057struct cpu_port {
1058 u64 mpidr;
1059 u32 port;
1060};
Nicolas Pitre62158f82013-05-21 23:34:41 -04001061
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001062/*
1063 * Use the port MSB as valid flag, shift can be made dynamic
1064 * by computing number of bits required for port indexes.
1065 * Code disabling CCI cpu ports runs with D-cache invalidated
1066 * and SCTLR bit clear so data accesses must be kept to a minimum
1067 * to improve performance; for now shift is left static to
1068 * avoid one more data access while disabling the CCI port.
1069 */
1070#define PORT_VALID_SHIFT 31
1071#define PORT_VALID (0x1 << PORT_VALID_SHIFT)
1072
1073static inline void init_cpu_port(struct cpu_port *port, u32 index, u64 mpidr)
1074{
1075 port->port = PORT_VALID | index;
1076 port->mpidr = mpidr;
1077}
1078
1079static inline bool cpu_port_is_valid(struct cpu_port *port)
1080{
1081 return !!(port->port & PORT_VALID);
1082}
1083
1084static inline bool cpu_port_match(struct cpu_port *port, u64 mpidr)
1085{
1086 return port->mpidr == (mpidr & MPIDR_HWID_BITMASK);
1087}
1088
1089static struct cpu_port cpu_port[NR_CPUS];
1090
1091/**
1092 * __cci_ace_get_port - Function to retrieve the port index connected to
1093 * a cpu or device.
1094 *
1095 * @dn: device node of the device to look-up
1096 * @type: port type
1097 *
1098 * Return value:
1099 * - CCI port index if success
1100 * - -ENODEV if failure
1101 */
1102static int __cci_ace_get_port(struct device_node *dn, int type)
1103{
1104 int i;
1105 bool ace_match;
1106 struct device_node *cci_portn;
1107
1108 cci_portn = of_parse_phandle(dn, "cci-control-port", 0);
1109 for (i = 0; i < nb_cci_ports; i++) {
1110 ace_match = ports[i].type == type;
1111 if (ace_match && cci_portn == ports[i].dn)
1112 return i;
1113 }
1114 return -ENODEV;
1115}
1116
1117int cci_ace_get_port(struct device_node *dn)
1118{
1119 return __cci_ace_get_port(dn, ACE_LITE_PORT);
1120}
1121EXPORT_SYMBOL_GPL(cci_ace_get_port);
1122
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001123static void cci_ace_init_ports(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001124{
Sudeep KarkadaNagesha78b4d6e2013-06-17 14:51:48 +01001125 int port, cpu;
1126 struct device_node *cpun;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001127
1128 /*
1129 * Port index look-up speeds up the function disabling ports by CPU,
1130 * since the logical to port index mapping is done once and does
1131 * not change after system boot.
1132 * The stashed index array is initialized for all possible CPUs
1133 * at probe time.
1134 */
Sudeep KarkadaNagesha78b4d6e2013-06-17 14:51:48 +01001135 for_each_possible_cpu(cpu) {
1136 /* too early to use cpu->of_node */
1137 cpun = of_get_cpu_node(cpu, NULL);
1138
1139 if (WARN(!cpun, "Missing cpu device node\n"))
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001140 continue;
1141
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001142 port = __cci_ace_get_port(cpun, ACE_PORT);
1143 if (port < 0)
1144 continue;
1145
1146 init_cpu_port(&cpu_port[cpu], port, cpu_logical_map(cpu));
1147 }
1148
1149 for_each_possible_cpu(cpu) {
1150 WARN(!cpu_port_is_valid(&cpu_port[cpu]),
1151 "CPU %u does not have an associated CCI port\n",
1152 cpu);
1153 }
1154}
1155/*
1156 * Functions to enable/disable a CCI interconnect slave port
1157 *
1158 * They are called by low-level power management code to disable slave
1159 * interfaces snoops and DVM broadcast.
1160 * Since they may execute with cache data allocation disabled and
1161 * after the caches have been cleaned and invalidated the functions provide
1162 * no explicit locking since they may run with D-cache disabled, so normal
1163 * cacheable kernel locks based on ldrex/strex may not work.
1164 * Locking has to be provided by BSP implementations to ensure proper
1165 * operations.
1166 */
1167
1168/**
1169 * cci_port_control() - function to control a CCI port
1170 *
1171 * @port: index of the port to setup
1172 * @enable: if true enables the port, if false disables it
1173 */
1174static void notrace cci_port_control(unsigned int port, bool enable)
1175{
1176 void __iomem *base = ports[port].base;
1177
1178 writel_relaxed(enable ? CCI_ENABLE_REQ : 0, base + CCI_PORT_CTRL);
1179 /*
1180 * This function is called from power down procedures
1181 * and must not execute any instruction that might
1182 * cause the processor to be put in a quiescent state
1183 * (eg wfi). Hence, cpu_relax() can not be added to this
1184 * read loop to optimize power, since it might hide possibly
1185 * disruptive operations.
1186 */
1187 while (readl_relaxed(cci_ctrl_base + CCI_CTRL_STATUS) & 0x1)
1188 ;
1189}
1190
1191/**
1192 * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
1193 * reference
1194 *
1195 * @mpidr: mpidr of the CPU whose CCI port should be disabled
1196 *
1197 * Disabling a CCI port for a CPU implies disabling the CCI port
1198 * controlling that CPU cluster. Code disabling CPU CCI ports
1199 * must make sure that the CPU running the code is the last active CPU
1200 * in the cluster ie all other CPUs are quiescent in a low power state.
1201 *
1202 * Return:
1203 * 0 on success
1204 * -ENODEV on port look-up failure
1205 */
1206int notrace cci_disable_port_by_cpu(u64 mpidr)
1207{
1208 int cpu;
1209 bool is_valid;
1210 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
1211 is_valid = cpu_port_is_valid(&cpu_port[cpu]);
1212 if (is_valid && cpu_port_match(&cpu_port[cpu], mpidr)) {
1213 cci_port_control(cpu_port[cpu].port, false);
1214 return 0;
1215 }
1216 }
1217 return -ENODEV;
1218}
1219EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu);
1220
1221/**
Nicolas Pitre62158f82013-05-21 23:34:41 -04001222 * cci_enable_port_for_self() - enable a CCI port for calling CPU
1223 *
1224 * Enabling a CCI port for the calling CPU implies enabling the CCI
1225 * port controlling that CPU's cluster. Caller must make sure that the
1226 * CPU running the code is the first active CPU in the cluster and all
1227 * other CPUs are quiescent in a low power state or waiting for this CPU
1228 * to complete the CCI initialization.
1229 *
1230 * Because this is called when the MMU is still off and with no stack,
1231 * the code must be position independent and ideally rely on callee
1232 * clobbered registers only. To achieve this we must code this function
1233 * entirely in assembler.
1234 *
1235 * On success this returns with the proper CCI port enabled. In case of
1236 * any failure this never returns as the inability to enable the CCI is
1237 * fatal and there is no possible recovery at this stage.
1238 */
1239asmlinkage void __naked cci_enable_port_for_self(void)
1240{
1241 asm volatile ("\n"
Arnd Bergmannf4902492013-06-03 15:15:36 +02001242" .arch armv7-a\n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04001243" mrc p15, 0, r0, c0, c0, 5 @ get MPIDR value \n"
1244" and r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n"
1245" adr r1, 5f \n"
1246" ldr r2, [r1] \n"
1247" add r1, r1, r2 @ &cpu_port \n"
1248" add ip, r1, %[sizeof_cpu_port] \n"
1249
1250 /* Loop over the cpu_port array looking for a matching MPIDR */
1251"1: ldr r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
1252" cmp r2, r0 @ compare MPIDR \n"
1253" bne 2f \n"
1254
1255 /* Found a match, now test port validity */
1256" ldr r3, [r1, %[offsetof_cpu_port_port]] \n"
1257" tst r3, #"__stringify(PORT_VALID)" \n"
1258" bne 3f \n"
1259
1260 /* no match, loop with the next cpu_port entry */
1261"2: add r1, r1, %[sizeof_struct_cpu_port] \n"
1262" cmp r1, ip @ done? \n"
1263" blo 1b \n"
1264
1265 /* CCI port not found -- cheaply try to stall this CPU */
1266"cci_port_not_found: \n"
1267" wfi \n"
1268" wfe \n"
1269" b cci_port_not_found \n"
1270
1271 /* Use matched port index to look up the corresponding ports entry */
1272"3: bic r3, r3, #"__stringify(PORT_VALID)" \n"
1273" adr r0, 6f \n"
1274" ldmia r0, {r1, r2} \n"
1275" sub r1, r1, r0 @ virt - phys \n"
1276" ldr r0, [r0, r2] @ *(&ports) \n"
1277" mov r2, %[sizeof_struct_ace_port] \n"
1278" mla r0, r2, r3, r0 @ &ports[index] \n"
1279" sub r0, r0, r1 @ virt_to_phys() \n"
1280
1281 /* Enable the CCI port */
1282" ldr r0, [r0, %[offsetof_port_phys]] \n"
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07001283" mov r3, %[cci_enable_req]\n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04001284" str r3, [r0, #"__stringify(CCI_PORT_CTRL)"] \n"
1285
1286 /* poll the status reg for completion */
1287" adr r1, 7f \n"
1288" ldr r0, [r1] \n"
1289" ldr r0, [r0, r1] @ cci_ctrl_base \n"
1290"4: ldr r1, [r0, #"__stringify(CCI_CTRL_STATUS)"] \n"
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07001291" tst r1, %[cci_control_status_bits] \n"
Nicolas Pitre62158f82013-05-21 23:34:41 -04001292" bne 4b \n"
1293
1294" mov r0, #0 \n"
1295" bx lr \n"
1296
1297" .align 2 \n"
1298"5: .word cpu_port - . \n"
1299"6: .word . \n"
1300" .word ports - 6b \n"
1301"7: .word cci_ctrl_phys - . \n"
1302 : :
1303 [sizeof_cpu_port] "i" (sizeof(cpu_port)),
Victor Kamenskyfdb07ae2013-10-15 21:50:34 -07001304 [cci_enable_req] "i" cpu_to_le32(CCI_ENABLE_REQ),
1305 [cci_control_status_bits] "i" cpu_to_le32(1),
Nicolas Pitre62158f82013-05-21 23:34:41 -04001306#ifndef __ARMEB__
1307 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
1308#else
1309 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
1310#endif
1311 [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
1312 [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
1313 [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
1314 [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
1315
1316 unreachable();
1317}
1318
1319/**
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001320 * __cci_control_port_by_device() - function to control a CCI port by device
1321 * reference
1322 *
1323 * @dn: device node pointer of the device whose CCI port should be
1324 * controlled
1325 * @enable: if true enables the port, if false disables it
1326 *
1327 * Return:
1328 * 0 on success
1329 * -ENODEV on port look-up failure
1330 */
1331int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
1332{
1333 int port;
1334
1335 if (!dn)
1336 return -ENODEV;
1337
1338 port = __cci_ace_get_port(dn, ACE_LITE_PORT);
1339 if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n",
1340 dn->full_name))
1341 return -ENODEV;
1342 cci_port_control(port, enable);
1343 return 0;
1344}
1345EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
1346
1347/**
1348 * __cci_control_port_by_index() - function to control a CCI port by port index
1349 *
1350 * @port: port index previously retrieved with cci_ace_get_port()
1351 * @enable: if true enables the port, if false disables it
1352 *
1353 * Return:
1354 * 0 on success
1355 * -ENODEV on port index out of range
1356 * -EPERM if operation carried out on an ACE PORT
1357 */
1358int notrace __cci_control_port_by_index(u32 port, bool enable)
1359{
1360 if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
1361 return -ENODEV;
1362 /*
1363 * CCI control for ports connected to CPUS is extremely fragile
1364 * and must be made to go through a specific and controlled
1365 * interface (ie cci_disable_port_by_cpu(); control by general purpose
1366 * indexing is therefore disabled for ACE ports.
1367 */
1368 if (ports[port].type == ACE_PORT)
1369 return -EPERM;
1370
1371 cci_port_control(port, enable);
1372 return 0;
1373}
1374EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
1375
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001376static const struct of_device_id arm_cci_ctrl_if_matches[] = {
1377 {.compatible = "arm,cci-400-ctrl-if", },
1378 {},
1379};
1380
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001381static int cci_probe_ports(struct device_node *np)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001382{
1383 struct cci_nb_ports const *cci_config;
1384 int ret, i, nb_ace = 0, nb_ace_lite = 0;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001385 struct device_node *cp;
Nicolas Pitre62158f82013-05-21 23:34:41 -04001386 struct resource res;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001387 const char *match_str;
1388 bool is_ace;
1389
Abhilash Kesavan896ddd62015-01-10 08:41:35 +05301390
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001391 cci_config = of_match_node(arm_cci_matches, np)->data;
1392 if (!cci_config)
1393 return -ENODEV;
1394
1395 nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
1396
Lorenzo Pieralisi7c762032014-01-27 10:50:37 +00001397 ports = kcalloc(nb_cci_ports, sizeof(*ports), GFP_KERNEL);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001398 if (!ports)
1399 return -ENOMEM;
1400
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001401 for_each_child_of_node(np, cp) {
1402 if (!of_match_node(arm_cci_ctrl_if_matches, cp))
1403 continue;
1404
1405 i = nb_ace + nb_ace_lite;
1406
1407 if (i >= nb_cci_ports)
1408 break;
1409
1410 if (of_property_read_string(cp, "interface-type",
1411 &match_str)) {
1412 WARN(1, "node %s missing interface-type property\n",
1413 cp->full_name);
1414 continue;
1415 }
1416 is_ace = strcmp(match_str, "ace") == 0;
1417 if (!is_ace && strcmp(match_str, "ace-lite")) {
1418 WARN(1, "node %s containing invalid interface-type property, skipping it\n",
1419 cp->full_name);
1420 continue;
1421 }
1422
Nicolas Pitre62158f82013-05-21 23:34:41 -04001423 ret = of_address_to_resource(cp, 0, &res);
1424 if (!ret) {
1425 ports[i].base = ioremap(res.start, resource_size(&res));
1426 ports[i].phys = res.start;
1427 }
1428 if (ret || !ports[i].base) {
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001429 WARN(1, "unable to ioremap CCI port %d\n", i);
1430 continue;
1431 }
1432
1433 if (is_ace) {
1434 if (WARN_ON(nb_ace >= cci_config->nb_ace))
1435 continue;
1436 ports[i].type = ACE_PORT;
1437 ++nb_ace;
1438 } else {
1439 if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
1440 continue;
1441 ports[i].type = ACE_LITE_PORT;
1442 ++nb_ace_lite;
1443 }
1444 ports[i].dn = cp;
1445 }
1446
1447 /* initialize a stashed array of ACE ports to speed-up look-up */
1448 cci_ace_init_ports();
1449
1450 /*
1451 * Multi-cluster systems may need this data when non-coherent, during
1452 * cluster power-up/power-down. Make sure it reaches main memory.
1453 */
1454 sync_cache_w(&cci_ctrl_base);
Nicolas Pitre62158f82013-05-21 23:34:41 -04001455 sync_cache_w(&cci_ctrl_phys);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001456 sync_cache_w(&ports);
1457 sync_cache_w(&cpu_port);
1458 __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
1459 pr_info("ARM CCI driver probed\n");
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001460
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001461 return 0;
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001462}
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001463
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001464static int cci_probe(void)
1465{
1466 int ret;
1467 struct device_node *np;
1468 struct resource res;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001469
Suzuki K. Poulosef6b9e832015-03-18 12:24:38 +00001470 np = of_find_matching_node(NULL, arm_cci_matches);
1471 if(!np || !of_device_is_available(np))
1472 return -ENODEV;
1473
1474 ret = of_address_to_resource(np, 0, &res);
1475 if (!ret) {
1476 cci_ctrl_base = ioremap(res.start, resource_size(&res));
1477 cci_ctrl_phys = res.start;
1478 }
1479 if (ret || !cci_ctrl_base) {
1480 WARN(1, "unable to ioremap CCI ctrl\n");
1481 return -ENXIO;
1482 }
1483
1484 return cci_probe_ports(np);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001485}
1486
1487static int cci_init_status = -EAGAIN;
1488static DEFINE_MUTEX(cci_probing);
1489
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001490static int cci_init(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001491{
1492 if (cci_init_status != -EAGAIN)
1493 return cci_init_status;
1494
1495 mutex_lock(&cci_probing);
1496 if (cci_init_status == -EAGAIN)
1497 cci_init_status = cci_probe();
1498 mutex_unlock(&cci_probing);
1499 return cci_init_status;
1500}
1501
1502/*
1503 * To sort out early init calls ordering a helper function is provided to
1504 * check if the CCI driver has beed initialized. Function check if the driver
1505 * has been initialized, if not it calls the init function that probes
1506 * the driver and updates the return value.
1507 */
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001508bool cci_probed(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001509{
1510 return cci_init() == 0;
1511}
1512EXPORT_SYMBOL_GPL(cci_probed);
1513
1514early_initcall(cci_init);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001515core_initcall(cci_platform_init);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001516MODULE_LICENSE("GPL");
1517MODULE_DESCRIPTION("ARM CCI support");