blob: d9d954eb0fa0135e5a868cec4850237ff56f8804 [file] [log] [blame]
Lorenzo Pieralisie53b9232012-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>
Jon Medhurst0b9bd1c2013-05-10 14:47:27 +010020#include <linux/platform_device.h>
Lorenzo Pieralisie53b9232012-07-13 15:55:52 +010021#include <linux/of_address.h>
22#include <linux/slab.h>
23
24#include <asm/cacheflush.h>
Punit Agrawal4311d5a2012-09-20 16:39:25 +010025#include <asm/irq_regs.h>
26#include <asm/pmu.h>
Lorenzo Pieralisie53b9232012-07-13 15:55:52 +010027#include <asm/smp_plat.h>
28
Jon Medhurst0b9bd1c2013-05-10 14:47:27 +010029#define DRIVER_NAME "CCI"
30
Lorenzo Pieralisie53b9232012-07-13 15:55:52 +010031#define CCI_PORT_CTRL 0x0
32#define CCI_CTRL_STATUS 0xc
33
34#define CCI_ENABLE_SNOOP_REQ 0x1
35#define CCI_ENABLE_DVM_REQ 0x2
36#define CCI_ENABLE_REQ (CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ)
37
38struct cci_nb_ports {
39 unsigned int nb_ace;
40 unsigned int nb_ace_lite;
41};
42
43enum cci_ace_port_type {
44 ACE_INVALID_PORT = 0x0,
45 ACE_PORT,
46 ACE_LITE_PORT,
47};
48
49struct cci_ace_port {
50 void __iomem *base;
Nicolas Pitreab28e082013-05-21 23:34:41 -040051 unsigned long phys;
Lorenzo Pieralisie53b9232012-07-13 15:55:52 +010052 enum cci_ace_port_type type;
53 struct device_node *dn;
54};
55
56static struct cci_ace_port *ports;
57static unsigned int nb_cci_ports;
58
59static void __iomem *cci_ctrl_base;
Nicolas Pitreab28e082013-05-21 23:34:41 -040060static unsigned long cci_ctrl_phys;
Lorenzo Pieralisie53b9232012-07-13 15:55:52 +010061
Jon Medhurst0b9bd1c2013-05-10 14:47:27 +010062#ifdef CONFIG_HW_PERF_EVENTS
63
64static void __iomem *cci_pmu_base;
65
Punit Agrawal4311d5a2012-09-20 16:39:25 +010066#define CCI400_PMCR 0x0100
67
68#define CCI400_PMU_CYCLE_CNTR_BASE 0x0000
69#define CCI400_PMU_CNTR_BASE(idx) (CCI400_PMU_CYCLE_CNTR_BASE + (idx) * 0x1000)
70
71#define CCI400_PMCR_CEN 0x00000001
72#define CCI400_PMCR_RST 0x00000002
73#define CCI400_PMCR_CCR 0x00000004
74#define CCI400_PMCR_CCD 0x00000008
75#define CCI400_PMCR_EX 0x00000010
76#define CCI400_PMCR_DP 0x00000020
77#define CCI400_PMCR_NCNT_MASK 0x0000F800
78#define CCI400_PMCR_NCNT_SHIFT 11
79
80#define CCI400_PMU_EVT_SEL 0x000
81#define CCI400_PMU_CNTR 0x004
82#define CCI400_PMU_CNTR_CTRL 0x008
83#define CCI400_PMU_OVERFLOW 0x00C
84
85#define CCI400_PMU_OVERFLOW_FLAG 1
86
87enum cci400_perf_events {
88 CCI400_PMU_CYCLES = 0xFF
89};
90
91#define CCI400_PMU_EVENT_MASK 0xff
92#define CCI400_PMU_EVENT_SOURCE(event) ((event >> 5) & 0x7)
93#define CCI400_PMU_EVENT_CODE(event) (event & 0x1f)
94
95#define CCI400_PMU_EVENT_SOURCE_S0 0
96#define CCI400_PMU_EVENT_SOURCE_S4 4
97#define CCI400_PMU_EVENT_SOURCE_M0 5
98#define CCI400_PMU_EVENT_SOURCE_M2 7
99
100#define CCI400_PMU_EVENT_SLAVE_MIN 0x0
101#define CCI400_PMU_EVENT_SLAVE_MAX 0x13
102
103#define CCI400_PMU_EVENT_MASTER_MIN 0x14
104#define CCI400_PMU_EVENT_MASTER_MAX 0x1A
105
106#define CCI400_PMU_MAX_HW_EVENTS 5 /* CCI PMU has 4 counters + 1 cycle counter */
107
108#define CCI400_PMU_CYCLE_COUNTER_IDX 0
109#define CCI400_PMU_COUNTER0_IDX 1
110#define CCI400_PMU_COUNTER_LAST(cci_pmu) (CCI400_PMU_CYCLE_COUNTER_IDX + cci_pmu->num_events - 1)
111
112
113static struct perf_event *events[CCI400_PMU_MAX_HW_EVENTS];
114static unsigned long used_mask[BITS_TO_LONGS(CCI400_PMU_MAX_HW_EVENTS)];
115static struct pmu_hw_events cci_hw_events = {
116 .events = events,
117 .used_mask = used_mask,
118};
119
120static int cci_pmu_validate_hw_event(u8 hw_event)
121{
122 u8 ev_source = CCI400_PMU_EVENT_SOURCE(hw_event);
123 u8 ev_code = CCI400_PMU_EVENT_CODE(hw_event);
124
125 if (ev_source <= CCI400_PMU_EVENT_SOURCE_S4 &&
126 ev_code <= CCI400_PMU_EVENT_SLAVE_MAX)
127 return hw_event;
128 else if (CCI400_PMU_EVENT_SOURCE_M0 <= ev_source &&
129 ev_source <= CCI400_PMU_EVENT_SOURCE_M2 &&
130 CCI400_PMU_EVENT_MASTER_MIN <= ev_code &&
131 ev_code <= CCI400_PMU_EVENT_MASTER_MAX)
132 return hw_event;
133
134 return -EINVAL;
135}
136
137static inline int cci_pmu_counter_is_valid(struct arm_pmu *cci_pmu, int idx)
138{
139 return CCI400_PMU_CYCLE_COUNTER_IDX <= idx &&
140 idx <= CCI400_PMU_COUNTER_LAST(cci_pmu);
141}
142
143static inline u32 cci_pmu_read_register(int idx, unsigned int offset)
144{
145 return readl_relaxed(cci_pmu_base + CCI400_PMU_CNTR_BASE(idx) + offset);
146}
147
148static inline void cci_pmu_write_register(u32 value, int idx, unsigned int offset)
149{
150 return writel_relaxed(value, cci_pmu_base + CCI400_PMU_CNTR_BASE(idx) + offset);
151}
152
153static inline void cci_pmu_disable_counter(int idx)
154{
155 cci_pmu_write_register(0, idx, CCI400_PMU_CNTR_CTRL);
156}
157
158static inline void cci_pmu_enable_counter(int idx)
159{
160 cci_pmu_write_register(1, idx, CCI400_PMU_CNTR_CTRL);
161}
162
163static inline void cci_pmu_select_event(int idx, unsigned long event)
164{
165 event &= CCI400_PMU_EVENT_MASK;
166 cci_pmu_write_register(event, idx, CCI400_PMU_EVT_SEL);
167}
168
169static u32 cci_pmu_get_max_counters(void)
170{
171 u32 n_cnts = (readl_relaxed(cci_ctrl_base + CCI400_PMCR) &
172 CCI400_PMCR_NCNT_MASK) >> CCI400_PMCR_NCNT_SHIFT;
173
174 /* add 1 for cycle counter */
175 return n_cnts + 1;
176}
177
178static struct pmu_hw_events *cci_pmu_get_hw_events(void)
179{
180 return &cci_hw_events;
181}
182
183static int cci_pmu_get_event_idx(struct pmu_hw_events *hw, struct perf_event *event)
184{
185 struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
186 struct hw_perf_event *hw_event = &event->hw;
187 unsigned long cci_event = hw_event->config_base & CCI400_PMU_EVENT_MASK;
188 int idx;
189
190 if (cci_event == CCI400_PMU_CYCLES) {
191 if (test_and_set_bit(CCI400_PMU_CYCLE_COUNTER_IDX, hw->used_mask))
192 return -EAGAIN;
193
194 return CCI400_PMU_CYCLE_COUNTER_IDX;
195 }
196
197 for (idx = CCI400_PMU_COUNTER0_IDX; idx <= CCI400_PMU_COUNTER_LAST(cci_pmu); ++idx) {
198 if (!test_and_set_bit(idx, hw->used_mask))
199 return idx;
200 }
201
202 /* No counters available */
203 return -EAGAIN;
204}
205
206static int cci_pmu_map_event(struct perf_event *event)
207{
208 int mapping;
209 u8 config = event->attr.config & CCI400_PMU_EVENT_MASK;
210
211 if (event->attr.type < PERF_TYPE_MAX)
212 return -ENOENT;
213
214 /* 0xff is used to represent CCI Cycles */
215 if (config == 0xff)
216 mapping = config;
217 else
218 mapping = cci_pmu_validate_hw_event(config);
219
220 return mapping;
221}
222
223static int cci_pmu_request_irq(struct arm_pmu *cci_pmu, irq_handler_t handler)
224{
225 int irq, err, i = 0;
226 struct platform_device *pmu_device = cci_pmu->plat_device;
227
228 if (unlikely(!pmu_device))
229 return -ENODEV;
230
231 /* CCI exports 6 interrupts - 1 nERRORIRQ + 5 nEVNTCNTOVERFLOW (PMU)
232 nERRORIRQ will be handled by secure firmware on TC2. So we
233 assume that all CCI interrupts listed in the linux device
234 tree are PMU interrupts.
235
236 The following code should then be able to handle different routing
237 of the CCI PMU interrupts.
238 */
239 while ((irq = platform_get_irq(pmu_device, i)) > 0) {
240 err = request_irq(irq, handler, 0, "arm-cci-pmu", cci_pmu);
241 if (err) {
242 dev_err(&pmu_device->dev, "unable to request IRQ%d for ARM CCI PMU counters\n",
243 irq);
244 return err;
245 }
246 i++;
247 }
248
249 return 0;
250}
251
252static irqreturn_t cci_pmu_handle_irq(int irq_num, void *dev)
253{
254 struct arm_pmu *cci_pmu = (struct arm_pmu *)dev;
255 struct pmu_hw_events *events = cci_pmu->get_hw_events();
256 struct perf_sample_data data;
257 struct pt_regs *regs;
258 int idx;
259
260 regs = get_irq_regs();
261
262 /* Iterate over counters and update the corresponding perf events.
263 This should work regardless of whether we have per-counter overflow
264 interrupt or a combined overflow interrupt. */
265 for (idx = CCI400_PMU_CYCLE_COUNTER_IDX; idx <= CCI400_PMU_COUNTER_LAST(cci_pmu); idx++) {
266 struct perf_event *event = events->events[idx];
267 struct hw_perf_event *hw_counter;
268
269 if (!event)
270 continue;
271
272 hw_counter = &event->hw;
273
274 /* Did this counter overflow? */
275 if (!(cci_pmu_read_register(idx, CCI400_PMU_OVERFLOW) & CCI400_PMU_OVERFLOW_FLAG))
276 continue;
277 cci_pmu_write_register(CCI400_PMU_OVERFLOW_FLAG, idx, CCI400_PMU_OVERFLOW);
278
279 armpmu_event_update(event);
280 perf_sample_data_init(&data, 0, hw_counter->last_period);
281 if (!armpmu_event_set_period(event))
282 continue;
283
284 if (perf_event_overflow(event, &data, regs))
285 cci_pmu->disable(event);
286 }
287
288 irq_work_run();
289 return IRQ_HANDLED;
290}
291
292static void cci_pmu_free_irq(struct arm_pmu *cci_pmu)
293{
294 int irq, i = 0;
295 struct platform_device *pmu_device = cci_pmu->plat_device;
296
297 while ((irq = platform_get_irq(pmu_device, i)) > 0) {
298 free_irq(irq, cci_pmu);
299 i++;
300 }
301}
302
303static void cci_pmu_enable_event(struct perf_event *event)
304{
305 unsigned long flags;
306 struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
307 struct pmu_hw_events *events = cci_pmu->get_hw_events();
308 struct hw_perf_event *hw_counter = &event->hw;
309 int idx = hw_counter->idx;
310
311 if (unlikely(!cci_pmu_counter_is_valid(cci_pmu, idx))) {
312 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
313 return;
314 }
315
316 raw_spin_lock_irqsave(&events->pmu_lock, flags);
317
318 /* Configure the event to count, unless you are counting cycles */
319 if (idx != CCI400_PMU_CYCLE_COUNTER_IDX)
320 cci_pmu_select_event(idx, hw_counter->config_base);
321
322 cci_pmu_enable_counter(idx);
323
324 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
325}
326
327static void cci_pmu_disable_event(struct perf_event *event)
328{
329 unsigned long flags;
330 struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
331 struct pmu_hw_events *events = cci_pmu->get_hw_events();
332 struct hw_perf_event *hw_counter = &event->hw;
333 int idx = hw_counter->idx;
334
335 if (unlikely(!cci_pmu_counter_is_valid(cci_pmu, idx))) {
336 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
337 return;
338 }
339
340 raw_spin_lock_irqsave(&events->pmu_lock, flags);
341
342 cci_pmu_disable_counter(idx);
343
344 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
345}
346
347static void cci_pmu_start(struct arm_pmu *cci_pmu)
348{
349 u32 val;
350 unsigned long flags;
351 struct pmu_hw_events *events = cci_pmu->get_hw_events();
352
353 raw_spin_lock_irqsave(&events->pmu_lock, flags);
354
355 /* Enable all the PMU counters. */
356 val = readl(cci_ctrl_base + CCI400_PMCR) | CCI400_PMCR_CEN;
357 writel(val, cci_ctrl_base + CCI400_PMCR);
358
359 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
360}
361
362static void cci_pmu_stop(struct arm_pmu *cci_pmu)
363{
364 u32 val;
365 unsigned long flags;
366 struct pmu_hw_events *events = cci_pmu->get_hw_events();
367
368 raw_spin_lock_irqsave(&events->pmu_lock, flags);
369
370 /* Disable all the PMU counters. */
371 val = readl(cci_ctrl_base + CCI400_PMCR) & ~CCI400_PMCR_CEN;
372 writel(val, cci_ctrl_base + CCI400_PMCR);
373
374 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
375}
376
377static u32 cci_pmu_read_counter(struct perf_event *event)
378{
379 struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
380 struct hw_perf_event *hw_counter = &event->hw;
381 int idx = hw_counter->idx;
382 u32 value;
383
384 if (unlikely(!cci_pmu_counter_is_valid(cci_pmu, idx))) {
385 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
386 return 0;
387 }
388 value = cci_pmu_read_register(idx, CCI400_PMU_CNTR);
389
390 return value;
391}
392
393static void cci_pmu_write_counter(struct perf_event *event, u32 value)
394{
395 struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
396 struct hw_perf_event *hw_counter = &event->hw;
397 int idx = hw_counter->idx;
398
399 if (unlikely(!cci_pmu_counter_is_valid(cci_pmu, idx)))
400 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
401 else
402 cci_pmu_write_register(value, idx, CCI400_PMU_CNTR);
403}
404
405static struct arm_pmu cci_pmu = {
406 .name = DRIVER_NAME,
407 .max_period = (1LLU << 32) - 1,
408 .get_hw_events = cci_pmu_get_hw_events,
409 .get_event_idx = cci_pmu_get_event_idx,
410 .map_event = cci_pmu_map_event,
411 .request_irq = cci_pmu_request_irq,
412 .handle_irq = cci_pmu_handle_irq,
413 .free_irq = cci_pmu_free_irq,
414 .enable = cci_pmu_enable_event,
415 .disable = cci_pmu_disable_event,
416 .start = cci_pmu_start,
417 .stop = cci_pmu_stop,
418 .read_counter = cci_pmu_read_counter,
419 .write_counter = cci_pmu_write_counter,
420};
421
Jon Medhurst0b9bd1c2013-05-10 14:47:27 +0100422static int cci_pmu_probe(struct platform_device *pdev)
423{
424 struct resource *res;
425
426 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
427 cci_pmu_base = devm_ioremap_resource(&pdev->dev, res);
428 if (IS_ERR(cci_pmu_base))
429 return PTR_ERR(cci_pmu_base);
430
Punit Agrawal4311d5a2012-09-20 16:39:25 +0100431 cci_pmu.plat_device = pdev;
432 cci_pmu.num_events = cci_pmu_get_max_counters();
433 raw_spin_lock_init(&cci_hw_events.pmu_lock);
434 cpumask_setall(&cci_pmu.valid_cpus);
435
436 return armpmu_register(&cci_pmu, -1);
Jon Medhurst0b9bd1c2013-05-10 14:47:27 +0100437}
438
439static const struct of_device_id arm_cci_pmu_matches[] = {
440 {.compatible = "arm,cci-400-pmu"},
441 {},
442};
443
444static struct platform_driver cci_pmu_platform_driver = {
445 .driver = {
446 .name = DRIVER_NAME,
447 .of_match_table = arm_cci_pmu_matches,
448 },
449 .probe = cci_pmu_probe,
450};
451
452static int __init cci_pmu_init(void)
453{
454 if (platform_driver_register(&cci_pmu_platform_driver))
455 WARN(1, "unable to register CCI platform driver\n");
456 return 0;
457}
458
459#else
460
461static int __init cci_pmu_init(void)
462{
463 return 0;
464}
465
Jon Medhurst0b9bd1c2013-05-10 14:47:27 +0100466#endif /* CONFIG_HW_PERF_EVENTS */
467
Lorenzo Pieralisie53b9232012-07-13 15:55:52 +0100468struct cpu_port {
469 u64 mpidr;
470 u32 port;
471};
Nicolas Pitreab28e082013-05-21 23:34:41 -0400472
Lorenzo Pieralisie53b9232012-07-13 15:55:52 +0100473/*
474 * Use the port MSB as valid flag, shift can be made dynamic
475 * by computing number of bits required for port indexes.
476 * Code disabling CCI cpu ports runs with D-cache invalidated
477 * and SCTLR bit clear so data accesses must be kept to a minimum
478 * to improve performance; for now shift is left static to
479 * avoid one more data access while disabling the CCI port.
480 */
481#define PORT_VALID_SHIFT 31
482#define PORT_VALID (0x1 << PORT_VALID_SHIFT)
483
484static inline void init_cpu_port(struct cpu_port *port, u32 index, u64 mpidr)
485{
486 port->port = PORT_VALID | index;
487 port->mpidr = mpidr;
488}
489
490static inline bool cpu_port_is_valid(struct cpu_port *port)
491{
492 return !!(port->port & PORT_VALID);
493}
494
495static inline bool cpu_port_match(struct cpu_port *port, u64 mpidr)
496{
497 return port->mpidr == (mpidr & MPIDR_HWID_BITMASK);
498}
499
500static struct cpu_port cpu_port[NR_CPUS];
501
502/**
503 * __cci_ace_get_port - Function to retrieve the port index connected to
504 * a cpu or device.
505 *
506 * @dn: device node of the device to look-up
507 * @type: port type
508 *
509 * Return value:
510 * - CCI port index if success
511 * - -ENODEV if failure
512 */
513static int __cci_ace_get_port(struct device_node *dn, int type)
514{
515 int i;
516 bool ace_match;
517 struct device_node *cci_portn;
518
519 cci_portn = of_parse_phandle(dn, "cci-control-port", 0);
520 for (i = 0; i < nb_cci_ports; i++) {
521 ace_match = ports[i].type == type;
522 if (ace_match && cci_portn == ports[i].dn)
523 return i;
524 }
525 return -ENODEV;
526}
527
528int cci_ace_get_port(struct device_node *dn)
529{
530 return __cci_ace_get_port(dn, ACE_LITE_PORT);
531}
532EXPORT_SYMBOL_GPL(cci_ace_get_port);
533
534static void __init cci_ace_init_ports(void)
535{
536 int port, ac, cpu;
537 u64 hwid;
538 const u32 *cell;
539 struct device_node *cpun, *cpus;
540
541 cpus = of_find_node_by_path("/cpus");
542 if (WARN(!cpus, "Missing cpus node, bailing out\n"))
543 return;
544
545 if (WARN_ON(of_property_read_u32(cpus, "#address-cells", &ac)))
546 ac = of_n_addr_cells(cpus);
547
548 /*
549 * Port index look-up speeds up the function disabling ports by CPU,
550 * since the logical to port index mapping is done once and does
551 * not change after system boot.
552 * The stashed index array is initialized for all possible CPUs
553 * at probe time.
554 */
555 for_each_child_of_node(cpus, cpun) {
556 if (of_node_cmp(cpun->type, "cpu"))
557 continue;
558 cell = of_get_property(cpun, "reg", NULL);
559 if (WARN(!cell, "%s: missing reg property\n", cpun->full_name))
560 continue;
561
562 hwid = of_read_number(cell, ac);
563 cpu = get_logical_index(hwid & MPIDR_HWID_BITMASK);
564
565 if (cpu < 0 || !cpu_possible(cpu))
566 continue;
567 port = __cci_ace_get_port(cpun, ACE_PORT);
568 if (port < 0)
569 continue;
570
571 init_cpu_port(&cpu_port[cpu], port, cpu_logical_map(cpu));
572 }
573
574 for_each_possible_cpu(cpu) {
575 WARN(!cpu_port_is_valid(&cpu_port[cpu]),
576 "CPU %u does not have an associated CCI port\n",
577 cpu);
578 }
579}
580/*
581 * Functions to enable/disable a CCI interconnect slave port
582 *
583 * They are called by low-level power management code to disable slave
584 * interfaces snoops and DVM broadcast.
585 * Since they may execute with cache data allocation disabled and
586 * after the caches have been cleaned and invalidated the functions provide
587 * no explicit locking since they may run with D-cache disabled, so normal
588 * cacheable kernel locks based on ldrex/strex may not work.
589 * Locking has to be provided by BSP implementations to ensure proper
590 * operations.
591 */
592
593/**
594 * cci_port_control() - function to control a CCI port
595 *
596 * @port: index of the port to setup
597 * @enable: if true enables the port, if false disables it
598 */
599static void notrace cci_port_control(unsigned int port, bool enable)
600{
601 void __iomem *base = ports[port].base;
602
603 writel_relaxed(enable ? CCI_ENABLE_REQ : 0, base + CCI_PORT_CTRL);
604 /*
605 * This function is called from power down procedures
606 * and must not execute any instruction that might
607 * cause the processor to be put in a quiescent state
608 * (eg wfi). Hence, cpu_relax() can not be added to this
609 * read loop to optimize power, since it might hide possibly
610 * disruptive operations.
611 */
612 while (readl_relaxed(cci_ctrl_base + CCI_CTRL_STATUS) & 0x1)
613 ;
614}
615
616/**
617 * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
618 * reference
619 *
620 * @mpidr: mpidr of the CPU whose CCI port should be disabled
621 *
622 * Disabling a CCI port for a CPU implies disabling the CCI port
623 * controlling that CPU cluster. Code disabling CPU CCI ports
624 * must make sure that the CPU running the code is the last active CPU
625 * in the cluster ie all other CPUs are quiescent in a low power state.
626 *
627 * Return:
628 * 0 on success
629 * -ENODEV on port look-up failure
630 */
631int notrace cci_disable_port_by_cpu(u64 mpidr)
632{
633 int cpu;
634 bool is_valid;
635 for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
636 is_valid = cpu_port_is_valid(&cpu_port[cpu]);
637 if (is_valid && cpu_port_match(&cpu_port[cpu], mpidr)) {
638 cci_port_control(cpu_port[cpu].port, false);
639 return 0;
640 }
641 }
642 return -ENODEV;
643}
644EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu);
645
646/**
Nicolas Pitreab28e082013-05-21 23:34:41 -0400647 * cci_enable_port_for_self() - enable a CCI port for calling CPU
648 *
649 * Enabling a CCI port for the calling CPU implies enabling the CCI
650 * port controlling that CPU's cluster. Caller must make sure that the
651 * CPU running the code is the first active CPU in the cluster and all
652 * other CPUs are quiescent in a low power state or waiting for this CPU
653 * to complete the CCI initialization.
654 *
655 * Because this is called when the MMU is still off and with no stack,
656 * the code must be position independent and ideally rely on callee
657 * clobbered registers only. To achieve this we must code this function
658 * entirely in assembler.
659 *
660 * On success this returns with the proper CCI port enabled. In case of
661 * any failure this never returns as the inability to enable the CCI is
662 * fatal and there is no possible recovery at this stage.
663 */
664asmlinkage void __naked cci_enable_port_for_self(void)
665{
666 asm volatile ("\n"
667
668" mrc p15, 0, r0, c0, c0, 5 @ get MPIDR value \n"
669" and r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n"
670" adr r1, 5f \n"
671" ldr r2, [r1] \n"
672" add r1, r1, r2 @ &cpu_port \n"
673" add ip, r1, %[sizeof_cpu_port] \n"
674
675 /* Loop over the cpu_port array looking for a matching MPIDR */
676"1: ldr r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
677" cmp r2, r0 @ compare MPIDR \n"
678" bne 2f \n"
679
680 /* Found a match, now test port validity */
681" ldr r3, [r1, %[offsetof_cpu_port_port]] \n"
682" tst r3, #"__stringify(PORT_VALID)" \n"
683" bne 3f \n"
684
685 /* no match, loop with the next cpu_port entry */
686"2: add r1, r1, %[sizeof_struct_cpu_port] \n"
687" cmp r1, ip @ done? \n"
688" blo 1b \n"
689
690 /* CCI port not found -- cheaply try to stall this CPU */
691"cci_port_not_found: \n"
692" wfi \n"
693" wfe \n"
694" b cci_port_not_found \n"
695
696 /* Use matched port index to look up the corresponding ports entry */
697"3: bic r3, r3, #"__stringify(PORT_VALID)" \n"
698" adr r0, 6f \n"
699" ldmia r0, {r1, r2} \n"
700" sub r1, r1, r0 @ virt - phys \n"
701" ldr r0, [r0, r2] @ *(&ports) \n"
702" mov r2, %[sizeof_struct_ace_port] \n"
703" mla r0, r2, r3, r0 @ &ports[index] \n"
704" sub r0, r0, r1 @ virt_to_phys() \n"
705
706 /* Enable the CCI port */
707" ldr r0, [r0, %[offsetof_port_phys]] \n"
Victor Kamenskyeb192462013-10-15 21:50:34 -0700708" mov r3, %[cci_enable_req]\n"
Nicolas Pitreab28e082013-05-21 23:34:41 -0400709" str r3, [r0, #"__stringify(CCI_PORT_CTRL)"] \n"
710
711 /* poll the status reg for completion */
712" adr r1, 7f \n"
713" ldr r0, [r1] \n"
714" ldr r0, [r0, r1] @ cci_ctrl_base \n"
715"4: ldr r1, [r0, #"__stringify(CCI_CTRL_STATUS)"] \n"
Victor Kamenskyeb192462013-10-15 21:50:34 -0700716" tst r1, %[cci_control_status_bits] \n"
Nicolas Pitreab28e082013-05-21 23:34:41 -0400717" bne 4b \n"
718
719" mov r0, #0 \n"
720" bx lr \n"
721
722" .align 2 \n"
723"5: .word cpu_port - . \n"
724"6: .word . \n"
725" .word ports - 6b \n"
726"7: .word cci_ctrl_phys - . \n"
727 : :
728 [sizeof_cpu_port] "i" (sizeof(cpu_port)),
Victor Kamenskyeb192462013-10-15 21:50:34 -0700729 [cci_enable_req] "i" cpu_to_le32(CCI_ENABLE_REQ),
730 [cci_control_status_bits] "i" cpu_to_le32(1),
Nicolas Pitreab28e082013-05-21 23:34:41 -0400731#ifndef __ARMEB__
732 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
733#else
734 [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
735#endif
736 [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
737 [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
738 [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
739 [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
740
741 unreachable();
742}
743
744/**
Lorenzo Pieralisie53b9232012-07-13 15:55:52 +0100745 * __cci_control_port_by_device() - function to control a CCI port by device
746 * reference
747 *
748 * @dn: device node pointer of the device whose CCI port should be
749 * controlled
750 * @enable: if true enables the port, if false disables it
751 *
752 * Return:
753 * 0 on success
754 * -ENODEV on port look-up failure
755 */
756int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
757{
758 int port;
759
760 if (!dn)
761 return -ENODEV;
762
763 port = __cci_ace_get_port(dn, ACE_LITE_PORT);
764 if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n",
765 dn->full_name))
766 return -ENODEV;
767 cci_port_control(port, enable);
768 return 0;
769}
770EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
771
772/**
773 * __cci_control_port_by_index() - function to control a CCI port by port index
774 *
775 * @port: port index previously retrieved with cci_ace_get_port()
776 * @enable: if true enables the port, if false disables it
777 *
778 * Return:
779 * 0 on success
780 * -ENODEV on port index out of range
781 * -EPERM if operation carried out on an ACE PORT
782 */
783int notrace __cci_control_port_by_index(u32 port, bool enable)
784{
785 if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
786 return -ENODEV;
787 /*
788 * CCI control for ports connected to CPUS is extremely fragile
789 * and must be made to go through a specific and controlled
790 * interface (ie cci_disable_port_by_cpu(); control by general purpose
791 * indexing is therefore disabled for ACE ports.
792 */
793 if (ports[port].type == ACE_PORT)
794 return -EPERM;
795
796 cci_port_control(port, enable);
797 return 0;
798}
799EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
800
801static const struct cci_nb_ports cci400_ports = {
802 .nb_ace = 2,
803 .nb_ace_lite = 3
804};
805
806static const struct of_device_id arm_cci_matches[] = {
807 {.compatible = "arm,cci-400", .data = &cci400_ports },
808 {},
809};
810
811static const struct of_device_id arm_cci_ctrl_if_matches[] = {
812 {.compatible = "arm,cci-400-ctrl-if", },
813 {},
814};
815
816static int __init cci_probe(void)
817{
818 struct cci_nb_ports const *cci_config;
819 int ret, i, nb_ace = 0, nb_ace_lite = 0;
820 struct device_node *np, *cp;
Nicolas Pitreab28e082013-05-21 23:34:41 -0400821 struct resource res;
Lorenzo Pieralisie53b9232012-07-13 15:55:52 +0100822 const char *match_str;
823 bool is_ace;
824
825 np = of_find_matching_node(NULL, arm_cci_matches);
826 if (!np)
827 return -ENODEV;
828
829 cci_config = of_match_node(arm_cci_matches, np)->data;
830 if (!cci_config)
831 return -ENODEV;
832
833 nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
834
835 ports = kcalloc(sizeof(*ports), nb_cci_ports, GFP_KERNEL);
836 if (!ports)
837 return -ENOMEM;
838
Nicolas Pitreab28e082013-05-21 23:34:41 -0400839 ret = of_address_to_resource(np, 0, &res);
840 if (!ret) {
841 cci_ctrl_base = ioremap(res.start, resource_size(&res));
842 cci_ctrl_phys = res.start;
843 }
844 if (ret || !cci_ctrl_base) {
Lorenzo Pieralisie53b9232012-07-13 15:55:52 +0100845 WARN(1, "unable to ioremap CCI ctrl\n");
846 ret = -ENXIO;
847 goto memalloc_err;
848 }
849
850 for_each_child_of_node(np, cp) {
851 if (!of_match_node(arm_cci_ctrl_if_matches, cp))
852 continue;
853
854 i = nb_ace + nb_ace_lite;
855
856 if (i >= nb_cci_ports)
857 break;
858
859 if (of_property_read_string(cp, "interface-type",
860 &match_str)) {
861 WARN(1, "node %s missing interface-type property\n",
862 cp->full_name);
863 continue;
864 }
865 is_ace = strcmp(match_str, "ace") == 0;
866 if (!is_ace && strcmp(match_str, "ace-lite")) {
867 WARN(1, "node %s containing invalid interface-type property, skipping it\n",
868 cp->full_name);
869 continue;
870 }
871
Nicolas Pitreab28e082013-05-21 23:34:41 -0400872 ret = of_address_to_resource(cp, 0, &res);
873 if (!ret) {
874 ports[i].base = ioremap(res.start, resource_size(&res));
875 ports[i].phys = res.start;
876 }
877 if (ret || !ports[i].base) {
Lorenzo Pieralisie53b9232012-07-13 15:55:52 +0100878 WARN(1, "unable to ioremap CCI port %d\n", i);
879 continue;
880 }
881
882 if (is_ace) {
883 if (WARN_ON(nb_ace >= cci_config->nb_ace))
884 continue;
885 ports[i].type = ACE_PORT;
886 ++nb_ace;
887 } else {
888 if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
889 continue;
890 ports[i].type = ACE_LITE_PORT;
891 ++nb_ace_lite;
892 }
893 ports[i].dn = cp;
894 }
895
896 /* initialize a stashed array of ACE ports to speed-up look-up */
897 cci_ace_init_ports();
898
899 /*
900 * Multi-cluster systems may need this data when non-coherent, during
901 * cluster power-up/power-down. Make sure it reaches main memory.
902 */
903 sync_cache_w(&cci_ctrl_base);
Nicolas Pitreab28e082013-05-21 23:34:41 -0400904 sync_cache_w(&cci_ctrl_phys);
Lorenzo Pieralisie53b9232012-07-13 15:55:52 +0100905 sync_cache_w(&ports);
906 sync_cache_w(&cpu_port);
907 __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
908 pr_info("ARM CCI driver probed\n");
909 return 0;
910
911memalloc_err:
912
913 kfree(ports);
914 return ret;
915}
916
917static int cci_init_status = -EAGAIN;
918static DEFINE_MUTEX(cci_probing);
919
920static int __init cci_init(void)
921{
922 if (cci_init_status != -EAGAIN)
923 return cci_init_status;
924
925 mutex_lock(&cci_probing);
926 if (cci_init_status == -EAGAIN)
927 cci_init_status = cci_probe();
928 mutex_unlock(&cci_probing);
929 return cci_init_status;
930}
931
932/*
933 * To sort out early init calls ordering a helper function is provided to
934 * check if the CCI driver has beed initialized. Function check if the driver
935 * has been initialized, if not it calls the init function that probes
936 * the driver and updates the return value.
937 */
938bool __init cci_probed(void)
939{
940 return cci_init() == 0;
941}
942EXPORT_SYMBOL_GPL(cci_probed);
943
944early_initcall(cci_init);
Jon Medhurst0b9bd1c2013-05-10 14:47:27 +0100945core_initcall(cci_pmu_init);
Lorenzo Pieralisie53b9232012-07-13 15:55:52 +0100946MODULE_LICENSE("GPL");
947MODULE_DESCRIPTION("ARM CCI support");