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>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/of_address.h>
22 #include <linux/of_irq.h>
23 #include <linux/of_platform.h>
24 #include <linux/perf_event.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
29 #include <asm/cacheflush.h>
30 #include <asm/smp_plat.h>
32 static void __iomem
*cci_ctrl_base
;
33 static unsigned long cci_ctrl_phys
;
35 #ifdef CONFIG_ARM_CCI400_PORT_CTRL
38 unsigned int nb_ace_lite
;
41 static const struct cci_nb_ports cci400_ports
= {
46 #define CCI400_PORTS_DATA (&cci400_ports)
48 #define CCI400_PORTS_DATA (NULL)
51 static const struct of_device_id arm_cci_matches
[] = {
52 #ifdef CONFIG_ARM_CCI400_COMMON
53 {.compatible
= "arm,cci-400", .data
= CCI400_PORTS_DATA
},
58 #ifdef CONFIG_ARM_CCI400_PMU
60 #define DRIVER_NAME "CCI-400"
61 #define DRIVER_NAME_PMU DRIVER_NAME " PMU"
63 #define CCI_PMCR 0x0100
64 #define CCI_PID2 0x0fe8
66 #define CCI_PMCR_CEN 0x00000001
67 #define CCI_PMCR_NCNT_MASK 0x0000f800
68 #define CCI_PMCR_NCNT_SHIFT 11
70 #define CCI_PID2_REV_MASK 0xf0
71 #define CCI_PID2_REV_SHIFT 4
73 #define CCI_PMU_EVT_SEL 0x000
74 #define CCI_PMU_CNTR 0x004
75 #define CCI_PMU_CNTR_CTRL 0x008
76 #define CCI_PMU_OVRFLW 0x00c
78 #define CCI_PMU_OVRFLW_FLAG 1
80 #define CCI_PMU_CNTR_BASE(idx) ((idx) * SZ_4K)
82 #define CCI_PMU_CNTR_MASK ((1ULL << 32) -1)
84 #define CCI_PMU_EVENT_MASK 0xffUL
85 #define CCI_PMU_EVENT_SOURCE(event) ((event >> 5) & 0x7)
86 #define CCI_PMU_EVENT_CODE(event) (event & 0x1f)
88 #define CCI_PMU_MAX_HW_EVENTS 5 /* CCI PMU has 4 counters + 1 cycle counter */
90 /* Types of interfaces that can generate events */
102 struct cci_pmu_hw_events
{
103 struct perf_event
*events
[CCI_PMU_MAX_HW_EVENTS
];
104 unsigned long used_mask
[BITS_TO_LONGS(CCI_PMU_MAX_HW_EVENTS
)];
105 raw_spinlock_t pmu_lock
;
108 struct cci_pmu_model
{
110 struct event_range event_ranges
[CCI_IF_MAX
];
113 static struct cci_pmu_model cci_pmu_models
[];
119 int irqs
[CCI_PMU_MAX_HW_EVENTS
];
120 unsigned long active_irqs
;
121 const struct cci_pmu_model
*model
;
122 struct cci_pmu_hw_events hw_events
;
123 struct platform_device
*plat_device
;
125 atomic_t active_events
;
126 struct mutex reserve_mutex
;
129 static struct cci_pmu
*pmu
;
131 #define to_cci_pmu(c) (container_of(c, struct cci_pmu, pmu))
134 #define CCI_PORT_S0 0
135 #define CCI_PORT_S1 1
136 #define CCI_PORT_S2 2
137 #define CCI_PORT_S3 3
138 #define CCI_PORT_S4 4
139 #define CCI_PORT_M0 5
140 #define CCI_PORT_M1 6
141 #define CCI_PORT_M2 7
145 #define CCI_REV_R1_PX 5
148 * Instead of an event id to monitor CCI cycles, a dedicated counter is
149 * provided. Use 0xff to represent CCI cycles and hope that no future revisions
150 * make use of this event in hardware.
152 enum cci400_perf_events
{
153 CCI_PMU_CYCLES
= 0xff
156 #define CCI_PMU_CYCLE_CNTR_IDX 0
157 #define CCI_PMU_CNTR0_IDX 1
158 #define CCI_PMU_CNTR_LAST(cci_pmu) (CCI_PMU_CYCLE_CNTR_IDX + cci_pmu->num_events - 1)
161 * CCI PMU event id is an 8-bit value made of two parts - bits 7:5 for one of 8
162 * ports and bits 4:0 are event codes. There are different event codes
163 * associated with each port type.
165 * Additionally, the range of events associated with the port types changed
166 * between Rev0 and Rev1.
168 * The constants below define the range of valid codes for each port type for
169 * the different revisions and are used to validate the event to be monitored.
172 #define CCI_REV_R0_SLAVE_PORT_MIN_EV 0x00
173 #define CCI_REV_R0_SLAVE_PORT_MAX_EV 0x13
174 #define CCI_REV_R0_MASTER_PORT_MIN_EV 0x14
175 #define CCI_REV_R0_MASTER_PORT_MAX_EV 0x1a
177 #define CCI_REV_R1_SLAVE_PORT_MIN_EV 0x00
178 #define CCI_REV_R1_SLAVE_PORT_MAX_EV 0x14
179 #define CCI_REV_R1_MASTER_PORT_MIN_EV 0x00
180 #define CCI_REV_R1_MASTER_PORT_MAX_EV 0x11
182 static int pmu_validate_hw_event(unsigned long hw_event
)
184 u8 ev_source
= CCI_PMU_EVENT_SOURCE(hw_event
);
185 u8 ev_code
= CCI_PMU_EVENT_CODE(hw_event
);
188 if (hw_event
& ~CCI_PMU_EVENT_MASK
)
197 /* Slave Interface */
198 if_type
= CCI_IF_SLAVE
;
203 /* Master Interface */
204 if_type
= CCI_IF_MASTER
;
210 if (ev_code
>= pmu
->model
->event_ranges
[if_type
].min
&&
211 ev_code
<= pmu
->model
->event_ranges
[if_type
].max
)
217 static int probe_cci_revision(void)
220 rev
= readl_relaxed(cci_ctrl_base
+ CCI_PID2
) & CCI_PID2_REV_MASK
;
221 rev
>>= CCI_PID2_REV_SHIFT
;
223 if (rev
< CCI_REV_R1_PX
)
229 static const struct cci_pmu_model
*probe_cci_model(struct platform_device
*pdev
)
231 if (platform_has_secure_cci_access())
232 return &cci_pmu_models
[probe_cci_revision()];
236 static int pmu_is_valid_counter(struct cci_pmu
*cci_pmu
, int idx
)
238 return CCI_PMU_CYCLE_CNTR_IDX
<= idx
&&
239 idx
<= CCI_PMU_CNTR_LAST(cci_pmu
);
242 static u32
pmu_read_register(int idx
, unsigned int offset
)
244 return readl_relaxed(pmu
->base
+ CCI_PMU_CNTR_BASE(idx
) + offset
);
247 static void pmu_write_register(u32 value
, int idx
, unsigned int offset
)
249 return writel_relaxed(value
, pmu
->base
+ CCI_PMU_CNTR_BASE(idx
) + offset
);
252 static void pmu_disable_counter(int idx
)
254 pmu_write_register(0, idx
, CCI_PMU_CNTR_CTRL
);
257 static void pmu_enable_counter(int idx
)
259 pmu_write_register(1, idx
, CCI_PMU_CNTR_CTRL
);
262 static void pmu_set_event(int idx
, unsigned long event
)
264 pmu_write_register(event
, idx
, CCI_PMU_EVT_SEL
);
267 static u32
pmu_get_max_counters(void)
269 u32 n_cnts
= (readl_relaxed(cci_ctrl_base
+ CCI_PMCR
) &
270 CCI_PMCR_NCNT_MASK
) >> CCI_PMCR_NCNT_SHIFT
;
272 /* add 1 for cycle counter */
276 static int pmu_get_event_idx(struct cci_pmu_hw_events
*hw
, struct perf_event
*event
)
278 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
279 struct hw_perf_event
*hw_event
= &event
->hw
;
280 unsigned long cci_event
= hw_event
->config_base
;
283 if (cci_event
== CCI_PMU_CYCLES
) {
284 if (test_and_set_bit(CCI_PMU_CYCLE_CNTR_IDX
, hw
->used_mask
))
287 return CCI_PMU_CYCLE_CNTR_IDX
;
290 for (idx
= CCI_PMU_CNTR0_IDX
; idx
<= CCI_PMU_CNTR_LAST(cci_pmu
); ++idx
)
291 if (!test_and_set_bit(idx
, hw
->used_mask
))
294 /* No counters available */
298 static int pmu_map_event(struct perf_event
*event
)
301 unsigned long config
= event
->attr
.config
;
303 if (event
->attr
.type
< PERF_TYPE_MAX
)
306 if (config
== CCI_PMU_CYCLES
)
309 mapping
= pmu_validate_hw_event(config
);
314 static int pmu_request_irq(struct cci_pmu
*cci_pmu
, irq_handler_t handler
)
317 struct platform_device
*pmu_device
= cci_pmu
->plat_device
;
319 if (unlikely(!pmu_device
))
322 if (pmu
->nr_irqs
< 1) {
323 dev_err(&pmu_device
->dev
, "no irqs for CCI PMUs defined\n");
328 * Register all available CCI PMU interrupts. In the interrupt handler
329 * we iterate over the counters checking for interrupt source (the
330 * overflowing counter) and clear it.
332 * This should allow handling of non-unique interrupt for the counters.
334 for (i
= 0; i
< pmu
->nr_irqs
; i
++) {
335 int err
= request_irq(pmu
->irqs
[i
], handler
, IRQF_SHARED
,
336 "arm-cci-pmu", cci_pmu
);
338 dev_err(&pmu_device
->dev
, "unable to request IRQ%d for ARM CCI PMU counters\n",
343 set_bit(i
, &pmu
->active_irqs
);
349 static void pmu_free_irq(struct cci_pmu
*cci_pmu
)
353 for (i
= 0; i
< pmu
->nr_irqs
; i
++) {
354 if (!test_and_clear_bit(i
, &pmu
->active_irqs
))
357 free_irq(pmu
->irqs
[i
], cci_pmu
);
361 static u32
pmu_read_counter(struct perf_event
*event
)
363 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
364 struct hw_perf_event
*hw_counter
= &event
->hw
;
365 int idx
= hw_counter
->idx
;
368 if (unlikely(!pmu_is_valid_counter(cci_pmu
, idx
))) {
369 dev_err(&cci_pmu
->plat_device
->dev
, "Invalid CCI PMU counter %d\n", idx
);
372 value
= pmu_read_register(idx
, CCI_PMU_CNTR
);
377 static void pmu_write_counter(struct perf_event
*event
, u32 value
)
379 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
380 struct hw_perf_event
*hw_counter
= &event
->hw
;
381 int idx
= hw_counter
->idx
;
383 if (unlikely(!pmu_is_valid_counter(cci_pmu
, idx
)))
384 dev_err(&cci_pmu
->plat_device
->dev
, "Invalid CCI PMU counter %d\n", idx
);
386 pmu_write_register(value
, idx
, CCI_PMU_CNTR
);
389 static u64
pmu_event_update(struct perf_event
*event
)
391 struct hw_perf_event
*hwc
= &event
->hw
;
392 u64 delta
, prev_raw_count
, new_raw_count
;
395 prev_raw_count
= local64_read(&hwc
->prev_count
);
396 new_raw_count
= pmu_read_counter(event
);
397 } while (local64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
398 new_raw_count
) != prev_raw_count
);
400 delta
= (new_raw_count
- prev_raw_count
) & CCI_PMU_CNTR_MASK
;
402 local64_add(delta
, &event
->count
);
404 return new_raw_count
;
407 static void pmu_read(struct perf_event
*event
)
409 pmu_event_update(event
);
412 void pmu_event_set_period(struct perf_event
*event
)
414 struct hw_perf_event
*hwc
= &event
->hw
;
416 * The CCI PMU counters have a period of 2^32. To account for the
417 * possiblity of extreme interrupt latency we program for a period of
418 * half that. Hopefully we can handle the interrupt before another 2^31
419 * events occur and the counter overtakes its previous value.
421 u64 val
= 1ULL << 31;
422 local64_set(&hwc
->prev_count
, val
);
423 pmu_write_counter(event
, val
);
426 static irqreturn_t
pmu_handle_irq(int irq_num
, void *dev
)
429 struct cci_pmu
*cci_pmu
= dev
;
430 struct cci_pmu_hw_events
*events
= &pmu
->hw_events
;
431 int idx
, handled
= IRQ_NONE
;
433 raw_spin_lock_irqsave(&events
->pmu_lock
, flags
);
435 * Iterate over counters and update the corresponding perf events.
436 * This should work regardless of whether we have per-counter overflow
437 * interrupt or a combined overflow interrupt.
439 for (idx
= CCI_PMU_CYCLE_CNTR_IDX
; idx
<= CCI_PMU_CNTR_LAST(cci_pmu
); idx
++) {
440 struct perf_event
*event
= events
->events
[idx
];
441 struct hw_perf_event
*hw_counter
;
446 hw_counter
= &event
->hw
;
448 /* Did this counter overflow? */
449 if (!(pmu_read_register(idx
, CCI_PMU_OVRFLW
) &
450 CCI_PMU_OVRFLW_FLAG
))
453 pmu_write_register(CCI_PMU_OVRFLW_FLAG
, idx
, CCI_PMU_OVRFLW
);
455 pmu_event_update(event
);
456 pmu_event_set_period(event
);
457 handled
= IRQ_HANDLED
;
459 raw_spin_unlock_irqrestore(&events
->pmu_lock
, flags
);
461 return IRQ_RETVAL(handled
);
464 static int cci_pmu_get_hw(struct cci_pmu
*cci_pmu
)
466 int ret
= pmu_request_irq(cci_pmu
, pmu_handle_irq
);
468 pmu_free_irq(cci_pmu
);
474 static void cci_pmu_put_hw(struct cci_pmu
*cci_pmu
)
476 pmu_free_irq(cci_pmu
);
479 static void hw_perf_event_destroy(struct perf_event
*event
)
481 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
482 atomic_t
*active_events
= &cci_pmu
->active_events
;
483 struct mutex
*reserve_mutex
= &cci_pmu
->reserve_mutex
;
485 if (atomic_dec_and_mutex_lock(active_events
, reserve_mutex
)) {
486 cci_pmu_put_hw(cci_pmu
);
487 mutex_unlock(reserve_mutex
);
491 static void cci_pmu_enable(struct pmu
*pmu
)
493 struct cci_pmu
*cci_pmu
= to_cci_pmu(pmu
);
494 struct cci_pmu_hw_events
*hw_events
= &cci_pmu
->hw_events
;
495 int enabled
= bitmap_weight(hw_events
->used_mask
, cci_pmu
->num_events
);
502 raw_spin_lock_irqsave(&hw_events
->pmu_lock
, flags
);
504 /* Enable all the PMU counters. */
505 val
= readl_relaxed(cci_ctrl_base
+ CCI_PMCR
) | CCI_PMCR_CEN
;
506 writel(val
, cci_ctrl_base
+ CCI_PMCR
);
507 raw_spin_unlock_irqrestore(&hw_events
->pmu_lock
, flags
);
511 static void cci_pmu_disable(struct pmu
*pmu
)
513 struct cci_pmu
*cci_pmu
= to_cci_pmu(pmu
);
514 struct cci_pmu_hw_events
*hw_events
= &cci_pmu
->hw_events
;
518 raw_spin_lock_irqsave(&hw_events
->pmu_lock
, flags
);
520 /* Disable all the PMU counters. */
521 val
= readl_relaxed(cci_ctrl_base
+ CCI_PMCR
) & ~CCI_PMCR_CEN
;
522 writel(val
, cci_ctrl_base
+ CCI_PMCR
);
523 raw_spin_unlock_irqrestore(&hw_events
->pmu_lock
, flags
);
526 static void cci_pmu_start(struct perf_event
*event
, int pmu_flags
)
528 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
529 struct cci_pmu_hw_events
*hw_events
= &cci_pmu
->hw_events
;
530 struct hw_perf_event
*hwc
= &event
->hw
;
535 * To handle interrupt latency, we always reprogram the period
536 * regardlesss of PERF_EF_RELOAD.
538 if (pmu_flags
& PERF_EF_RELOAD
)
539 WARN_ON_ONCE(!(hwc
->state
& PERF_HES_UPTODATE
));
543 if (unlikely(!pmu_is_valid_counter(cci_pmu
, idx
))) {
544 dev_err(&cci_pmu
->plat_device
->dev
, "Invalid CCI PMU counter %d\n", idx
);
548 raw_spin_lock_irqsave(&hw_events
->pmu_lock
, flags
);
550 /* Configure the event to count, unless you are counting cycles */
551 if (idx
!= CCI_PMU_CYCLE_CNTR_IDX
)
552 pmu_set_event(idx
, hwc
->config_base
);
554 pmu_event_set_period(event
);
555 pmu_enable_counter(idx
);
557 raw_spin_unlock_irqrestore(&hw_events
->pmu_lock
, flags
);
560 static void cci_pmu_stop(struct perf_event
*event
, int pmu_flags
)
562 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
563 struct hw_perf_event
*hwc
= &event
->hw
;
566 if (hwc
->state
& PERF_HES_STOPPED
)
569 if (unlikely(!pmu_is_valid_counter(cci_pmu
, idx
))) {
570 dev_err(&cci_pmu
->plat_device
->dev
, "Invalid CCI PMU counter %d\n", idx
);
575 * We always reprogram the counter, so ignore PERF_EF_UPDATE. See
578 pmu_disable_counter(idx
);
579 pmu_event_update(event
);
580 hwc
->state
|= PERF_HES_STOPPED
| PERF_HES_UPTODATE
;
583 static int cci_pmu_add(struct perf_event
*event
, int flags
)
585 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
586 struct cci_pmu_hw_events
*hw_events
= &cci_pmu
->hw_events
;
587 struct hw_perf_event
*hwc
= &event
->hw
;
591 perf_pmu_disable(event
->pmu
);
593 /* If we don't have a space for the counter then finish early. */
594 idx
= pmu_get_event_idx(hw_events
, event
);
601 hw_events
->events
[idx
] = event
;
603 hwc
->state
= PERF_HES_STOPPED
| PERF_HES_UPTODATE
;
604 if (flags
& PERF_EF_START
)
605 cci_pmu_start(event
, PERF_EF_RELOAD
);
607 /* Propagate our changes to the userspace mapping. */
608 perf_event_update_userpage(event
);
611 perf_pmu_enable(event
->pmu
);
615 static void cci_pmu_del(struct perf_event
*event
, int flags
)
617 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
618 struct cci_pmu_hw_events
*hw_events
= &cci_pmu
->hw_events
;
619 struct hw_perf_event
*hwc
= &event
->hw
;
622 cci_pmu_stop(event
, PERF_EF_UPDATE
);
623 hw_events
->events
[idx
] = NULL
;
624 clear_bit(idx
, hw_events
->used_mask
);
626 perf_event_update_userpage(event
);
630 validate_event(struct pmu
*cci_pmu
,
631 struct cci_pmu_hw_events
*hw_events
,
632 struct perf_event
*event
)
634 if (is_software_event(event
))
638 * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
639 * core perf code won't check that the pmu->ctx == leader->ctx
640 * until after pmu->event_init(event).
642 if (event
->pmu
!= cci_pmu
)
645 if (event
->state
< PERF_EVENT_STATE_OFF
)
648 if (event
->state
== PERF_EVENT_STATE_OFF
&& !event
->attr
.enable_on_exec
)
651 return pmu_get_event_idx(hw_events
, event
) >= 0;
655 validate_group(struct perf_event
*event
)
657 struct perf_event
*sibling
, *leader
= event
->group_leader
;
658 struct cci_pmu_hw_events fake_pmu
= {
660 * Initialise the fake PMU. We only need to populate the
661 * used_mask for the purposes of validation.
666 if (!validate_event(event
->pmu
, &fake_pmu
, leader
))
669 list_for_each_entry(sibling
, &leader
->sibling_list
, group_entry
) {
670 if (!validate_event(event
->pmu
, &fake_pmu
, sibling
))
674 if (!validate_event(event
->pmu
, &fake_pmu
, event
))
681 __hw_perf_event_init(struct perf_event
*event
)
683 struct hw_perf_event
*hwc
= &event
->hw
;
686 mapping
= pmu_map_event(event
);
689 pr_debug("event %x:%llx not supported\n", event
->attr
.type
,
695 * We don't assign an index until we actually place the event onto
696 * hardware. Use -1 to signify that we haven't decided where to put it
700 hwc
->config_base
= 0;
705 * Store the event encoding into the config_base field.
707 hwc
->config_base
|= (unsigned long)mapping
;
710 * Limit the sample_period to half of the counter width. That way, the
711 * new counter value is far less likely to overtake the previous one
712 * unless you have some serious IRQ latency issues.
714 hwc
->sample_period
= CCI_PMU_CNTR_MASK
>> 1;
715 hwc
->last_period
= hwc
->sample_period
;
716 local64_set(&hwc
->period_left
, hwc
->sample_period
);
718 if (event
->group_leader
!= event
) {
719 if (validate_group(event
) != 0)
726 static int cci_pmu_event_init(struct perf_event
*event
)
728 struct cci_pmu
*cci_pmu
= to_cci_pmu(event
->pmu
);
729 atomic_t
*active_events
= &cci_pmu
->active_events
;
733 if (event
->attr
.type
!= event
->pmu
->type
)
736 /* Shared by all CPUs, no meaningful state to sample */
737 if (is_sampling_event(event
) || event
->attach_state
& PERF_ATTACH_TASK
)
740 /* We have no filtering of any kind */
741 if (event
->attr
.exclude_user
||
742 event
->attr
.exclude_kernel
||
743 event
->attr
.exclude_hv
||
744 event
->attr
.exclude_idle
||
745 event
->attr
.exclude_host
||
746 event
->attr
.exclude_guest
)
750 * Following the example set by other "uncore" PMUs, we accept any CPU
751 * and rewrite its affinity dynamically rather than having perf core
752 * handle cpu == -1 and pid == -1 for this case.
754 * The perf core will pin online CPUs for the duration of this call and
755 * the event being installed into its context, so the PMU's CPU can't
756 * change under our feet.
758 cpu
= cpumask_first(&cci_pmu
->cpus
);
759 if (event
->cpu
< 0 || cpu
< 0)
763 event
->destroy
= hw_perf_event_destroy
;
764 if (!atomic_inc_not_zero(active_events
)) {
765 mutex_lock(&cci_pmu
->reserve_mutex
);
766 if (atomic_read(active_events
) == 0)
767 err
= cci_pmu_get_hw(cci_pmu
);
769 atomic_inc(active_events
);
770 mutex_unlock(&cci_pmu
->reserve_mutex
);
775 err
= __hw_perf_event_init(event
);
777 hw_perf_event_destroy(event
);
782 static ssize_t
pmu_attr_cpumask_show(struct device
*dev
,
783 struct device_attribute
*attr
, char *buf
)
785 int n
= scnprintf(buf
, PAGE_SIZE
- 1, "%*pbl",
786 cpumask_pr_args(&pmu
->cpus
));
792 static DEVICE_ATTR(cpumask
, S_IRUGO
, pmu_attr_cpumask_show
, NULL
);
794 static struct attribute
*pmu_attrs
[] = {
795 &dev_attr_cpumask
.attr
,
799 static struct attribute_group pmu_attr_group
= {
803 static const struct attribute_group
*pmu_attr_groups
[] = {
808 static int cci_pmu_init(struct cci_pmu
*cci_pmu
, struct platform_device
*pdev
)
810 char *name
= cci_pmu
->model
->name
;
811 cci_pmu
->pmu
= (struct pmu
) {
812 .name
= cci_pmu
->model
->name
,
813 .task_ctx_nr
= perf_invalid_context
,
814 .pmu_enable
= cci_pmu_enable
,
815 .pmu_disable
= cci_pmu_disable
,
816 .event_init
= cci_pmu_event_init
,
819 .start
= cci_pmu_start
,
820 .stop
= cci_pmu_stop
,
822 .attr_groups
= pmu_attr_groups
,
825 cci_pmu
->plat_device
= pdev
;
826 cci_pmu
->num_events
= pmu_get_max_counters();
828 return perf_pmu_register(&cci_pmu
->pmu
, name
, -1);
831 static int cci_pmu_cpu_notifier(struct notifier_block
*self
,
832 unsigned long action
, void *hcpu
)
834 unsigned int cpu
= (long)hcpu
;
837 switch (action
& ~CPU_TASKS_FROZEN
) {
838 case CPU_DOWN_PREPARE
:
839 if (!cpumask_test_and_clear_cpu(cpu
, &pmu
->cpus
))
841 target
= cpumask_any_but(cpu_online_mask
, cpu
);
842 if (target
< 0) // UP, last CPU
845 * TODO: migrate context once core races on event->ctx have
848 cpumask_set_cpu(target
, &pmu
->cpus
);
856 static struct notifier_block cci_pmu_cpu_nb
= {
857 .notifier_call
= cci_pmu_cpu_notifier
,
859 * to migrate uncore events, our notifier should be executed
860 * before perf core's notifier.
862 .priority
= CPU_PRI_PERF
+ 1,
865 static struct cci_pmu_model cci_pmu_models
[] = {
870 CCI_REV_R0_SLAVE_PORT_MIN_EV
,
871 CCI_REV_R0_SLAVE_PORT_MAX_EV
,
874 CCI_REV_R0_MASTER_PORT_MIN_EV
,
875 CCI_REV_R0_MASTER_PORT_MAX_EV
,
880 .name
= "CCI_400_r1",
883 CCI_REV_R1_SLAVE_PORT_MIN_EV
,
884 CCI_REV_R1_SLAVE_PORT_MAX_EV
,
887 CCI_REV_R1_MASTER_PORT_MIN_EV
,
888 CCI_REV_R1_MASTER_PORT_MAX_EV
,
894 static const struct of_device_id arm_cci_pmu_matches
[] = {
896 .compatible
= "arm,cci-400-pmu",
900 .compatible
= "arm,cci-400-pmu,r0",
901 .data
= &cci_pmu_models
[CCI_REV_R0
],
904 .compatible
= "arm,cci-400-pmu,r1",
905 .data
= &cci_pmu_models
[CCI_REV_R1
],
910 static inline const struct cci_pmu_model
*get_cci_model(struct platform_device
*pdev
)
912 const struct of_device_id
*match
= of_match_node(arm_cci_pmu_matches
,
919 dev_warn(&pdev
->dev
, "DEPRECATED compatible property,"
920 "requires secure access to CCI registers");
921 return probe_cci_model(pdev
);
924 static bool is_duplicate_irq(int irq
, int *irqs
, int nr_irqs
)
928 for (i
= 0; i
< nr_irqs
; i
++)
935 static int cci_pmu_probe(struct platform_device
*pdev
)
937 struct resource
*res
;
939 const struct cci_pmu_model
*model
;
941 model
= get_cci_model(pdev
);
943 dev_warn(&pdev
->dev
, "CCI PMU version not supported\n");
947 pmu
= devm_kzalloc(&pdev
->dev
, sizeof(*pmu
), GFP_KERNEL
);
952 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
953 pmu
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
954 if (IS_ERR(pmu
->base
))
958 * CCI PMU has 5 overflow signals - one per counter; but some may be tied
959 * together to a common interrupt.
962 for (i
= 0; i
< CCI_PMU_MAX_HW_EVENTS
; i
++) {
963 irq
= platform_get_irq(pdev
, i
);
967 if (is_duplicate_irq(irq
, pmu
->irqs
, pmu
->nr_irqs
))
970 pmu
->irqs
[pmu
->nr_irqs
++] = irq
;
974 * Ensure that the device tree has as many interrupts as the number
977 if (i
< CCI_PMU_MAX_HW_EVENTS
) {
978 dev_warn(&pdev
->dev
, "In-correct number of interrupts: %d, should be %d\n",
979 i
, CCI_PMU_MAX_HW_EVENTS
);
983 raw_spin_lock_init(&pmu
->hw_events
.pmu_lock
);
984 mutex_init(&pmu
->reserve_mutex
);
985 atomic_set(&pmu
->active_events
, 0);
986 cpumask_set_cpu(smp_processor_id(), &pmu
->cpus
);
988 ret
= register_cpu_notifier(&cci_pmu_cpu_nb
);
992 ret
= cci_pmu_init(pmu
, pdev
);
996 pr_info("ARM %s PMU driver probed", pmu
->model
->name
);
1000 static int cci_platform_probe(struct platform_device
*pdev
)
1005 return of_platform_populate(pdev
->dev
.of_node
, NULL
, NULL
, &pdev
->dev
);
1008 static struct platform_driver cci_pmu_driver
= {
1010 .name
= DRIVER_NAME_PMU
,
1011 .of_match_table
= arm_cci_pmu_matches
,
1013 .probe
= cci_pmu_probe
,
1016 static struct platform_driver cci_platform_driver
= {
1018 .name
= DRIVER_NAME
,
1019 .of_match_table
= arm_cci_matches
,
1021 .probe
= cci_platform_probe
,
1024 static int __init
cci_platform_init(void)
1028 ret
= platform_driver_register(&cci_pmu_driver
);
1032 return platform_driver_register(&cci_platform_driver
);
1035 #else /* !CONFIG_ARM_CCI400_PMU */
1037 static int __init
cci_platform_init(void)
1042 #endif /* CONFIG_ARM_CCI400_PMU */
1044 #ifdef CONFIG_ARM_CCI400_PORT_CTRL
1046 #define CCI_PORT_CTRL 0x0
1047 #define CCI_CTRL_STATUS 0xc
1049 #define CCI_ENABLE_SNOOP_REQ 0x1
1050 #define CCI_ENABLE_DVM_REQ 0x2
1051 #define CCI_ENABLE_REQ (CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ)
1053 enum cci_ace_port_type
{
1054 ACE_INVALID_PORT
= 0x0,
1059 struct cci_ace_port
{
1062 enum cci_ace_port_type type
;
1063 struct device_node
*dn
;
1066 static struct cci_ace_port
*ports
;
1067 static unsigned int nb_cci_ports
;
1075 * Use the port MSB as valid flag, shift can be made dynamic
1076 * by computing number of bits required for port indexes.
1077 * Code disabling CCI cpu ports runs with D-cache invalidated
1078 * and SCTLR bit clear so data accesses must be kept to a minimum
1079 * to improve performance; for now shift is left static to
1080 * avoid one more data access while disabling the CCI port.
1082 #define PORT_VALID_SHIFT 31
1083 #define PORT_VALID (0x1 << PORT_VALID_SHIFT)
1085 static inline void init_cpu_port(struct cpu_port
*port
, u32 index
, u64 mpidr
)
1087 port
->port
= PORT_VALID
| index
;
1088 port
->mpidr
= mpidr
;
1091 static inline bool cpu_port_is_valid(struct cpu_port
*port
)
1093 return !!(port
->port
& PORT_VALID
);
1096 static inline bool cpu_port_match(struct cpu_port
*port
, u64 mpidr
)
1098 return port
->mpidr
== (mpidr
& MPIDR_HWID_BITMASK
);
1101 static struct cpu_port cpu_port
[NR_CPUS
];
1104 * __cci_ace_get_port - Function to retrieve the port index connected to
1107 * @dn: device node of the device to look-up
1111 * - CCI port index if success
1112 * - -ENODEV if failure
1114 static int __cci_ace_get_port(struct device_node
*dn
, int type
)
1118 struct device_node
*cci_portn
;
1120 cci_portn
= of_parse_phandle(dn
, "cci-control-port", 0);
1121 for (i
= 0; i
< nb_cci_ports
; i
++) {
1122 ace_match
= ports
[i
].type
== type
;
1123 if (ace_match
&& cci_portn
== ports
[i
].dn
)
1129 int cci_ace_get_port(struct device_node
*dn
)
1131 return __cci_ace_get_port(dn
, ACE_LITE_PORT
);
1133 EXPORT_SYMBOL_GPL(cci_ace_get_port
);
1135 static void cci_ace_init_ports(void)
1138 struct device_node
*cpun
;
1141 * Port index look-up speeds up the function disabling ports by CPU,
1142 * since the logical to port index mapping is done once and does
1143 * not change after system boot.
1144 * The stashed index array is initialized for all possible CPUs
1147 for_each_possible_cpu(cpu
) {
1148 /* too early to use cpu->of_node */
1149 cpun
= of_get_cpu_node(cpu
, NULL
);
1151 if (WARN(!cpun
, "Missing cpu device node\n"))
1154 port
= __cci_ace_get_port(cpun
, ACE_PORT
);
1158 init_cpu_port(&cpu_port
[cpu
], port
, cpu_logical_map(cpu
));
1161 for_each_possible_cpu(cpu
) {
1162 WARN(!cpu_port_is_valid(&cpu_port
[cpu
]),
1163 "CPU %u does not have an associated CCI port\n",
1168 * Functions to enable/disable a CCI interconnect slave port
1170 * They are called by low-level power management code to disable slave
1171 * interfaces snoops and DVM broadcast.
1172 * Since they may execute with cache data allocation disabled and
1173 * after the caches have been cleaned and invalidated the functions provide
1174 * no explicit locking since they may run with D-cache disabled, so normal
1175 * cacheable kernel locks based on ldrex/strex may not work.
1176 * Locking has to be provided by BSP implementations to ensure proper
1181 * cci_port_control() - function to control a CCI port
1183 * @port: index of the port to setup
1184 * @enable: if true enables the port, if false disables it
1186 static void notrace
cci_port_control(unsigned int port
, bool enable
)
1188 void __iomem
*base
= ports
[port
].base
;
1190 writel_relaxed(enable
? CCI_ENABLE_REQ
: 0, base
+ CCI_PORT_CTRL
);
1192 * This function is called from power down procedures
1193 * and must not execute any instruction that might
1194 * cause the processor to be put in a quiescent state
1195 * (eg wfi). Hence, cpu_relax() can not be added to this
1196 * read loop to optimize power, since it might hide possibly
1197 * disruptive operations.
1199 while (readl_relaxed(cci_ctrl_base
+ CCI_CTRL_STATUS
) & 0x1)
1204 * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
1207 * @mpidr: mpidr of the CPU whose CCI port should be disabled
1209 * Disabling a CCI port for a CPU implies disabling the CCI port
1210 * controlling that CPU cluster. Code disabling CPU CCI ports
1211 * must make sure that the CPU running the code is the last active CPU
1212 * in the cluster ie all other CPUs are quiescent in a low power state.
1216 * -ENODEV on port look-up failure
1218 int notrace
cci_disable_port_by_cpu(u64 mpidr
)
1222 for (cpu
= 0; cpu
< nr_cpu_ids
; cpu
++) {
1223 is_valid
= cpu_port_is_valid(&cpu_port
[cpu
]);
1224 if (is_valid
&& cpu_port_match(&cpu_port
[cpu
], mpidr
)) {
1225 cci_port_control(cpu_port
[cpu
].port
, false);
1231 EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu
);
1234 * cci_enable_port_for_self() - enable a CCI port for calling CPU
1236 * Enabling a CCI port for the calling CPU implies enabling the CCI
1237 * port controlling that CPU's cluster. Caller must make sure that the
1238 * CPU running the code is the first active CPU in the cluster and all
1239 * other CPUs are quiescent in a low power state or waiting for this CPU
1240 * to complete the CCI initialization.
1242 * Because this is called when the MMU is still off and with no stack,
1243 * the code must be position independent and ideally rely on callee
1244 * clobbered registers only. To achieve this we must code this function
1245 * entirely in assembler.
1247 * On success this returns with the proper CCI port enabled. In case of
1248 * any failure this never returns as the inability to enable the CCI is
1249 * fatal and there is no possible recovery at this stage.
1251 asmlinkage
void __naked
cci_enable_port_for_self(void)
1255 " mrc p15, 0, r0, c0, c0, 5 @ get MPIDR value \n"
1256 " and r0, r0, #"__stringify(MPIDR_HWID_BITMASK
)" \n"
1259 " add r1, r1, r2 @ &cpu_port \n"
1260 " add ip, r1, %[sizeof_cpu_port] \n"
1262 /* Loop over the cpu_port array looking for a matching MPIDR */
1263 "1: ldr r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
1264 " cmp r2, r0 @ compare MPIDR \n"
1267 /* Found a match, now test port validity */
1268 " ldr r3, [r1, %[offsetof_cpu_port_port]] \n"
1269 " tst r3, #"__stringify(PORT_VALID
)" \n"
1272 /* no match, loop with the next cpu_port entry */
1273 "2: add r1, r1, %[sizeof_struct_cpu_port] \n"
1274 " cmp r1, ip @ done? \n"
1277 /* CCI port not found -- cheaply try to stall this CPU */
1278 "cci_port_not_found: \n"
1281 " b cci_port_not_found \n"
1283 /* Use matched port index to look up the corresponding ports entry */
1284 "3: bic r3, r3, #"__stringify(PORT_VALID
)" \n"
1286 " ldmia r0, {r1, r2} \n"
1287 " sub r1, r1, r0 @ virt - phys \n"
1288 " ldr r0, [r0, r2] @ *(&ports) \n"
1289 " mov r2, %[sizeof_struct_ace_port] \n"
1290 " mla r0, r2, r3, r0 @ &ports[index] \n"
1291 " sub r0, r0, r1 @ virt_to_phys() \n"
1293 /* Enable the CCI port */
1294 " ldr r0, [r0, %[offsetof_port_phys]] \n"
1295 " mov r3, %[cci_enable_req]\n"
1296 " str r3, [r0, #"__stringify(CCI_PORT_CTRL
)"] \n"
1298 /* poll the status reg for completion */
1301 " ldr r0, [r0, r1] @ cci_ctrl_base \n"
1302 "4: ldr r1, [r0, #"__stringify(CCI_CTRL_STATUS
)"] \n"
1303 " tst r1, %[cci_control_status_bits] \n"
1310 "5: .word cpu_port - . \n"
1312 " .word ports - 6b \n"
1313 "7: .word cci_ctrl_phys - . \n"
1315 [sizeof_cpu_port
] "i" (sizeof(cpu_port
)),
1316 [cci_enable_req
] "i" cpu_to_le32(CCI_ENABLE_REQ
),
1317 [cci_control_status_bits
] "i" cpu_to_le32(1),
1319 [offsetof_cpu_port_mpidr_lsb
] "i" (offsetof(struct cpu_port
, mpidr
)),
1321 [offsetof_cpu_port_mpidr_lsb
] "i" (offsetof(struct cpu_port
, mpidr
)+4),
1323 [offsetof_cpu_port_port
] "i" (offsetof(struct cpu_port
, port
)),
1324 [sizeof_struct_cpu_port
] "i" (sizeof(struct cpu_port
)),
1325 [sizeof_struct_ace_port
] "i" (sizeof(struct cci_ace_port
)),
1326 [offsetof_port_phys
] "i" (offsetof(struct cci_ace_port
, phys
)) );
1332 * __cci_control_port_by_device() - function to control a CCI port by device
1335 * @dn: device node pointer of the device whose CCI port should be
1337 * @enable: if true enables the port, if false disables it
1341 * -ENODEV on port look-up failure
1343 int notrace
__cci_control_port_by_device(struct device_node
*dn
, bool enable
)
1350 port
= __cci_ace_get_port(dn
, ACE_LITE_PORT
);
1351 if (WARN_ONCE(port
< 0, "node %s ACE lite port look-up failure\n",
1354 cci_port_control(port
, enable
);
1357 EXPORT_SYMBOL_GPL(__cci_control_port_by_device
);
1360 * __cci_control_port_by_index() - function to control a CCI port by port index
1362 * @port: port index previously retrieved with cci_ace_get_port()
1363 * @enable: if true enables the port, if false disables it
1367 * -ENODEV on port index out of range
1368 * -EPERM if operation carried out on an ACE PORT
1370 int notrace
__cci_control_port_by_index(u32 port
, bool enable
)
1372 if (port
>= nb_cci_ports
|| ports
[port
].type
== ACE_INVALID_PORT
)
1375 * CCI control for ports connected to CPUS is extremely fragile
1376 * and must be made to go through a specific and controlled
1377 * interface (ie cci_disable_port_by_cpu(); control by general purpose
1378 * indexing is therefore disabled for ACE ports.
1380 if (ports
[port
].type
== ACE_PORT
)
1383 cci_port_control(port
, enable
);
1386 EXPORT_SYMBOL_GPL(__cci_control_port_by_index
);
1388 static const struct of_device_id arm_cci_ctrl_if_matches
[] = {
1389 {.compatible
= "arm,cci-400-ctrl-if", },
1393 static int cci_probe_ports(struct device_node
*np
)
1395 struct cci_nb_ports
const *cci_config
;
1396 int ret
, i
, nb_ace
= 0, nb_ace_lite
= 0;
1397 struct device_node
*cp
;
1398 struct resource res
;
1399 const char *match_str
;
1403 cci_config
= of_match_node(arm_cci_matches
, np
)->data
;
1407 nb_cci_ports
= cci_config
->nb_ace
+ cci_config
->nb_ace_lite
;
1409 ports
= kcalloc(nb_cci_ports
, sizeof(*ports
), GFP_KERNEL
);
1413 for_each_child_of_node(np
, cp
) {
1414 if (!of_match_node(arm_cci_ctrl_if_matches
, cp
))
1417 i
= nb_ace
+ nb_ace_lite
;
1419 if (i
>= nb_cci_ports
)
1422 if (of_property_read_string(cp
, "interface-type",
1424 WARN(1, "node %s missing interface-type property\n",
1428 is_ace
= strcmp(match_str
, "ace") == 0;
1429 if (!is_ace
&& strcmp(match_str
, "ace-lite")) {
1430 WARN(1, "node %s containing invalid interface-type property, skipping it\n",
1435 ret
= of_address_to_resource(cp
, 0, &res
);
1437 ports
[i
].base
= ioremap(res
.start
, resource_size(&res
));
1438 ports
[i
].phys
= res
.start
;
1440 if (ret
|| !ports
[i
].base
) {
1441 WARN(1, "unable to ioremap CCI port %d\n", i
);
1446 if (WARN_ON(nb_ace
>= cci_config
->nb_ace
))
1448 ports
[i
].type
= ACE_PORT
;
1451 if (WARN_ON(nb_ace_lite
>= cci_config
->nb_ace_lite
))
1453 ports
[i
].type
= ACE_LITE_PORT
;
1459 /* initialize a stashed array of ACE ports to speed-up look-up */
1460 cci_ace_init_ports();
1463 * Multi-cluster systems may need this data when non-coherent, during
1464 * cluster power-up/power-down. Make sure it reaches main memory.
1466 sync_cache_w(&cci_ctrl_base
);
1467 sync_cache_w(&cci_ctrl_phys
);
1468 sync_cache_w(&ports
);
1469 sync_cache_w(&cpu_port
);
1470 __sync_cache_range_w(ports
, sizeof(*ports
) * nb_cci_ports
);
1471 pr_info("ARM CCI driver probed\n");
1475 #else /* !CONFIG_ARM_CCI400_PORT_CTRL */
1476 static inline int cci_probe_ports(struct device_node
*np
)
1480 #endif /* CONFIG_ARM_CCI400_PORT_CTRL */
1482 static int cci_probe(void)
1485 struct device_node
*np
;
1486 struct resource res
;
1488 np
= of_find_matching_node(NULL
, arm_cci_matches
);
1489 if(!np
|| !of_device_is_available(np
))
1492 ret
= of_address_to_resource(np
, 0, &res
);
1494 cci_ctrl_base
= ioremap(res
.start
, resource_size(&res
));
1495 cci_ctrl_phys
= res
.start
;
1497 if (ret
|| !cci_ctrl_base
) {
1498 WARN(1, "unable to ioremap CCI ctrl\n");
1502 return cci_probe_ports(np
);
1505 static int cci_init_status
= -EAGAIN
;
1506 static DEFINE_MUTEX(cci_probing
);
1508 static int cci_init(void)
1510 if (cci_init_status
!= -EAGAIN
)
1511 return cci_init_status
;
1513 mutex_lock(&cci_probing
);
1514 if (cci_init_status
== -EAGAIN
)
1515 cci_init_status
= cci_probe();
1516 mutex_unlock(&cci_probing
);
1517 return cci_init_status
;
1521 * To sort out early init calls ordering a helper function is provided to
1522 * check if the CCI driver has beed initialized. Function check if the driver
1523 * has been initialized, if not it calls the init function that probes
1524 * the driver and updates the return value.
1526 bool cci_probed(void)
1528 return cci_init() == 0;
1530 EXPORT_SYMBOL_GPL(cci_probed
);
1532 early_initcall(cci_init
);
1533 core_initcall(cci_platform_init
);
1534 MODULE_LICENSE("GPL");
1535 MODULE_DESCRIPTION("ARM CCI support");