PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / bus / arm-cci.c
blob962fd35cbd8d24f2645d9649dcaf0c7430815ea4
1 /*
2 * CCI cache coherent interconnect driver
4 * Copyright (C) 2013 ARM Ltd.
5 * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
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.
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.
17 #include <linux/arm-cci.h>
18 #include <linux/io.h>
19 #include <linux/module.h>
20 #include <linux/of_address.h>
21 #include <linux/of_irq.h>
22 #include <linux/of_platform.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
27 #include <asm/cacheflush.h>
28 #include <asm/irq_regs.h>
29 #include <asm/pmu.h>
30 #include <asm/smp_plat.h>
32 #define DRIVER_NAME "CCI-400"
33 #define DRIVER_NAME_PMU DRIVER_NAME " PMU"
34 #define PMU_NAME "CCI_400"
36 #define CCI_PORT_CTRL 0x0
37 #define CCI_CTRL_STATUS 0xc
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)
43 struct cci_nb_ports {
44 unsigned int nb_ace;
45 unsigned int nb_ace_lite;
48 enum cci_ace_port_type {
49 ACE_INVALID_PORT = 0x0,
50 ACE_PORT,
51 ACE_LITE_PORT,
54 struct cci_ace_port {
55 void __iomem *base;
56 unsigned long phys;
57 enum cci_ace_port_type type;
58 struct device_node *dn;
61 static struct cci_ace_port *ports;
62 static unsigned int nb_cci_ports;
64 static void __iomem *cci_ctrl_base;
65 static unsigned long cci_ctrl_phys;
67 #ifdef CONFIG_HW_PERF_EVENTS
69 #define CCI_PMCR 0x0100
70 #define CCI_PID2 0x0fe8
72 #define CCI_PMCR_CEN 0x00000001
73 #define CCI_PMCR_NCNT_MASK 0x0000f800
74 #define CCI_PMCR_NCNT_SHIFT 11
76 #define CCI_PID2_REV_MASK 0xf0
77 #define CCI_PID2_REV_SHIFT 4
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
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
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
99 #define CCI_PMU_OVRFLW_FLAG 1
101 #define CCI_PMU_CNTR_BASE(idx) ((idx) * SZ_4K)
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.
108 enum cci400_perf_events {
109 CCI_PMU_CYCLES = 0xff
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)
116 #define CCI_PMU_MAX_HW_EVENTS 5 /* CCI PMU has 4 counters + 1 cycle counter */
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)
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.
127 * Additionally, the range of events associated with the port types changed
128 * between Rev0 and Rev1.
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.
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
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
144 struct pmu_port_event_ranges {
145 u8 slave_min;
146 u8 slave_max;
147 u8 master_min;
148 u8 master_max;
151 static 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,
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,
166 struct 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;
177 static struct cci_pmu_drv_data *pmu;
179 static bool is_duplicate_irq(int irq, int *irqs, int nr_irqs)
181 int i;
183 for (i = 0; i < nr_irqs; i++)
184 if (irq == irqs[i])
185 return true;
187 return false;
190 static int probe_cci_revision(void)
192 int rev;
193 rev = readl_relaxed(cci_ctrl_base + CCI_PID2) & CCI_PID2_REV_MASK;
194 rev >>= CCI_PID2_REV_SHIFT;
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;
201 return -ENOENT;
204 static struct pmu_port_event_ranges *port_range_by_rev(void)
206 int rev = probe_cci_revision();
208 if (rev < 0)
209 return NULL;
211 return &port_event_range[rev];
214 static int pmu_is_valid_slave_event(u8 ev_code)
216 return pmu->port_ranges->slave_min <= ev_code &&
217 ev_code <= pmu->port_ranges->slave_max;
220 static int pmu_is_valid_master_event(u8 ev_code)
222 return pmu->port_ranges->master_min <= ev_code &&
223 ev_code <= pmu->port_ranges->master_max;
226 static int pmu_validate_hw_event(u8 hw_event)
228 u8 ev_source = CCI_PMU_EVENT_SOURCE(hw_event);
229 u8 ev_code = CCI_PMU_EVENT_CODE(hw_event);
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;
250 return -ENOENT;
253 static int pmu_is_valid_counter(struct arm_pmu *cci_pmu, int idx)
255 return CCI_PMU_CYCLE_CNTR_IDX <= idx &&
256 idx <= CCI_PMU_CNTR_LAST(cci_pmu);
259 static u32 pmu_read_register(int idx, unsigned int offset)
261 return readl_relaxed(pmu->base + CCI_PMU_CNTR_BASE(idx) + offset);
264 static void pmu_write_register(u32 value, int idx, unsigned int offset)
266 return writel_relaxed(value, pmu->base + CCI_PMU_CNTR_BASE(idx) + offset);
269 static void pmu_disable_counter(int idx)
271 pmu_write_register(0, idx, CCI_PMU_CNTR_CTRL);
274 static void pmu_enable_counter(int idx)
276 pmu_write_register(1, idx, CCI_PMU_CNTR_CTRL);
279 static void pmu_set_event(int idx, unsigned long event)
281 event &= CCI_PMU_EVENT_MASK;
282 pmu_write_register(event, idx, CCI_PMU_EVT_SEL);
285 static u32 pmu_get_max_counters(void)
287 u32 n_cnts = (readl_relaxed(cci_ctrl_base + CCI_PMCR) &
288 CCI_PMCR_NCNT_MASK) >> CCI_PMCR_NCNT_SHIFT;
290 /* add 1 for cycle counter */
291 return n_cnts + 1;
294 static struct pmu_hw_events *pmu_get_hw_events(void)
296 return &pmu->hw_events;
299 static int pmu_get_event_idx(struct pmu_hw_events *hw, struct perf_event *event)
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;
306 if (cci_event == CCI_PMU_CYCLES) {
307 if (test_and_set_bit(CCI_PMU_CYCLE_CNTR_IDX, hw->used_mask))
308 return -EAGAIN;
310 return CCI_PMU_CYCLE_CNTR_IDX;
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;
317 /* No counters available */
318 return -EAGAIN;
321 static int pmu_map_event(struct perf_event *event)
323 int mapping;
324 u8 config = event->attr.config & CCI_PMU_EVENT_MASK;
326 if (event->attr.type < PERF_TYPE_MAX)
327 return -ENOENT;
329 if (config == CCI_PMU_CYCLES)
330 mapping = config;
331 else
332 mapping = pmu_validate_hw_event(config);
334 return mapping;
337 static int pmu_request_irq(struct arm_pmu *cci_pmu, irq_handler_t handler)
339 int i;
340 struct platform_device *pmu_device = cci_pmu->plat_device;
342 if (unlikely(!pmu_device))
343 return -ENODEV;
345 if (pmu->nr_irqs < 1) {
346 dev_err(&pmu_device->dev, "no irqs for CCI PMUs defined\n");
347 return -ENODEV;
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.
355 * This should allow handling of non-unique interrupt for the counters.
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;
366 set_bit(i, &pmu->active_irqs);
369 return 0;
372 static irqreturn_t pmu_handle_irq(int irq_num, void *dev)
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;
381 raw_spin_lock_irqsave(&events->pmu_lock, flags);
382 regs = get_irq_regs();
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.
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;
392 if (!event)
393 continue;
395 hw_counter = &event->hw;
397 /* Did this counter overflow? */
398 if (!pmu_read_register(idx, CCI_PMU_OVRFLW) & CCI_PMU_OVRFLW_FLAG)
399 continue;
401 pmu_write_register(CCI_PMU_OVRFLW_FLAG, idx, CCI_PMU_OVRFLW);
403 handled = IRQ_HANDLED;
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;
410 if (perf_event_overflow(event, &data, regs))
411 cci_pmu->disable(event);
413 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
415 return IRQ_RETVAL(handled);
418 static void pmu_free_irq(struct arm_pmu *cci_pmu)
420 int i;
422 for (i = 0; i < pmu->nr_irqs; i++) {
423 if (!test_and_clear_bit(i, &pmu->active_irqs))
424 continue;
426 free_irq(pmu->irqs[i], cci_pmu);
430 static void pmu_enable_event(struct perf_event *event)
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;
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;
443 raw_spin_lock_irqsave(&events->pmu_lock, flags);
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);
449 pmu_enable_counter(idx);
451 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
454 static void pmu_disable_event(struct perf_event *event)
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;
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;
465 pmu_disable_counter(idx);
468 static void pmu_start(struct arm_pmu *cci_pmu)
470 u32 val;
471 unsigned long flags;
472 struct pmu_hw_events *events = cci_pmu->get_hw_events();
474 raw_spin_lock_irqsave(&events->pmu_lock, flags);
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);
480 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
483 static void pmu_stop(struct arm_pmu *cci_pmu)
485 u32 val;
486 unsigned long flags;
487 struct pmu_hw_events *events = cci_pmu->get_hw_events();
489 raw_spin_lock_irqsave(&events->pmu_lock, flags);
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);
495 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
498 static u32 pmu_read_counter(struct perf_event *event)
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;
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;
509 value = pmu_read_register(idx, CCI_PMU_CNTR);
511 return value;
514 static void pmu_write_counter(struct perf_event *event, u32 value)
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;
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);
526 static int cci_pmu_init(struct arm_pmu *cci_pmu, struct platform_device *pdev)
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,
545 cci_pmu->plat_device = pdev;
546 cci_pmu->num_events = pmu_get_max_counters();
548 return armpmu_register(cci_pmu, -1);
551 static const struct of_device_id arm_cci_pmu_matches[] = {
553 .compatible = "arm,cci-400-pmu",
558 static int cci_pmu_probe(struct platform_device *pdev)
560 struct resource *res;
561 int i, ret, irq;
563 pmu = devm_kzalloc(&pdev->dev, sizeof(*pmu), GFP_KERNEL);
564 if (!pmu)
565 return -ENOMEM;
567 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
568 pmu->base = devm_ioremap_resource(&pdev->dev, res);
569 if (IS_ERR(pmu->base))
570 return -ENOMEM;
573 * CCI PMU has 5 overflow signals - one per counter; but some may be tied
574 * together to a common interrupt.
576 pmu->nr_irqs = 0;
577 for (i = 0; i < CCI_PMU_MAX_HW_EVENTS; i++) {
578 irq = platform_get_irq(pdev, i);
579 if (irq < 0)
580 break;
582 if (is_duplicate_irq(irq, pmu->irqs, pmu->nr_irqs))
583 continue;
585 pmu->irqs[pmu->nr_irqs++] = irq;
589 * Ensure that the device tree has as many interrupts as the number
590 * of counters.
592 if (i < CCI_PMU_MAX_HW_EVENTS) {
593 dev_warn(&pdev->dev, "In-correct number of interrupts: %d, should be %d\n",
594 i, CCI_PMU_MAX_HW_EVENTS);
595 return -EINVAL;
598 pmu->port_ranges = port_range_by_rev();
599 if (!pmu->port_ranges) {
600 dev_warn(&pdev->dev, "CCI PMU version not supported\n");
601 return -EINVAL;
604 pmu->cci_pmu = devm_kzalloc(&pdev->dev, sizeof(*(pmu->cci_pmu)), GFP_KERNEL);
605 if (!pmu->cci_pmu)
606 return -ENOMEM;
608 pmu->hw_events.events = pmu->events;
609 pmu->hw_events.used_mask = pmu->used_mask;
610 raw_spin_lock_init(&pmu->hw_events.pmu_lock);
612 ret = cci_pmu_init(pmu->cci_pmu, pdev);
613 if (ret)
614 return ret;
616 return 0;
619 static int cci_platform_probe(struct platform_device *pdev)
621 if (!cci_probed())
622 return -ENODEV;
624 return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
627 #endif /* CONFIG_HW_PERF_EVENTS */
629 struct cpu_port {
630 u64 mpidr;
631 u32 port;
635 * Use the port MSB as valid flag, shift can be made dynamic
636 * by computing number of bits required for port indexes.
637 * Code disabling CCI cpu ports runs with D-cache invalidated
638 * and SCTLR bit clear so data accesses must be kept to a minimum
639 * to improve performance; for now shift is left static to
640 * avoid one more data access while disabling the CCI port.
642 #define PORT_VALID_SHIFT 31
643 #define PORT_VALID (0x1 << PORT_VALID_SHIFT)
645 static inline void init_cpu_port(struct cpu_port *port, u32 index, u64 mpidr)
647 port->port = PORT_VALID | index;
648 port->mpidr = mpidr;
651 static inline bool cpu_port_is_valid(struct cpu_port *port)
653 return !!(port->port & PORT_VALID);
656 static inline bool cpu_port_match(struct cpu_port *port, u64 mpidr)
658 return port->mpidr == (mpidr & MPIDR_HWID_BITMASK);
661 static struct cpu_port cpu_port[NR_CPUS];
664 * __cci_ace_get_port - Function to retrieve the port index connected to
665 * a cpu or device.
667 * @dn: device node of the device to look-up
668 * @type: port type
670 * Return value:
671 * - CCI port index if success
672 * - -ENODEV if failure
674 static int __cci_ace_get_port(struct device_node *dn, int type)
676 int i;
677 bool ace_match;
678 struct device_node *cci_portn;
680 cci_portn = of_parse_phandle(dn, "cci-control-port", 0);
681 for (i = 0; i < nb_cci_ports; i++) {
682 ace_match = ports[i].type == type;
683 if (ace_match && cci_portn == ports[i].dn)
684 return i;
686 return -ENODEV;
689 int cci_ace_get_port(struct device_node *dn)
691 return __cci_ace_get_port(dn, ACE_LITE_PORT);
693 EXPORT_SYMBOL_GPL(cci_ace_get_port);
695 static void cci_ace_init_ports(void)
697 int port, cpu;
698 struct device_node *cpun;
701 * Port index look-up speeds up the function disabling ports by CPU,
702 * since the logical to port index mapping is done once and does
703 * not change after system boot.
704 * The stashed index array is initialized for all possible CPUs
705 * at probe time.
707 for_each_possible_cpu(cpu) {
708 /* too early to use cpu->of_node */
709 cpun = of_get_cpu_node(cpu, NULL);
711 if (WARN(!cpun, "Missing cpu device node\n"))
712 continue;
714 port = __cci_ace_get_port(cpun, ACE_PORT);
715 if (port < 0)
716 continue;
718 init_cpu_port(&cpu_port[cpu], port, cpu_logical_map(cpu));
721 for_each_possible_cpu(cpu) {
722 WARN(!cpu_port_is_valid(&cpu_port[cpu]),
723 "CPU %u does not have an associated CCI port\n",
724 cpu);
728 * Functions to enable/disable a CCI interconnect slave port
730 * They are called by low-level power management code to disable slave
731 * interfaces snoops and DVM broadcast.
732 * Since they may execute with cache data allocation disabled and
733 * after the caches have been cleaned and invalidated the functions provide
734 * no explicit locking since they may run with D-cache disabled, so normal
735 * cacheable kernel locks based on ldrex/strex may not work.
736 * Locking has to be provided by BSP implementations to ensure proper
737 * operations.
741 * cci_port_control() - function to control a CCI port
743 * @port: index of the port to setup
744 * @enable: if true enables the port, if false disables it
746 static void notrace cci_port_control(unsigned int port, bool enable)
748 void __iomem *base = ports[port].base;
750 writel_relaxed(enable ? CCI_ENABLE_REQ : 0, base + CCI_PORT_CTRL);
752 * This function is called from power down procedures
753 * and must not execute any instruction that might
754 * cause the processor to be put in a quiescent state
755 * (eg wfi). Hence, cpu_relax() can not be added to this
756 * read loop to optimize power, since it might hide possibly
757 * disruptive operations.
759 while (readl_relaxed(cci_ctrl_base + CCI_CTRL_STATUS) & 0x1)
764 * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
765 * reference
767 * @mpidr: mpidr of the CPU whose CCI port should be disabled
769 * Disabling a CCI port for a CPU implies disabling the CCI port
770 * controlling that CPU cluster. Code disabling CPU CCI ports
771 * must make sure that the CPU running the code is the last active CPU
772 * in the cluster ie all other CPUs are quiescent in a low power state.
774 * Return:
775 * 0 on success
776 * -ENODEV on port look-up failure
778 int notrace cci_disable_port_by_cpu(u64 mpidr)
780 int cpu;
781 bool is_valid;
782 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
783 is_valid = cpu_port_is_valid(&cpu_port[cpu]);
784 if (is_valid && cpu_port_match(&cpu_port[cpu], mpidr)) {
785 cci_port_control(cpu_port[cpu].port, false);
786 return 0;
789 return -ENODEV;
791 EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu);
794 * cci_enable_port_for_self() - enable a CCI port for calling CPU
796 * Enabling a CCI port for the calling CPU implies enabling the CCI
797 * port controlling that CPU's cluster. Caller must make sure that the
798 * CPU running the code is the first active CPU in the cluster and all
799 * other CPUs are quiescent in a low power state or waiting for this CPU
800 * to complete the CCI initialization.
802 * Because this is called when the MMU is still off and with no stack,
803 * the code must be position independent and ideally rely on callee
804 * clobbered registers only. To achieve this we must code this function
805 * entirely in assembler.
807 * On success this returns with the proper CCI port enabled. In case of
808 * any failure this never returns as the inability to enable the CCI is
809 * fatal and there is no possible recovery at this stage.
811 asmlinkage void __naked cci_enable_port_for_self(void)
813 asm volatile ("\n"
814 " .arch armv7-a\n"
815 " mrc p15, 0, r0, c0, c0, 5 @ get MPIDR value \n"
816 " and r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n"
817 " adr r1, 5f \n"
818 " ldr r2, [r1] \n"
819 " add r1, r1, r2 @ &cpu_port \n"
820 " add ip, r1, %[sizeof_cpu_port] \n"
822 /* Loop over the cpu_port array looking for a matching MPIDR */
823 "1: ldr r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
824 " cmp r2, r0 @ compare MPIDR \n"
825 " bne 2f \n"
827 /* Found a match, now test port validity */
828 " ldr r3, [r1, %[offsetof_cpu_port_port]] \n"
829 " tst r3, #"__stringify(PORT_VALID)" \n"
830 " bne 3f \n"
832 /* no match, loop with the next cpu_port entry */
833 "2: add r1, r1, %[sizeof_struct_cpu_port] \n"
834 " cmp r1, ip @ done? \n"
835 " blo 1b \n"
837 /* CCI port not found -- cheaply try to stall this CPU */
838 "cci_port_not_found: \n"
839 " wfi \n"
840 " wfe \n"
841 " b cci_port_not_found \n"
843 /* Use matched port index to look up the corresponding ports entry */
844 "3: bic r3, r3, #"__stringify(PORT_VALID)" \n"
845 " adr r0, 6f \n"
846 " ldmia r0, {r1, r2} \n"
847 " sub r1, r1, r0 @ virt - phys \n"
848 " ldr r0, [r0, r2] @ *(&ports) \n"
849 " mov r2, %[sizeof_struct_ace_port] \n"
850 " mla r0, r2, r3, r0 @ &ports[index] \n"
851 " sub r0, r0, r1 @ virt_to_phys() \n"
853 /* Enable the CCI port */
854 " ldr r0, [r0, %[offsetof_port_phys]] \n"
855 " mov r3, %[cci_enable_req]\n"
856 " str r3, [r0, #"__stringify(CCI_PORT_CTRL)"] \n"
858 /* poll the status reg for completion */
859 " adr r1, 7f \n"
860 " ldr r0, [r1] \n"
861 " ldr r0, [r0, r1] @ cci_ctrl_base \n"
862 "4: ldr r1, [r0, #"__stringify(CCI_CTRL_STATUS)"] \n"
863 " tst r1, %[cci_control_status_bits] \n"
864 " bne 4b \n"
866 " mov r0, #0 \n"
867 " bx lr \n"
869 " .align 2 \n"
870 "5: .word cpu_port - . \n"
871 "6: .word . \n"
872 " .word ports - 6b \n"
873 "7: .word cci_ctrl_phys - . \n"
875 [sizeof_cpu_port] "i" (sizeof(cpu_port)),
876 [cci_enable_req] "i" cpu_to_le32(CCI_ENABLE_REQ),
877 [cci_control_status_bits] "i" cpu_to_le32(1),
878 #ifndef __ARMEB__
879 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
880 #else
881 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
882 #endif
883 [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
884 [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
885 [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
886 [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
888 unreachable();
892 * __cci_control_port_by_device() - function to control a CCI port by device
893 * reference
895 * @dn: device node pointer of the device whose CCI port should be
896 * controlled
897 * @enable: if true enables the port, if false disables it
899 * Return:
900 * 0 on success
901 * -ENODEV on port look-up failure
903 int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
905 int port;
907 if (!dn)
908 return -ENODEV;
910 port = __cci_ace_get_port(dn, ACE_LITE_PORT);
911 if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n",
912 dn->full_name))
913 return -ENODEV;
914 cci_port_control(port, enable);
915 return 0;
917 EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
920 * __cci_control_port_by_index() - function to control a CCI port by port index
922 * @port: port index previously retrieved with cci_ace_get_port()
923 * @enable: if true enables the port, if false disables it
925 * Return:
926 * 0 on success
927 * -ENODEV on port index out of range
928 * -EPERM if operation carried out on an ACE PORT
930 int notrace __cci_control_port_by_index(u32 port, bool enable)
932 if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
933 return -ENODEV;
935 * CCI control for ports connected to CPUS is extremely fragile
936 * and must be made to go through a specific and controlled
937 * interface (ie cci_disable_port_by_cpu(); control by general purpose
938 * indexing is therefore disabled for ACE ports.
940 if (ports[port].type == ACE_PORT)
941 return -EPERM;
943 cci_port_control(port, enable);
944 return 0;
946 EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
948 static const struct cci_nb_ports cci400_ports = {
949 .nb_ace = 2,
950 .nb_ace_lite = 3
953 static const struct of_device_id arm_cci_matches[] = {
954 {.compatible = "arm,cci-400", .data = &cci400_ports },
958 static const struct of_device_id arm_cci_ctrl_if_matches[] = {
959 {.compatible = "arm,cci-400-ctrl-if", },
963 static int cci_probe(void)
965 struct cci_nb_ports const *cci_config;
966 int ret, i, nb_ace = 0, nb_ace_lite = 0;
967 struct device_node *np, *cp;
968 struct resource res;
969 const char *match_str;
970 bool is_ace;
972 np = of_find_matching_node(NULL, arm_cci_matches);
973 if (!np)
974 return -ENODEV;
976 cci_config = of_match_node(arm_cci_matches, np)->data;
977 if (!cci_config)
978 return -ENODEV;
980 nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
982 ports = kcalloc(nb_cci_ports, sizeof(*ports), GFP_KERNEL);
983 if (!ports)
984 return -ENOMEM;
986 ret = of_address_to_resource(np, 0, &res);
987 if (!ret) {
988 cci_ctrl_base = ioremap(res.start, resource_size(&res));
989 cci_ctrl_phys = res.start;
991 if (ret || !cci_ctrl_base) {
992 WARN(1, "unable to ioremap CCI ctrl\n");
993 ret = -ENXIO;
994 goto memalloc_err;
997 for_each_child_of_node(np, cp) {
998 if (!of_match_node(arm_cci_ctrl_if_matches, cp))
999 continue;
1001 i = nb_ace + nb_ace_lite;
1003 if (i >= nb_cci_ports)
1004 break;
1006 if (of_property_read_string(cp, "interface-type",
1007 &match_str)) {
1008 WARN(1, "node %s missing interface-type property\n",
1009 cp->full_name);
1010 continue;
1012 is_ace = strcmp(match_str, "ace") == 0;
1013 if (!is_ace && strcmp(match_str, "ace-lite")) {
1014 WARN(1, "node %s containing invalid interface-type property, skipping it\n",
1015 cp->full_name);
1016 continue;
1019 ret = of_address_to_resource(cp, 0, &res);
1020 if (!ret) {
1021 ports[i].base = ioremap(res.start, resource_size(&res));
1022 ports[i].phys = res.start;
1024 if (ret || !ports[i].base) {
1025 WARN(1, "unable to ioremap CCI port %d\n", i);
1026 continue;
1029 if (is_ace) {
1030 if (WARN_ON(nb_ace >= cci_config->nb_ace))
1031 continue;
1032 ports[i].type = ACE_PORT;
1033 ++nb_ace;
1034 } else {
1035 if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
1036 continue;
1037 ports[i].type = ACE_LITE_PORT;
1038 ++nb_ace_lite;
1040 ports[i].dn = cp;
1043 /* initialize a stashed array of ACE ports to speed-up look-up */
1044 cci_ace_init_ports();
1047 * Multi-cluster systems may need this data when non-coherent, during
1048 * cluster power-up/power-down. Make sure it reaches main memory.
1050 sync_cache_w(&cci_ctrl_base);
1051 sync_cache_w(&cci_ctrl_phys);
1052 sync_cache_w(&ports);
1053 sync_cache_w(&cpu_port);
1054 __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
1055 pr_info("ARM CCI driver probed\n");
1056 return 0;
1058 memalloc_err:
1060 kfree(ports);
1061 return ret;
1064 static int cci_init_status = -EAGAIN;
1065 static DEFINE_MUTEX(cci_probing);
1067 static int cci_init(void)
1069 if (cci_init_status != -EAGAIN)
1070 return cci_init_status;
1072 mutex_lock(&cci_probing);
1073 if (cci_init_status == -EAGAIN)
1074 cci_init_status = cci_probe();
1075 mutex_unlock(&cci_probing);
1076 return cci_init_status;
1079 #ifdef CONFIG_HW_PERF_EVENTS
1080 static struct platform_driver cci_pmu_driver = {
1081 .driver = {
1082 .name = DRIVER_NAME_PMU,
1083 .of_match_table = arm_cci_pmu_matches,
1085 .probe = cci_pmu_probe,
1088 static struct platform_driver cci_platform_driver = {
1089 .driver = {
1090 .name = DRIVER_NAME,
1091 .of_match_table = arm_cci_matches,
1093 .probe = cci_platform_probe,
1096 static int __init cci_platform_init(void)
1098 int ret;
1100 ret = platform_driver_register(&cci_pmu_driver);
1101 if (ret)
1102 return ret;
1104 return platform_driver_register(&cci_platform_driver);
1107 #else
1109 static int __init cci_platform_init(void)
1111 return 0;
1114 #endif
1116 * To sort out early init calls ordering a helper function is provided to
1117 * check if the CCI driver has beed initialized. Function check if the driver
1118 * has been initialized, if not it calls the init function that probes
1119 * the driver and updates the return value.
1121 bool cci_probed(void)
1123 return cci_init() == 0;
1125 EXPORT_SYMBOL_GPL(cci_probed);
1127 early_initcall(cci_init);
1128 core_initcall(cci_platform_init);
1129 MODULE_LICENSE("GPL");
1130 MODULE_DESCRIPTION("ARM CCI support");