blob: dc6528e8b8fb620665a76ef6cac1654e746f1843 [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>
19#include <linux/module.h>
20#include <linux/of_address.h>
Punit Agrawalb91c8f22013-08-22 14:41:51 +010021#include <linux/of_irq.h>
22#include <linux/of_platform.h>
23#include <linux/platform_device.h>
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010024#include <linux/slab.h>
Punit Agrawalb91c8f22013-08-22 14:41:51 +010025#include <linux/spinlock.h>
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010026
27#include <asm/cacheflush.h>
Punit Agrawalb91c8f22013-08-22 14:41:51 +010028#include <asm/irq_regs.h>
29#include <asm/pmu.h>
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010030#include <asm/smp_plat.h>
31
Punit Agrawalb91c8f22013-08-22 14:41:51 +010032#define DRIVER_NAME "CCI-400"
33#define DRIVER_NAME_PMU DRIVER_NAME " PMU"
34#define PMU_NAME "CCI_400"
35
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010036#define CCI_PORT_CTRL 0x0
37#define CCI_CTRL_STATUS 0xc
38
39#define CCI_ENABLE_SNOOP_REQ 0x1
40#define CCI_ENABLE_DVM_REQ 0x2
41#define CCI_ENABLE_REQ (CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ)
42
43struct cci_nb_ports {
44 unsigned int nb_ace;
45 unsigned int nb_ace_lite;
46};
47
48enum cci_ace_port_type {
49 ACE_INVALID_PORT = 0x0,
50 ACE_PORT,
51 ACE_LITE_PORT,
52};
53
54struct cci_ace_port {
55 void __iomem *base;
Nicolas Pitre62158f82013-05-21 23:34:41 -040056 unsigned long phys;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010057 enum cci_ace_port_type type;
58 struct device_node *dn;
59};
60
61static struct cci_ace_port *ports;
62static unsigned int nb_cci_ports;
63
64static void __iomem *cci_ctrl_base;
Nicolas Pitre62158f82013-05-21 23:34:41 -040065static unsigned long cci_ctrl_phys;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +010066
Punit Agrawalb91c8f22013-08-22 14:41:51 +010067#ifdef CONFIG_HW_PERF_EVENTS
68
69#define CCI_PMCR 0x0100
70#define CCI_PID2 0x0fe8
71
72#define CCI_PMCR_CEN 0x00000001
73#define CCI_PMCR_NCNT_MASK 0x0000f800
74#define CCI_PMCR_NCNT_SHIFT 11
75
76#define CCI_PID2_REV_MASK 0xf0
77#define CCI_PID2_REV_SHIFT 4
78
79/* Port ids */
80#define CCI_PORT_S0 0
81#define CCI_PORT_S1 1
82#define CCI_PORT_S2 2
83#define CCI_PORT_S3 3
84#define CCI_PORT_S4 4
85#define CCI_PORT_M0 5
86#define CCI_PORT_M1 6
87#define CCI_PORT_M2 7
88
89#define CCI_REV_R0 0
90#define CCI_REV_R1 1
91#define CCI_REV_R0_P4 4
92#define CCI_REV_R1_P2 6
93
94#define CCI_PMU_EVT_SEL 0x000
95#define CCI_PMU_CNTR 0x004
96#define CCI_PMU_CNTR_CTRL 0x008
97#define CCI_PMU_OVRFLW 0x00c
98
99#define CCI_PMU_OVRFLW_FLAG 1
100
101#define CCI_PMU_CNTR_BASE(idx) ((idx) * SZ_4K)
102
103/*
104 * Instead of an event id to monitor CCI cycles, a dedicated counter is
105 * provided. Use 0xff to represent CCI cycles and hope that no future revisions
106 * make use of this event in hardware.
107 */
108enum cci400_perf_events {
109 CCI_PMU_CYCLES = 0xff
110};
111
112#define CCI_PMU_EVENT_MASK 0xff
113#define CCI_PMU_EVENT_SOURCE(event) ((event >> 5) & 0x7)
114#define CCI_PMU_EVENT_CODE(event) (event & 0x1f)
115
116#define CCI_PMU_MAX_HW_EVENTS 5 /* CCI PMU has 4 counters + 1 cycle counter */
117
118#define CCI_PMU_CYCLE_CNTR_IDX 0
119#define CCI_PMU_CNTR0_IDX 1
120#define CCI_PMU_CNTR_LAST(cci_pmu) (CCI_PMU_CYCLE_CNTR_IDX + cci_pmu->num_events - 1)
121
122/*
123 * CCI PMU event id is an 8-bit value made of two parts - bits 7:5 for one of 8
124 * ports and bits 4:0 are event codes. There are different event codes
125 * associated with each port type.
126 *
127 * Additionally, the range of events associated with the port types changed
128 * between Rev0 and Rev1.
129 *
130 * The constants below define the range of valid codes for each port type for
131 * the different revisions and are used to validate the event to be monitored.
132 */
133
134#define CCI_REV_R0_SLAVE_PORT_MIN_EV 0x00
135#define CCI_REV_R0_SLAVE_PORT_MAX_EV 0x13
136#define CCI_REV_R0_MASTER_PORT_MIN_EV 0x14
137#define CCI_REV_R0_MASTER_PORT_MAX_EV 0x1a
138
139#define CCI_REV_R1_SLAVE_PORT_MIN_EV 0x00
140#define CCI_REV_R1_SLAVE_PORT_MAX_EV 0x14
141#define CCI_REV_R1_MASTER_PORT_MIN_EV 0x00
142#define CCI_REV_R1_MASTER_PORT_MAX_EV 0x11
143
144struct pmu_port_event_ranges {
145 u8 slave_min;
146 u8 slave_max;
147 u8 master_min;
148 u8 master_max;
149};
150
151static struct pmu_port_event_ranges port_event_range[] = {
152 [CCI_REV_R0] = {
153 .slave_min = CCI_REV_R0_SLAVE_PORT_MIN_EV,
154 .slave_max = CCI_REV_R0_SLAVE_PORT_MAX_EV,
155 .master_min = CCI_REV_R0_MASTER_PORT_MIN_EV,
156 .master_max = CCI_REV_R0_MASTER_PORT_MAX_EV,
157 },
158 [CCI_REV_R1] = {
159 .slave_min = CCI_REV_R1_SLAVE_PORT_MIN_EV,
160 .slave_max = CCI_REV_R1_SLAVE_PORT_MAX_EV,
161 .master_min = CCI_REV_R1_MASTER_PORT_MIN_EV,
162 .master_max = CCI_REV_R1_MASTER_PORT_MAX_EV,
163 },
164};
165
166struct cci_pmu_drv_data {
167 void __iomem *base;
168 struct arm_pmu *cci_pmu;
169 int nr_irqs;
170 int irqs[CCI_PMU_MAX_HW_EVENTS];
171 unsigned long active_irqs;
172 struct perf_event *events[CCI_PMU_MAX_HW_EVENTS];
173 unsigned long used_mask[BITS_TO_LONGS(CCI_PMU_MAX_HW_EVENTS)];
174 struct pmu_port_event_ranges *port_ranges;
175 struct pmu_hw_events hw_events;
176};
177static struct cci_pmu_drv_data *pmu;
178
179static bool is_duplicate_irq(int irq, int *irqs, int nr_irqs)
180{
181 int i;
182
183 for (i = 0; i < nr_irqs; i++)
184 if (irq == irqs[i])
185 return true;
186
187 return false;
188}
189
190static int probe_cci_revision(void)
191{
192 int rev;
193 rev = readl_relaxed(cci_ctrl_base + CCI_PID2) & CCI_PID2_REV_MASK;
194 rev >>= CCI_PID2_REV_SHIFT;
195
196 if (rev <= CCI_REV_R0_P4)
197 return CCI_REV_R0;
198 else if (rev <= CCI_REV_R1_P2)
199 return CCI_REV_R1;
200
201 return -ENOENT;
202}
203
204static struct pmu_port_event_ranges *port_range_by_rev(void)
205{
206 int rev = probe_cci_revision();
207
208 if (rev < 0)
209 return NULL;
210
211 return &port_event_range[rev];
212}
213
214static int pmu_is_valid_slave_event(u8 ev_code)
215{
216 return pmu->port_ranges->slave_min <= ev_code &&
217 ev_code <= pmu->port_ranges->slave_max;
218}
219
220static int pmu_is_valid_master_event(u8 ev_code)
221{
222 return pmu->port_ranges->master_min <= ev_code &&
223 ev_code <= pmu->port_ranges->master_max;
224}
225
226static int pmu_validate_hw_event(u8 hw_event)
227{
228 u8 ev_source = CCI_PMU_EVENT_SOURCE(hw_event);
229 u8 ev_code = CCI_PMU_EVENT_CODE(hw_event);
230
231 switch (ev_source) {
232 case CCI_PORT_S0:
233 case CCI_PORT_S1:
234 case CCI_PORT_S2:
235 case CCI_PORT_S3:
236 case CCI_PORT_S4:
237 /* Slave Interface */
238 if (pmu_is_valid_slave_event(ev_code))
239 return hw_event;
240 break;
241 case CCI_PORT_M0:
242 case CCI_PORT_M1:
243 case CCI_PORT_M2:
244 /* Master Interface */
245 if (pmu_is_valid_master_event(ev_code))
246 return hw_event;
247 break;
248 }
249
250 return -ENOENT;
251}
252
253static int pmu_is_valid_counter(struct arm_pmu *cci_pmu, int idx)
254{
255 return CCI_PMU_CYCLE_CNTR_IDX <= idx &&
256 idx <= CCI_PMU_CNTR_LAST(cci_pmu);
257}
258
259static u32 pmu_read_register(int idx, unsigned int offset)
260{
261 return readl_relaxed(pmu->base + CCI_PMU_CNTR_BASE(idx) + offset);
262}
263
264static void pmu_write_register(u32 value, int idx, unsigned int offset)
265{
266 return writel_relaxed(value, pmu->base + CCI_PMU_CNTR_BASE(idx) + offset);
267}
268
269static void pmu_disable_counter(int idx)
270{
271 pmu_write_register(0, idx, CCI_PMU_CNTR_CTRL);
272}
273
274static void pmu_enable_counter(int idx)
275{
276 pmu_write_register(1, idx, CCI_PMU_CNTR_CTRL);
277}
278
279static void pmu_set_event(int idx, unsigned long event)
280{
281 event &= CCI_PMU_EVENT_MASK;
282 pmu_write_register(event, idx, CCI_PMU_EVT_SEL);
283}
284
285static u32 pmu_get_max_counters(void)
286{
287 u32 n_cnts = (readl_relaxed(cci_ctrl_base + CCI_PMCR) &
288 CCI_PMCR_NCNT_MASK) >> CCI_PMCR_NCNT_SHIFT;
289
290 /* add 1 for cycle counter */
291 return n_cnts + 1;
292}
293
294static struct pmu_hw_events *pmu_get_hw_events(void)
295{
296 return &pmu->hw_events;
297}
298
299static int pmu_get_event_idx(struct pmu_hw_events *hw, struct perf_event *event)
300{
301 struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
302 struct hw_perf_event *hw_event = &event->hw;
303 unsigned long cci_event = hw_event->config_base & CCI_PMU_EVENT_MASK;
304 int idx;
305
306 if (cci_event == CCI_PMU_CYCLES) {
307 if (test_and_set_bit(CCI_PMU_CYCLE_CNTR_IDX, hw->used_mask))
308 return -EAGAIN;
309
310 return CCI_PMU_CYCLE_CNTR_IDX;
311 }
312
313 for (idx = CCI_PMU_CNTR0_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); ++idx)
314 if (!test_and_set_bit(idx, hw->used_mask))
315 return idx;
316
317 /* No counters available */
318 return -EAGAIN;
319}
320
321static int pmu_map_event(struct perf_event *event)
322{
323 int mapping;
324 u8 config = event->attr.config & CCI_PMU_EVENT_MASK;
325
326 if (event->attr.type < PERF_TYPE_MAX)
327 return -ENOENT;
328
329 if (config == CCI_PMU_CYCLES)
330 mapping = config;
331 else
332 mapping = pmu_validate_hw_event(config);
333
334 return mapping;
335}
336
337static int pmu_request_irq(struct arm_pmu *cci_pmu, irq_handler_t handler)
338{
339 int i;
340 struct platform_device *pmu_device = cci_pmu->plat_device;
341
342 if (unlikely(!pmu_device))
343 return -ENODEV;
344
345 if (pmu->nr_irqs < 1) {
346 dev_err(&pmu_device->dev, "no irqs for CCI PMUs defined\n");
347 return -ENODEV;
348 }
349
350 /*
351 * Register all available CCI PMU interrupts. In the interrupt handler
352 * we iterate over the counters checking for interrupt source (the
353 * overflowing counter) and clear it.
354 *
355 * This should allow handling of non-unique interrupt for the counters.
356 */
357 for (i = 0; i < pmu->nr_irqs; i++) {
358 int err = request_irq(pmu->irqs[i], handler, IRQF_SHARED,
359 "arm-cci-pmu", cci_pmu);
360 if (err) {
361 dev_err(&pmu_device->dev, "unable to request IRQ%d for ARM CCI PMU counters\n",
362 pmu->irqs[i]);
363 return err;
364 }
365
366 set_bit(i, &pmu->active_irqs);
367 }
368
369 return 0;
370}
371
372static irqreturn_t pmu_handle_irq(int irq_num, void *dev)
373{
374 unsigned long flags;
375 struct arm_pmu *cci_pmu = (struct arm_pmu *)dev;
376 struct pmu_hw_events *events = cci_pmu->get_hw_events();
377 struct perf_sample_data data;
378 struct pt_regs *regs;
379 int idx, handled = IRQ_NONE;
380
381 raw_spin_lock_irqsave(&events->pmu_lock, flags);
382 regs = get_irq_regs();
383 /*
384 * Iterate over counters and update the corresponding perf events.
385 * This should work regardless of whether we have per-counter overflow
386 * interrupt or a combined overflow interrupt.
387 */
388 for (idx = CCI_PMU_CYCLE_CNTR_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++) {
389 struct perf_event *event = events->events[idx];
390 struct hw_perf_event *hw_counter;
391
392 if (!event)
393 continue;
394
395 hw_counter = &event->hw;
396
397 /* Did this counter overflow? */
398 if (!pmu_read_register(idx, CCI_PMU_OVRFLW) & CCI_PMU_OVRFLW_FLAG)
399 continue;
400
401 pmu_write_register(CCI_PMU_OVRFLW_FLAG, idx, CCI_PMU_OVRFLW);
402
403 handled = IRQ_HANDLED;
404
405 armpmu_event_update(event);
406 perf_sample_data_init(&data, 0, hw_counter->last_period);
407 if (!armpmu_event_set_period(event))
408 continue;
409
410 if (perf_event_overflow(event, &data, regs))
411 cci_pmu->disable(event);
412 }
413 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
414
415 return IRQ_RETVAL(handled);
416}
417
418static void pmu_free_irq(struct arm_pmu *cci_pmu)
419{
420 int i;
421
422 for (i = 0; i < pmu->nr_irqs; i++) {
423 if (!test_and_clear_bit(i, &pmu->active_irqs))
424 continue;
425
426 free_irq(pmu->irqs[i], cci_pmu);
427 }
428}
429
430static void pmu_enable_event(struct perf_event *event)
431{
432 unsigned long flags;
433 struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
434 struct pmu_hw_events *events = cci_pmu->get_hw_events();
435 struct hw_perf_event *hw_counter = &event->hw;
436 int idx = hw_counter->idx;
437
438 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
439 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
440 return;
441 }
442
443 raw_spin_lock_irqsave(&events->pmu_lock, flags);
444
445 /* Configure the event to count, unless you are counting cycles */
446 if (idx != CCI_PMU_CYCLE_CNTR_IDX)
447 pmu_set_event(idx, hw_counter->config_base);
448
449 pmu_enable_counter(idx);
450
451 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
452}
453
454static void pmu_disable_event(struct perf_event *event)
455{
456 struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
457 struct hw_perf_event *hw_counter = &event->hw;
458 int idx = hw_counter->idx;
459
460 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
461 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
462 return;
463 }
464
465 pmu_disable_counter(idx);
466}
467
468static void pmu_start(struct arm_pmu *cci_pmu)
469{
470 u32 val;
471 unsigned long flags;
472 struct pmu_hw_events *events = cci_pmu->get_hw_events();
473
474 raw_spin_lock_irqsave(&events->pmu_lock, flags);
475
476 /* Enable all the PMU counters. */
477 val = readl_relaxed(cci_ctrl_base + CCI_PMCR) | CCI_PMCR_CEN;
478 writel(val, cci_ctrl_base + CCI_PMCR);
479
480 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
481}
482
483static void pmu_stop(struct arm_pmu *cci_pmu)
484{
485 u32 val;
486 unsigned long flags;
487 struct pmu_hw_events *events = cci_pmu->get_hw_events();
488
489 raw_spin_lock_irqsave(&events->pmu_lock, flags);
490
491 /* Disable all the PMU counters. */
492 val = readl_relaxed(cci_ctrl_base + CCI_PMCR) & ~CCI_PMCR_CEN;
493 writel(val, cci_ctrl_base + CCI_PMCR);
494
495 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
496}
497
498static u32 pmu_read_counter(struct perf_event *event)
499{
500 struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
501 struct hw_perf_event *hw_counter = &event->hw;
502 int idx = hw_counter->idx;
503 u32 value;
504
505 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
506 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
507 return 0;
508 }
509 value = pmu_read_register(idx, CCI_PMU_CNTR);
510
511 return value;
512}
513
514static void pmu_write_counter(struct perf_event *event, u32 value)
515{
516 struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
517 struct hw_perf_event *hw_counter = &event->hw;
518 int idx = hw_counter->idx;
519
520 if (unlikely(!pmu_is_valid_counter(cci_pmu, idx)))
521 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
522 else
523 pmu_write_register(value, idx, CCI_PMU_CNTR);
524}
525
526static int cci_pmu_init(struct arm_pmu *cci_pmu, struct platform_device *pdev)
527{
528 *cci_pmu = (struct arm_pmu){
529 .name = PMU_NAME,
530 .max_period = (1LLU << 32) - 1,
531 .get_hw_events = pmu_get_hw_events,
532 .get_event_idx = pmu_get_event_idx,
533 .map_event = pmu_map_event,
534 .request_irq = pmu_request_irq,
535 .handle_irq = pmu_handle_irq,
536 .free_irq = pmu_free_irq,
537 .enable = pmu_enable_event,
538 .disable = pmu_disable_event,
539 .start = pmu_start,
540 .stop = pmu_stop,
541 .read_counter = pmu_read_counter,
542 .write_counter = pmu_write_counter,
543 };
544
545 cci_pmu->plat_device = pdev;
546 cci_pmu->num_events = pmu_get_max_counters();
547
548 return armpmu_register(cci_pmu, -1);
549}
550
551static const struct of_device_id arm_cci_pmu_matches[] = {
552 {
553 .compatible = "arm,cci-400-pmu",
554 },
555 {},
556};
557
558static int cci_pmu_probe(struct platform_device *pdev)
559{
560 struct resource *res;
561 int i, ret, irq;
562
563 pmu = devm_kzalloc(&pdev->dev, sizeof(*pmu), GFP_KERNEL);
564 if (!pmu)
565 return -ENOMEM;
566
567 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
568 if (!res) {
569 dev_warn(&pdev->dev, "Failed to get mem resource\n");
570 ret = -EINVAL;
571 goto memalloc_err;
572 };
573
574 pmu->base = devm_ioremap_resource(&pdev->dev, res);
575 if (!pmu->base) {
576 dev_warn(&pdev->dev, "Failed to ioremap\n");
577 ret = -ENOMEM;
578 goto memalloc_err;
579 }
580
581 /*
582 * CCI PMU has 5 overflow signals - one per counter; but some may be tied
583 * together to a common interrupt.
584 */
585 pmu->nr_irqs = 0;
586 for (i = 0; i < CCI_PMU_MAX_HW_EVENTS; i++) {
587 irq = platform_get_irq(pdev, i);
588 if (irq < 0)
589 break;
590
591 if (is_duplicate_irq(irq, pmu->irqs, pmu->nr_irqs))
592 continue;
593
594 pmu->irqs[pmu->nr_irqs++] = irq;
595 }
596
597 /*
598 * Ensure that the device tree has as many interrupts as the number
599 * of counters.
600 */
601 if (i < CCI_PMU_MAX_HW_EVENTS) {
602 dev_warn(&pdev->dev, "In-correct number of interrupts: %d, should be %d\n",
603 i, CCI_PMU_MAX_HW_EVENTS);
604 ret = -EINVAL;
605 goto memalloc_err;
606 }
607
608 pmu->port_ranges = port_range_by_rev();
609 if (!pmu->port_ranges) {
610 dev_warn(&pdev->dev, "CCI PMU version not supported\n");
611 ret = -EINVAL;
612 goto memalloc_err;
613 }
614
615 pmu->cci_pmu = devm_kzalloc(&pdev->dev, sizeof(*(pmu->cci_pmu)), GFP_KERNEL);
616 if (!pmu->cci_pmu) {
617 ret = -ENOMEM;
618 goto memalloc_err;
619 }
620
621 pmu->hw_events.events = pmu->events;
622 pmu->hw_events.used_mask = pmu->used_mask;
623 raw_spin_lock_init(&pmu->hw_events.pmu_lock);
624
625 ret = cci_pmu_init(pmu->cci_pmu, pdev);
626 if (ret)
627 goto pmuinit_err;
628
629 return 0;
630
631pmuinit_err:
632 kfree(pmu->cci_pmu);
633memalloc_err:
634 kfree(pmu);
635 return ret;
636}
637
638static int cci_platform_probe(struct platform_device *pdev)
639{
640 if (!cci_probed())
641 return -ENODEV;
642
643 return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
644}
645
646#endif /* CONFIG_HW_PERF_EVENTS */
647
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +0100648struct cpu_port {
649 u64 mpidr;
650 u32 port;
651};
Nicolas Pitre62158f82013-05-21 23:34:41 -0400652
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +0100653/*
654 * Use the port MSB as valid flag, shift can be made dynamic
655 * by computing number of bits required for port indexes.
656 * Code disabling CCI cpu ports runs with D-cache invalidated
657 * and SCTLR bit clear so data accesses must be kept to a minimum
658 * to improve performance; for now shift is left static to
659 * avoid one more data access while disabling the CCI port.
660 */
661#define PORT_VALID_SHIFT 31
662#define PORT_VALID (0x1 << PORT_VALID_SHIFT)
663
664static inline void init_cpu_port(struct cpu_port *port, u32 index, u64 mpidr)
665{
666 port->port = PORT_VALID | index;
667 port->mpidr = mpidr;
668}
669
670static inline bool cpu_port_is_valid(struct cpu_port *port)
671{
672 return !!(port->port & PORT_VALID);
673}
674
675static inline bool cpu_port_match(struct cpu_port *port, u64 mpidr)
676{
677 return port->mpidr == (mpidr & MPIDR_HWID_BITMASK);
678}
679
680static struct cpu_port cpu_port[NR_CPUS];
681
682/**
683 * __cci_ace_get_port - Function to retrieve the port index connected to
684 * a cpu or device.
685 *
686 * @dn: device node of the device to look-up
687 * @type: port type
688 *
689 * Return value:
690 * - CCI port index if success
691 * - -ENODEV if failure
692 */
693static int __cci_ace_get_port(struct device_node *dn, int type)
694{
695 int i;
696 bool ace_match;
697 struct device_node *cci_portn;
698
699 cci_portn = of_parse_phandle(dn, "cci-control-port", 0);
700 for (i = 0; i < nb_cci_ports; i++) {
701 ace_match = ports[i].type == type;
702 if (ace_match && cci_portn == ports[i].dn)
703 return i;
704 }
705 return -ENODEV;
706}
707
708int cci_ace_get_port(struct device_node *dn)
709{
710 return __cci_ace_get_port(dn, ACE_LITE_PORT);
711}
712EXPORT_SYMBOL_GPL(cci_ace_get_port);
713
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100714static void cci_ace_init_ports(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +0100715{
Sudeep KarkadaNagesha78b4d6e2013-06-17 14:51:48 +0100716 int port, cpu;
717 struct device_node *cpun;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +0100718
719 /*
720 * Port index look-up speeds up the function disabling ports by CPU,
721 * since the logical to port index mapping is done once and does
722 * not change after system boot.
723 * The stashed index array is initialized for all possible CPUs
724 * at probe time.
725 */
Sudeep KarkadaNagesha78b4d6e2013-06-17 14:51:48 +0100726 for_each_possible_cpu(cpu) {
727 /* too early to use cpu->of_node */
728 cpun = of_get_cpu_node(cpu, NULL);
729
730 if (WARN(!cpun, "Missing cpu device node\n"))
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +0100731 continue;
732
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +0100733 port = __cci_ace_get_port(cpun, ACE_PORT);
734 if (port < 0)
735 continue;
736
737 init_cpu_port(&cpu_port[cpu], port, cpu_logical_map(cpu));
738 }
739
740 for_each_possible_cpu(cpu) {
741 WARN(!cpu_port_is_valid(&cpu_port[cpu]),
742 "CPU %u does not have an associated CCI port\n",
743 cpu);
744 }
745}
746/*
747 * Functions to enable/disable a CCI interconnect slave port
748 *
749 * They are called by low-level power management code to disable slave
750 * interfaces snoops and DVM broadcast.
751 * Since they may execute with cache data allocation disabled and
752 * after the caches have been cleaned and invalidated the functions provide
753 * no explicit locking since they may run with D-cache disabled, so normal
754 * cacheable kernel locks based on ldrex/strex may not work.
755 * Locking has to be provided by BSP implementations to ensure proper
756 * operations.
757 */
758
759/**
760 * cci_port_control() - function to control a CCI port
761 *
762 * @port: index of the port to setup
763 * @enable: if true enables the port, if false disables it
764 */
765static void notrace cci_port_control(unsigned int port, bool enable)
766{
767 void __iomem *base = ports[port].base;
768
769 writel_relaxed(enable ? CCI_ENABLE_REQ : 0, base + CCI_PORT_CTRL);
770 /*
771 * This function is called from power down procedures
772 * and must not execute any instruction that might
773 * cause the processor to be put in a quiescent state
774 * (eg wfi). Hence, cpu_relax() can not be added to this
775 * read loop to optimize power, since it might hide possibly
776 * disruptive operations.
777 */
778 while (readl_relaxed(cci_ctrl_base + CCI_CTRL_STATUS) & 0x1)
779 ;
780}
781
782/**
783 * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
784 * reference
785 *
786 * @mpidr: mpidr of the CPU whose CCI port should be disabled
787 *
788 * Disabling a CCI port for a CPU implies disabling the CCI port
789 * controlling that CPU cluster. Code disabling CPU CCI ports
790 * must make sure that the CPU running the code is the last active CPU
791 * in the cluster ie all other CPUs are quiescent in a low power state.
792 *
793 * Return:
794 * 0 on success
795 * -ENODEV on port look-up failure
796 */
797int notrace cci_disable_port_by_cpu(u64 mpidr)
798{
799 int cpu;
800 bool is_valid;
801 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
802 is_valid = cpu_port_is_valid(&cpu_port[cpu]);
803 if (is_valid && cpu_port_match(&cpu_port[cpu], mpidr)) {
804 cci_port_control(cpu_port[cpu].port, false);
805 return 0;
806 }
807 }
808 return -ENODEV;
809}
810EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu);
811
812/**
Nicolas Pitre62158f82013-05-21 23:34:41 -0400813 * cci_enable_port_for_self() - enable a CCI port for calling CPU
814 *
815 * Enabling a CCI port for the calling CPU implies enabling the CCI
816 * port controlling that CPU's cluster. Caller must make sure that the
817 * CPU running the code is the first active CPU in the cluster and all
818 * other CPUs are quiescent in a low power state or waiting for this CPU
819 * to complete the CCI initialization.
820 *
821 * Because this is called when the MMU is still off and with no stack,
822 * the code must be position independent and ideally rely on callee
823 * clobbered registers only. To achieve this we must code this function
824 * entirely in assembler.
825 *
826 * On success this returns with the proper CCI port enabled. In case of
827 * any failure this never returns as the inability to enable the CCI is
828 * fatal and there is no possible recovery at this stage.
829 */
830asmlinkage void __naked cci_enable_port_for_self(void)
831{
832 asm volatile ("\n"
Arnd Bergmannf4902492013-06-03 15:15:36 +0200833" .arch armv7-a\n"
Nicolas Pitre62158f82013-05-21 23:34:41 -0400834" mrc p15, 0, r0, c0, c0, 5 @ get MPIDR value \n"
835" and r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n"
836" adr r1, 5f \n"
837" ldr r2, [r1] \n"
838" add r1, r1, r2 @ &cpu_port \n"
839" add ip, r1, %[sizeof_cpu_port] \n"
840
841 /* Loop over the cpu_port array looking for a matching MPIDR */
842"1: ldr r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
843" cmp r2, r0 @ compare MPIDR \n"
844" bne 2f \n"
845
846 /* Found a match, now test port validity */
847" ldr r3, [r1, %[offsetof_cpu_port_port]] \n"
848" tst r3, #"__stringify(PORT_VALID)" \n"
849" bne 3f \n"
850
851 /* no match, loop with the next cpu_port entry */
852"2: add r1, r1, %[sizeof_struct_cpu_port] \n"
853" cmp r1, ip @ done? \n"
854" blo 1b \n"
855
856 /* CCI port not found -- cheaply try to stall this CPU */
857"cci_port_not_found: \n"
858" wfi \n"
859" wfe \n"
860" b cci_port_not_found \n"
861
862 /* Use matched port index to look up the corresponding ports entry */
863"3: bic r3, r3, #"__stringify(PORT_VALID)" \n"
864" adr r0, 6f \n"
865" ldmia r0, {r1, r2} \n"
866" sub r1, r1, r0 @ virt - phys \n"
867" ldr r0, [r0, r2] @ *(&ports) \n"
868" mov r2, %[sizeof_struct_ace_port] \n"
869" mla r0, r2, r3, r0 @ &ports[index] \n"
870" sub r0, r0, r1 @ virt_to_phys() \n"
871
872 /* Enable the CCI port */
873" ldr r0, [r0, %[offsetof_port_phys]] \n"
874" mov r3, #"__stringify(CCI_ENABLE_REQ)" \n"
875" str r3, [r0, #"__stringify(CCI_PORT_CTRL)"] \n"
876
877 /* poll the status reg for completion */
878" adr r1, 7f \n"
879" ldr r0, [r1] \n"
880" ldr r0, [r0, r1] @ cci_ctrl_base \n"
881"4: ldr r1, [r0, #"__stringify(CCI_CTRL_STATUS)"] \n"
882" tst r1, #1 \n"
883" bne 4b \n"
884
885" mov r0, #0 \n"
886" bx lr \n"
887
888" .align 2 \n"
889"5: .word cpu_port - . \n"
890"6: .word . \n"
891" .word ports - 6b \n"
892"7: .word cci_ctrl_phys - . \n"
893 : :
894 [sizeof_cpu_port] "i" (sizeof(cpu_port)),
895#ifndef __ARMEB__
896 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
897#else
898 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
899#endif
900 [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
901 [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
902 [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
903 [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
904
905 unreachable();
906}
907
908/**
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +0100909 * __cci_control_port_by_device() - function to control a CCI port by device
910 * reference
911 *
912 * @dn: device node pointer of the device whose CCI port should be
913 * controlled
914 * @enable: if true enables the port, if false disables it
915 *
916 * Return:
917 * 0 on success
918 * -ENODEV on port look-up failure
919 */
920int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
921{
922 int port;
923
924 if (!dn)
925 return -ENODEV;
926
927 port = __cci_ace_get_port(dn, ACE_LITE_PORT);
928 if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n",
929 dn->full_name))
930 return -ENODEV;
931 cci_port_control(port, enable);
932 return 0;
933}
934EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
935
936/**
937 * __cci_control_port_by_index() - function to control a CCI port by port index
938 *
939 * @port: port index previously retrieved with cci_ace_get_port()
940 * @enable: if true enables the port, if false disables it
941 *
942 * Return:
943 * 0 on success
944 * -ENODEV on port index out of range
945 * -EPERM if operation carried out on an ACE PORT
946 */
947int notrace __cci_control_port_by_index(u32 port, bool enable)
948{
949 if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
950 return -ENODEV;
951 /*
952 * CCI control for ports connected to CPUS is extremely fragile
953 * and must be made to go through a specific and controlled
954 * interface (ie cci_disable_port_by_cpu(); control by general purpose
955 * indexing is therefore disabled for ACE ports.
956 */
957 if (ports[port].type == ACE_PORT)
958 return -EPERM;
959
960 cci_port_control(port, enable);
961 return 0;
962}
963EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
964
965static const struct cci_nb_ports cci400_ports = {
966 .nb_ace = 2,
967 .nb_ace_lite = 3
968};
969
970static const struct of_device_id arm_cci_matches[] = {
971 {.compatible = "arm,cci-400", .data = &cci400_ports },
972 {},
973};
974
975static const struct of_device_id arm_cci_ctrl_if_matches[] = {
976 {.compatible = "arm,cci-400-ctrl-if", },
977 {},
978};
979
Punit Agrawalb91c8f22013-08-22 14:41:51 +0100980static int cci_probe(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +0100981{
982 struct cci_nb_ports const *cci_config;
983 int ret, i, nb_ace = 0, nb_ace_lite = 0;
984 struct device_node *np, *cp;
Nicolas Pitre62158f82013-05-21 23:34:41 -0400985 struct resource res;
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +0100986 const char *match_str;
987 bool is_ace;
988
989 np = of_find_matching_node(NULL, arm_cci_matches);
990 if (!np)
991 return -ENODEV;
992
993 cci_config = of_match_node(arm_cci_matches, np)->data;
994 if (!cci_config)
995 return -ENODEV;
996
997 nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
998
999 ports = kcalloc(sizeof(*ports), nb_cci_ports, GFP_KERNEL);
1000 if (!ports)
1001 return -ENOMEM;
1002
Nicolas Pitre62158f82013-05-21 23:34:41 -04001003 ret = of_address_to_resource(np, 0, &res);
1004 if (!ret) {
1005 cci_ctrl_base = ioremap(res.start, resource_size(&res));
1006 cci_ctrl_phys = res.start;
1007 }
1008 if (ret || !cci_ctrl_base) {
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001009 WARN(1, "unable to ioremap CCI ctrl\n");
1010 ret = -ENXIO;
1011 goto memalloc_err;
1012 }
1013
1014 for_each_child_of_node(np, cp) {
1015 if (!of_match_node(arm_cci_ctrl_if_matches, cp))
1016 continue;
1017
1018 i = nb_ace + nb_ace_lite;
1019
1020 if (i >= nb_cci_ports)
1021 break;
1022
1023 if (of_property_read_string(cp, "interface-type",
1024 &match_str)) {
1025 WARN(1, "node %s missing interface-type property\n",
1026 cp->full_name);
1027 continue;
1028 }
1029 is_ace = strcmp(match_str, "ace") == 0;
1030 if (!is_ace && strcmp(match_str, "ace-lite")) {
1031 WARN(1, "node %s containing invalid interface-type property, skipping it\n",
1032 cp->full_name);
1033 continue;
1034 }
1035
Nicolas Pitre62158f82013-05-21 23:34:41 -04001036 ret = of_address_to_resource(cp, 0, &res);
1037 if (!ret) {
1038 ports[i].base = ioremap(res.start, resource_size(&res));
1039 ports[i].phys = res.start;
1040 }
1041 if (ret || !ports[i].base) {
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001042 WARN(1, "unable to ioremap CCI port %d\n", i);
1043 continue;
1044 }
1045
1046 if (is_ace) {
1047 if (WARN_ON(nb_ace >= cci_config->nb_ace))
1048 continue;
1049 ports[i].type = ACE_PORT;
1050 ++nb_ace;
1051 } else {
1052 if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
1053 continue;
1054 ports[i].type = ACE_LITE_PORT;
1055 ++nb_ace_lite;
1056 }
1057 ports[i].dn = cp;
1058 }
1059
1060 /* initialize a stashed array of ACE ports to speed-up look-up */
1061 cci_ace_init_ports();
1062
1063 /*
1064 * Multi-cluster systems may need this data when non-coherent, during
1065 * cluster power-up/power-down. Make sure it reaches main memory.
1066 */
1067 sync_cache_w(&cci_ctrl_base);
Nicolas Pitre62158f82013-05-21 23:34:41 -04001068 sync_cache_w(&cci_ctrl_phys);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001069 sync_cache_w(&ports);
1070 sync_cache_w(&cpu_port);
1071 __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
1072 pr_info("ARM CCI driver probed\n");
1073 return 0;
1074
1075memalloc_err:
1076
1077 kfree(ports);
1078 return ret;
1079}
1080
1081static int cci_init_status = -EAGAIN;
1082static DEFINE_MUTEX(cci_probing);
1083
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001084static int cci_init(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001085{
1086 if (cci_init_status != -EAGAIN)
1087 return cci_init_status;
1088
1089 mutex_lock(&cci_probing);
1090 if (cci_init_status == -EAGAIN)
1091 cci_init_status = cci_probe();
1092 mutex_unlock(&cci_probing);
1093 return cci_init_status;
1094}
1095
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001096#ifdef CONFIG_HW_PERF_EVENTS
1097static struct platform_driver cci_pmu_driver = {
1098 .driver = {
1099 .name = DRIVER_NAME_PMU,
1100 .of_match_table = arm_cci_pmu_matches,
1101 },
1102 .probe = cci_pmu_probe,
1103};
1104
1105static struct platform_driver cci_platform_driver = {
1106 .driver = {
1107 .name = DRIVER_NAME,
1108 .of_match_table = arm_cci_matches,
1109 },
1110 .probe = cci_platform_probe,
1111};
1112
1113static int __init cci_platform_init(void)
1114{
1115 int ret;
1116
1117 ret = platform_driver_register(&cci_pmu_driver);
1118 if (ret)
1119 return ret;
1120
1121 return platform_driver_register(&cci_platform_driver);
1122}
1123
1124#else
1125
1126static int __init cci_platform_init(void)
1127{
1128 return 0;
1129}
1130
1131#endif
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001132/*
1133 * To sort out early init calls ordering a helper function is provided to
1134 * check if the CCI driver has beed initialized. Function check if the driver
1135 * has been initialized, if not it calls the init function that probes
1136 * the driver and updates the return value.
1137 */
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001138bool cci_probed(void)
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001139{
1140 return cci_init() == 0;
1141}
1142EXPORT_SYMBOL_GPL(cci_probed);
1143
1144early_initcall(cci_init);
Punit Agrawalb91c8f22013-08-22 14:41:51 +01001145core_initcall(cci_platform_init);
Lorenzo Pieralisied69bdd2012-07-13 15:55:52 +01001146MODULE_LICENSE("GPL");
1147MODULE_DESCRIPTION("ARM CCI support");