Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1 | /* |
| 2 | * CCI cache coherent interconnect driver |
| 3 | * |
| 4 | * Copyright (C) 2013 ARM Ltd. |
| 5 | * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any |
| 12 | * kind, whether express or implied; without even the implied warranty |
| 13 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/arm-cci.h> |
| 18 | #include <linux/io.h> |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 20 | #include <linux/module.h> |
| 21 | #include <linux/of_address.h> |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 22 | #include <linux/of_irq.h> |
| 23 | #include <linux/of_platform.h> |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 24 | #include <linux/perf_event.h> |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 25 | #include <linux/platform_device.h> |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 26 | #include <linux/slab.h> |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 27 | #include <linux/spinlock.h> |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 28 | |
| 29 | #include <asm/cacheflush.h> |
| 30 | #include <asm/smp_plat.h> |
| 31 | |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 32 | static void __iomem *cci_ctrl_base; |
| 33 | static unsigned long cci_ctrl_phys; |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 34 | |
Suzuki K. Poulose | ee8e5d5 | 2015-03-18 12:24:41 +0000 | [diff] [blame] | 35 | #ifdef CONFIG_ARM_CCI400_PORT_CTRL |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 36 | struct cci_nb_ports { |
| 37 | unsigned int nb_ace; |
| 38 | unsigned int nb_ace_lite; |
| 39 | }; |
| 40 | |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 41 | static const struct cci_nb_ports cci400_ports = { |
| 42 | .nb_ace = 2, |
| 43 | .nb_ace_lite = 3 |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 44 | }; |
| 45 | |
Suzuki K. Poulose | ee8e5d5 | 2015-03-18 12:24:41 +0000 | [diff] [blame] | 46 | #define CCI400_PORTS_DATA (&cci400_ports) |
| 47 | #else |
| 48 | #define CCI400_PORTS_DATA (NULL) |
| 49 | #endif |
| 50 | |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 51 | static const struct of_device_id arm_cci_matches[] = { |
Suzuki K. Poulose | ee8e5d5 | 2015-03-18 12:24:41 +0000 | [diff] [blame] | 52 | #ifdef CONFIG_ARM_CCI400_COMMON |
| 53 | {.compatible = "arm,cci-400", .data = CCI400_PORTS_DATA }, |
| 54 | #endif |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 55 | {}, |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 56 | }; |
| 57 | |
Suzuki K. Poulose | ee8e5d5 | 2015-03-18 12:24:41 +0000 | [diff] [blame] | 58 | #ifdef CONFIG_ARM_CCI400_PMU |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 59 | |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 60 | #define DRIVER_NAME "CCI-400" |
| 61 | #define DRIVER_NAME_PMU DRIVER_NAME " PMU" |
| 62 | |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 63 | #define CCI_PMCR 0x0100 |
| 64 | #define CCI_PID2 0x0fe8 |
| 65 | |
| 66 | #define CCI_PMCR_CEN 0x00000001 |
| 67 | #define CCI_PMCR_NCNT_MASK 0x0000f800 |
| 68 | #define CCI_PMCR_NCNT_SHIFT 11 |
| 69 | |
| 70 | #define CCI_PID2_REV_MASK 0xf0 |
| 71 | #define CCI_PID2_REV_SHIFT 4 |
| 72 | |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 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 |
| 77 | |
| 78 | #define CCI_PMU_OVRFLW_FLAG 1 |
| 79 | |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 80 | #define CCI_PMU_CNTR_SIZE(model) ((model)->cntr_size) |
| 81 | #define CCI_PMU_CNTR_BASE(model, idx) ((idx) * CCI_PMU_CNTR_SIZE(model)) |
| 82 | #define CCI_PMU_CNTR_MASK ((1ULL << 32) -1) |
| 83 | #define CCI_PMU_CNTR_LAST(cci_pmu) (cci_pmu->num_cntrs - 1) |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 84 | |
Suzuki K. Poulose | 874c571 | 2015-03-18 12:24:42 +0000 | [diff] [blame] | 85 | #define CCI_PMU_EVENT_MASK 0xffUL |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 86 | #define CCI_PMU_EVENT_SOURCE(event) ((event >> 5) & 0x7) |
| 87 | #define CCI_PMU_EVENT_CODE(event) (event & 0x1f) |
| 88 | |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 89 | #define CCI_PMU_MAX_HW_CNTRS(model) \ |
| 90 | ((model)->num_hw_cntrs + (model)->fixed_hw_cntrs) |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 91 | |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 92 | /* Types of interfaces that can generate events */ |
| 93 | enum { |
| 94 | CCI_IF_SLAVE, |
| 95 | CCI_IF_MASTER, |
| 96 | CCI_IF_MAX, |
| 97 | }; |
| 98 | |
| 99 | struct event_range { |
| 100 | u32 min; |
| 101 | u32 max; |
| 102 | }; |
| 103 | |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 104 | struct cci_pmu_hw_events { |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 105 | struct perf_event **events; |
| 106 | unsigned long *used_mask; |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 107 | raw_spinlock_t pmu_lock; |
| 108 | }; |
| 109 | |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 110 | /* |
| 111 | * struct cci_pmu_model: |
| 112 | * @fixed_hw_cntrs - Number of fixed event counters |
| 113 | * @num_hw_cntrs - Maximum number of programmable event counters |
| 114 | * @cntr_size - Size of an event counter mapping |
| 115 | */ |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 116 | struct cci_pmu_model { |
| 117 | char *name; |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 118 | u32 fixed_hw_cntrs; |
| 119 | u32 num_hw_cntrs; |
| 120 | u32 cntr_size; |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 121 | struct event_range event_ranges[CCI_IF_MAX]; |
| 122 | }; |
| 123 | |
| 124 | static struct cci_pmu_model cci_pmu_models[]; |
| 125 | |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 126 | struct cci_pmu { |
| 127 | void __iomem *base; |
| 128 | struct pmu pmu; |
| 129 | int nr_irqs; |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 130 | int *irqs; |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 131 | unsigned long active_irqs; |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 132 | const struct cci_pmu_model *model; |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 133 | struct cci_pmu_hw_events hw_events; |
| 134 | struct platform_device *plat_device; |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 135 | int num_cntrs; |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 136 | atomic_t active_events; |
| 137 | struct mutex reserve_mutex; |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 138 | struct notifier_block cpu_nb; |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 139 | cpumask_t cpus; |
| 140 | }; |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 141 | |
| 142 | #define to_cci_pmu(c) (container_of(c, struct cci_pmu, pmu)) |
| 143 | |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 144 | /* Port ids */ |
| 145 | #define CCI_PORT_S0 0 |
| 146 | #define CCI_PORT_S1 1 |
| 147 | #define CCI_PORT_S2 2 |
| 148 | #define CCI_PORT_S3 3 |
| 149 | #define CCI_PORT_S4 4 |
| 150 | #define CCI_PORT_M0 5 |
| 151 | #define CCI_PORT_M1 6 |
| 152 | #define CCI_PORT_M2 7 |
| 153 | |
| 154 | #define CCI_REV_R0 0 |
| 155 | #define CCI_REV_R1 1 |
Punit Agrawal | 6fb0c4a | 2014-02-19 12:17:02 +0000 | [diff] [blame] | 156 | #define CCI_REV_R1_PX 5 |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 157 | |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 158 | /* |
| 159 | * Instead of an event id to monitor CCI cycles, a dedicated counter is |
| 160 | * provided. Use 0xff to represent CCI cycles and hope that no future revisions |
| 161 | * make use of this event in hardware. |
| 162 | */ |
| 163 | enum cci400_perf_events { |
| 164 | CCI_PMU_CYCLES = 0xff |
| 165 | }; |
| 166 | |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 167 | #define CCI_PMU_CYCLE_CNTR_IDX 0 |
| 168 | #define CCI_PMU_CNTR0_IDX 1 |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 169 | |
| 170 | /* |
| 171 | * CCI PMU event id is an 8-bit value made of two parts - bits 7:5 for one of 8 |
| 172 | * ports and bits 4:0 are event codes. There are different event codes |
| 173 | * associated with each port type. |
| 174 | * |
| 175 | * Additionally, the range of events associated with the port types changed |
| 176 | * between Rev0 and Rev1. |
| 177 | * |
| 178 | * The constants below define the range of valid codes for each port type for |
| 179 | * the different revisions and are used to validate the event to be monitored. |
| 180 | */ |
| 181 | |
| 182 | #define CCI_REV_R0_SLAVE_PORT_MIN_EV 0x00 |
| 183 | #define CCI_REV_R0_SLAVE_PORT_MAX_EV 0x13 |
| 184 | #define CCI_REV_R0_MASTER_PORT_MIN_EV 0x14 |
| 185 | #define CCI_REV_R0_MASTER_PORT_MAX_EV 0x1a |
| 186 | |
| 187 | #define CCI_REV_R1_SLAVE_PORT_MIN_EV 0x00 |
| 188 | #define CCI_REV_R1_SLAVE_PORT_MAX_EV 0x14 |
| 189 | #define CCI_REV_R1_MASTER_PORT_MIN_EV 0x00 |
| 190 | #define CCI_REV_R1_MASTER_PORT_MAX_EV 0x11 |
| 191 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 192 | static int pmu_validate_hw_event(struct cci_pmu *cci_pmu, unsigned long hw_event) |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 193 | { |
| 194 | u8 ev_source = CCI_PMU_EVENT_SOURCE(hw_event); |
| 195 | u8 ev_code = CCI_PMU_EVENT_CODE(hw_event); |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 196 | int if_type; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 197 | |
Suzuki K. Poulose | 874c571 | 2015-03-18 12:24:42 +0000 | [diff] [blame] | 198 | if (hw_event & ~CCI_PMU_EVENT_MASK) |
| 199 | return -ENOENT; |
| 200 | |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 201 | switch (ev_source) { |
| 202 | case CCI_PORT_S0: |
| 203 | case CCI_PORT_S1: |
| 204 | case CCI_PORT_S2: |
| 205 | case CCI_PORT_S3: |
| 206 | case CCI_PORT_S4: |
| 207 | /* Slave Interface */ |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 208 | if_type = CCI_IF_SLAVE; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 209 | break; |
| 210 | case CCI_PORT_M0: |
| 211 | case CCI_PORT_M1: |
| 212 | case CCI_PORT_M2: |
| 213 | /* Master Interface */ |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 214 | if_type = CCI_IF_MASTER; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 215 | break; |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 216 | default: |
| 217 | return -ENOENT; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 218 | } |
| 219 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 220 | if (ev_code >= cci_pmu->model->event_ranges[if_type].min && |
| 221 | ev_code <= cci_pmu->model->event_ranges[if_type].max) |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 222 | return hw_event; |
| 223 | |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 224 | return -ENOENT; |
| 225 | } |
| 226 | |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 227 | static int probe_cci_revision(void) |
| 228 | { |
| 229 | int rev; |
| 230 | rev = readl_relaxed(cci_ctrl_base + CCI_PID2) & CCI_PID2_REV_MASK; |
| 231 | rev >>= CCI_PID2_REV_SHIFT; |
| 232 | |
| 233 | if (rev < CCI_REV_R1_PX) |
| 234 | return CCI_REV_R0; |
| 235 | else |
| 236 | return CCI_REV_R1; |
| 237 | } |
| 238 | |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 239 | static const struct cci_pmu_model *probe_cci_model(struct platform_device *pdev) |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 240 | { |
Suzuki K. Poulose | 772742a | 2015-03-18 12:24:40 +0000 | [diff] [blame] | 241 | if (platform_has_secure_cci_access()) |
| 242 | return &cci_pmu_models[probe_cci_revision()]; |
| 243 | return NULL; |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 246 | static int pmu_is_valid_counter(struct cci_pmu *cci_pmu, int idx) |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 247 | { |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 248 | return 0 <= idx && idx <= CCI_PMU_CNTR_LAST(cci_pmu); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 249 | } |
| 250 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 251 | static u32 pmu_read_register(struct cci_pmu *cci_pmu, int idx, unsigned int offset) |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 252 | { |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 253 | return readl_relaxed(cci_pmu->base + |
| 254 | CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 257 | static void pmu_write_register(struct cci_pmu *cci_pmu, u32 value, |
| 258 | int idx, unsigned int offset) |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 259 | { |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 260 | return writel_relaxed(value, cci_pmu->base + |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 261 | CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 264 | static void pmu_disable_counter(struct cci_pmu *cci_pmu, int idx) |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 265 | { |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 266 | pmu_write_register(cci_pmu, 0, idx, CCI_PMU_CNTR_CTRL); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 269 | static void pmu_enable_counter(struct cci_pmu *cci_pmu, int idx) |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 270 | { |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 271 | pmu_write_register(cci_pmu, 1, idx, CCI_PMU_CNTR_CTRL); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 274 | static void pmu_set_event(struct cci_pmu *cci_pmu, int idx, unsigned long event) |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 275 | { |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 276 | pmu_write_register(cci_pmu, event, idx, CCI_PMU_EVT_SEL); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 277 | } |
| 278 | |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 279 | /* |
| 280 | * Returns the number of programmable counters actually implemented |
| 281 | * by the cci |
| 282 | */ |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 283 | static u32 pmu_get_max_counters(void) |
| 284 | { |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 285 | return (readl_relaxed(cci_ctrl_base + CCI_PMCR) & |
| 286 | CCI_PMCR_NCNT_MASK) >> CCI_PMCR_NCNT_SHIFT; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 289 | static int pmu_get_event_idx(struct cci_pmu_hw_events *hw, struct perf_event *event) |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 290 | { |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 291 | struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 292 | struct hw_perf_event *hw_event = &event->hw; |
Suzuki K. Poulose | 874c571 | 2015-03-18 12:24:42 +0000 | [diff] [blame] | 293 | unsigned long cci_event = hw_event->config_base; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 294 | int idx; |
| 295 | |
| 296 | if (cci_event == CCI_PMU_CYCLES) { |
| 297 | if (test_and_set_bit(CCI_PMU_CYCLE_CNTR_IDX, hw->used_mask)) |
| 298 | return -EAGAIN; |
| 299 | |
| 300 | return CCI_PMU_CYCLE_CNTR_IDX; |
| 301 | } |
| 302 | |
| 303 | for (idx = CCI_PMU_CNTR0_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); ++idx) |
| 304 | if (!test_and_set_bit(idx, hw->used_mask)) |
| 305 | return idx; |
| 306 | |
| 307 | /* No counters available */ |
| 308 | return -EAGAIN; |
| 309 | } |
| 310 | |
| 311 | static int pmu_map_event(struct perf_event *event) |
| 312 | { |
| 313 | int mapping; |
Suzuki K. Poulose | 874c571 | 2015-03-18 12:24:42 +0000 | [diff] [blame] | 314 | unsigned long config = event->attr.config; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 315 | |
| 316 | if (event->attr.type < PERF_TYPE_MAX) |
| 317 | return -ENOENT; |
| 318 | |
| 319 | if (config == CCI_PMU_CYCLES) |
| 320 | mapping = config; |
| 321 | else |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 322 | mapping = pmu_validate_hw_event(to_cci_pmu(event->pmu), |
| 323 | config); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 324 | |
| 325 | return mapping; |
| 326 | } |
| 327 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 328 | static int pmu_request_irq(struct cci_pmu *cci_pmu, irq_handler_t handler) |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 329 | { |
| 330 | int i; |
| 331 | struct platform_device *pmu_device = cci_pmu->plat_device; |
| 332 | |
| 333 | if (unlikely(!pmu_device)) |
| 334 | return -ENODEV; |
| 335 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 336 | if (cci_pmu->nr_irqs < 1) { |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 337 | dev_err(&pmu_device->dev, "no irqs for CCI PMUs defined\n"); |
| 338 | return -ENODEV; |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Register all available CCI PMU interrupts. In the interrupt handler |
| 343 | * we iterate over the counters checking for interrupt source (the |
| 344 | * overflowing counter) and clear it. |
| 345 | * |
| 346 | * This should allow handling of non-unique interrupt for the counters. |
| 347 | */ |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 348 | for (i = 0; i < cci_pmu->nr_irqs; i++) { |
| 349 | int err = request_irq(cci_pmu->irqs[i], handler, IRQF_SHARED, |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 350 | "arm-cci-pmu", cci_pmu); |
| 351 | if (err) { |
| 352 | dev_err(&pmu_device->dev, "unable to request IRQ%d for ARM CCI PMU counters\n", |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 353 | cci_pmu->irqs[i]); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 354 | return err; |
| 355 | } |
| 356 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 357 | set_bit(i, &cci_pmu->active_irqs); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 363 | static void pmu_free_irq(struct cci_pmu *cci_pmu) |
| 364 | { |
| 365 | int i; |
| 366 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 367 | for (i = 0; i < cci_pmu->nr_irqs; i++) { |
| 368 | if (!test_and_clear_bit(i, &cci_pmu->active_irqs)) |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 369 | continue; |
| 370 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 371 | free_irq(cci_pmu->irqs[i], cci_pmu); |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | static u32 pmu_read_counter(struct perf_event *event) |
| 376 | { |
| 377 | struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu); |
| 378 | struct hw_perf_event *hw_counter = &event->hw; |
| 379 | int idx = hw_counter->idx; |
| 380 | u32 value; |
| 381 | |
| 382 | if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) { |
| 383 | dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx); |
| 384 | return 0; |
| 385 | } |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 386 | value = pmu_read_register(cci_pmu, idx, CCI_PMU_CNTR); |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 387 | |
| 388 | return value; |
| 389 | } |
| 390 | |
| 391 | static void pmu_write_counter(struct perf_event *event, u32 value) |
| 392 | { |
| 393 | struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu); |
| 394 | struct hw_perf_event *hw_counter = &event->hw; |
| 395 | int idx = hw_counter->idx; |
| 396 | |
| 397 | if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) |
| 398 | dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx); |
| 399 | else |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 400 | pmu_write_register(cci_pmu, value, idx, CCI_PMU_CNTR); |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static u64 pmu_event_update(struct perf_event *event) |
| 404 | { |
| 405 | struct hw_perf_event *hwc = &event->hw; |
| 406 | u64 delta, prev_raw_count, new_raw_count; |
| 407 | |
| 408 | do { |
| 409 | prev_raw_count = local64_read(&hwc->prev_count); |
| 410 | new_raw_count = pmu_read_counter(event); |
| 411 | } while (local64_cmpxchg(&hwc->prev_count, prev_raw_count, |
| 412 | new_raw_count) != prev_raw_count); |
| 413 | |
| 414 | delta = (new_raw_count - prev_raw_count) & CCI_PMU_CNTR_MASK; |
| 415 | |
| 416 | local64_add(delta, &event->count); |
| 417 | |
| 418 | return new_raw_count; |
| 419 | } |
| 420 | |
| 421 | static void pmu_read(struct perf_event *event) |
| 422 | { |
| 423 | pmu_event_update(event); |
| 424 | } |
| 425 | |
| 426 | void pmu_event_set_period(struct perf_event *event) |
| 427 | { |
| 428 | struct hw_perf_event *hwc = &event->hw; |
| 429 | /* |
| 430 | * The CCI PMU counters have a period of 2^32. To account for the |
| 431 | * possiblity of extreme interrupt latency we program for a period of |
| 432 | * half that. Hopefully we can handle the interrupt before another 2^31 |
| 433 | * events occur and the counter overtakes its previous value. |
| 434 | */ |
| 435 | u64 val = 1ULL << 31; |
| 436 | local64_set(&hwc->prev_count, val); |
| 437 | pmu_write_counter(event, val); |
| 438 | } |
| 439 | |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 440 | static irqreturn_t pmu_handle_irq(int irq_num, void *dev) |
| 441 | { |
| 442 | unsigned long flags; |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 443 | struct cci_pmu *cci_pmu = dev; |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 444 | struct cci_pmu_hw_events *events = &cci_pmu->hw_events; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 445 | int idx, handled = IRQ_NONE; |
| 446 | |
| 447 | raw_spin_lock_irqsave(&events->pmu_lock, flags); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 448 | /* |
| 449 | * Iterate over counters and update the corresponding perf events. |
| 450 | * This should work regardless of whether we have per-counter overflow |
| 451 | * interrupt or a combined overflow interrupt. |
| 452 | */ |
| 453 | for (idx = CCI_PMU_CYCLE_CNTR_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++) { |
| 454 | struct perf_event *event = events->events[idx]; |
| 455 | struct hw_perf_event *hw_counter; |
| 456 | |
| 457 | if (!event) |
| 458 | continue; |
| 459 | |
| 460 | hw_counter = &event->hw; |
| 461 | |
| 462 | /* Did this counter overflow? */ |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 463 | if (!(pmu_read_register(cci_pmu, idx, CCI_PMU_OVRFLW) & |
Himangi Saraogi | fc5130d | 2014-07-30 11:37:35 +0100 | [diff] [blame] | 464 | CCI_PMU_OVRFLW_FLAG)) |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 465 | continue; |
| 466 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 467 | pmu_write_register(cci_pmu, CCI_PMU_OVRFLW_FLAG, idx, |
| 468 | CCI_PMU_OVRFLW); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 469 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 470 | pmu_event_update(event); |
| 471 | pmu_event_set_period(event); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 472 | handled = IRQ_HANDLED; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 473 | } |
| 474 | raw_spin_unlock_irqrestore(&events->pmu_lock, flags); |
| 475 | |
| 476 | return IRQ_RETVAL(handled); |
| 477 | } |
| 478 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 479 | static int cci_pmu_get_hw(struct cci_pmu *cci_pmu) |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 480 | { |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 481 | int ret = pmu_request_irq(cci_pmu, pmu_handle_irq); |
| 482 | if (ret) { |
| 483 | pmu_free_irq(cci_pmu); |
| 484 | return ret; |
| 485 | } |
| 486 | return 0; |
| 487 | } |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 488 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 489 | static void cci_pmu_put_hw(struct cci_pmu *cci_pmu) |
| 490 | { |
| 491 | pmu_free_irq(cci_pmu); |
| 492 | } |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 493 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 494 | static void hw_perf_event_destroy(struct perf_event *event) |
| 495 | { |
| 496 | struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu); |
| 497 | atomic_t *active_events = &cci_pmu->active_events; |
| 498 | struct mutex *reserve_mutex = &cci_pmu->reserve_mutex; |
| 499 | |
| 500 | if (atomic_dec_and_mutex_lock(active_events, reserve_mutex)) { |
| 501 | cci_pmu_put_hw(cci_pmu); |
| 502 | mutex_unlock(reserve_mutex); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 506 | static void cci_pmu_enable(struct pmu *pmu) |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 507 | { |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 508 | struct cci_pmu *cci_pmu = to_cci_pmu(pmu); |
| 509 | struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events; |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 510 | int enabled = bitmap_weight(hw_events->used_mask, cci_pmu->num_cntrs); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 511 | unsigned long flags; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 512 | u32 val; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 513 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 514 | if (!enabled) |
| 515 | return; |
| 516 | |
| 517 | raw_spin_lock_irqsave(&hw_events->pmu_lock, flags); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 518 | |
| 519 | /* Enable all the PMU counters. */ |
| 520 | val = readl_relaxed(cci_ctrl_base + CCI_PMCR) | CCI_PMCR_CEN; |
| 521 | writel(val, cci_ctrl_base + CCI_PMCR); |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 522 | raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 523 | |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 524 | } |
| 525 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 526 | static void cci_pmu_disable(struct pmu *pmu) |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 527 | { |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 528 | struct cci_pmu *cci_pmu = to_cci_pmu(pmu); |
| 529 | struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 530 | unsigned long flags; |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 531 | u32 val; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 532 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 533 | raw_spin_lock_irqsave(&hw_events->pmu_lock, flags); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 534 | |
| 535 | /* Disable all the PMU counters. */ |
| 536 | val = readl_relaxed(cci_ctrl_base + CCI_PMCR) & ~CCI_PMCR_CEN; |
| 537 | writel(val, cci_ctrl_base + CCI_PMCR); |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 538 | raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 539 | } |
| 540 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 541 | static void cci_pmu_start(struct perf_event *event, int pmu_flags) |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 542 | { |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 543 | struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu); |
| 544 | struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events; |
| 545 | struct hw_perf_event *hwc = &event->hw; |
| 546 | int idx = hwc->idx; |
| 547 | unsigned long flags; |
| 548 | |
| 549 | /* |
| 550 | * To handle interrupt latency, we always reprogram the period |
| 551 | * regardlesss of PERF_EF_RELOAD. |
| 552 | */ |
| 553 | if (pmu_flags & PERF_EF_RELOAD) |
| 554 | WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE)); |
| 555 | |
| 556 | hwc->state = 0; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 557 | |
| 558 | if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) { |
| 559 | dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx); |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 560 | return; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 561 | } |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 562 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 563 | raw_spin_lock_irqsave(&hw_events->pmu_lock, flags); |
| 564 | |
| 565 | /* Configure the event to count, unless you are counting cycles */ |
| 566 | if (idx != CCI_PMU_CYCLE_CNTR_IDX) |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 567 | pmu_set_event(cci_pmu, idx, hwc->config_base); |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 568 | |
| 569 | pmu_event_set_period(event); |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 570 | pmu_enable_counter(cci_pmu, idx); |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 571 | |
| 572 | raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 573 | } |
| 574 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 575 | static void cci_pmu_stop(struct perf_event *event, int pmu_flags) |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 576 | { |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 577 | struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu); |
| 578 | struct hw_perf_event *hwc = &event->hw; |
| 579 | int idx = hwc->idx; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 580 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 581 | if (hwc->state & PERF_HES_STOPPED) |
| 582 | return; |
| 583 | |
| 584 | if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) { |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 585 | dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx); |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 586 | return; |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * We always reprogram the counter, so ignore PERF_EF_UPDATE. See |
| 591 | * cci_pmu_start() |
| 592 | */ |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 593 | pmu_disable_counter(cci_pmu, idx); |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 594 | pmu_event_update(event); |
| 595 | hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 598 | static int cci_pmu_add(struct perf_event *event, int flags) |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 599 | { |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 600 | struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu); |
| 601 | struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events; |
| 602 | struct hw_perf_event *hwc = &event->hw; |
| 603 | int idx; |
| 604 | int err = 0; |
| 605 | |
| 606 | perf_pmu_disable(event->pmu); |
| 607 | |
| 608 | /* If we don't have a space for the counter then finish early. */ |
| 609 | idx = pmu_get_event_idx(hw_events, event); |
| 610 | if (idx < 0) { |
| 611 | err = idx; |
| 612 | goto out; |
| 613 | } |
| 614 | |
| 615 | event->hw.idx = idx; |
| 616 | hw_events->events[idx] = event; |
| 617 | |
| 618 | hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE; |
| 619 | if (flags & PERF_EF_START) |
| 620 | cci_pmu_start(event, PERF_EF_RELOAD); |
| 621 | |
| 622 | /* Propagate our changes to the userspace mapping. */ |
| 623 | perf_event_update_userpage(event); |
| 624 | |
| 625 | out: |
| 626 | perf_pmu_enable(event->pmu); |
| 627 | return err; |
| 628 | } |
| 629 | |
| 630 | static void cci_pmu_del(struct perf_event *event, int flags) |
| 631 | { |
| 632 | struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu); |
| 633 | struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events; |
| 634 | struct hw_perf_event *hwc = &event->hw; |
| 635 | int idx = hwc->idx; |
| 636 | |
| 637 | cci_pmu_stop(event, PERF_EF_UPDATE); |
| 638 | hw_events->events[idx] = NULL; |
| 639 | clear_bit(idx, hw_events->used_mask); |
| 640 | |
| 641 | perf_event_update_userpage(event); |
| 642 | } |
| 643 | |
| 644 | static int |
Suzuki K. Poulose | b186219 | 2015-03-17 18:15:00 +0000 | [diff] [blame] | 645 | validate_event(struct pmu *cci_pmu, |
| 646 | struct cci_pmu_hw_events *hw_events, |
| 647 | struct perf_event *event) |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 648 | { |
| 649 | if (is_software_event(event)) |
| 650 | return 1; |
| 651 | |
Suzuki K. Poulose | b186219 | 2015-03-17 18:15:00 +0000 | [diff] [blame] | 652 | /* |
| 653 | * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The |
| 654 | * core perf code won't check that the pmu->ctx == leader->ctx |
| 655 | * until after pmu->event_init(event). |
| 656 | */ |
| 657 | if (event->pmu != cci_pmu) |
| 658 | return 0; |
| 659 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 660 | if (event->state < PERF_EVENT_STATE_OFF) |
| 661 | return 1; |
| 662 | |
| 663 | if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec) |
| 664 | return 1; |
| 665 | |
| 666 | return pmu_get_event_idx(hw_events, event) >= 0; |
| 667 | } |
| 668 | |
| 669 | static int |
| 670 | validate_group(struct perf_event *event) |
| 671 | { |
| 672 | struct perf_event *sibling, *leader = event->group_leader; |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 673 | struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu); |
| 674 | unsigned long mask[BITS_TO_LONGS(cci_pmu->num_cntrs)]; |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 675 | struct cci_pmu_hw_events fake_pmu = { |
| 676 | /* |
| 677 | * Initialise the fake PMU. We only need to populate the |
| 678 | * used_mask for the purposes of validation. |
| 679 | */ |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 680 | .used_mask = mask, |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 681 | }; |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 682 | memset(mask, 0, BITS_TO_LONGS(cci_pmu->num_cntrs) * sizeof(unsigned long)); |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 683 | |
Suzuki K. Poulose | b186219 | 2015-03-17 18:15:00 +0000 | [diff] [blame] | 684 | if (!validate_event(event->pmu, &fake_pmu, leader)) |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 685 | return -EINVAL; |
| 686 | |
| 687 | list_for_each_entry(sibling, &leader->sibling_list, group_entry) { |
Suzuki K. Poulose | b186219 | 2015-03-17 18:15:00 +0000 | [diff] [blame] | 688 | if (!validate_event(event->pmu, &fake_pmu, sibling)) |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 689 | return -EINVAL; |
| 690 | } |
| 691 | |
Suzuki K. Poulose | b186219 | 2015-03-17 18:15:00 +0000 | [diff] [blame] | 692 | if (!validate_event(event->pmu, &fake_pmu, event)) |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 693 | return -EINVAL; |
| 694 | |
| 695 | return 0; |
| 696 | } |
| 697 | |
| 698 | static int |
| 699 | __hw_perf_event_init(struct perf_event *event) |
| 700 | { |
| 701 | struct hw_perf_event *hwc = &event->hw; |
| 702 | int mapping; |
| 703 | |
| 704 | mapping = pmu_map_event(event); |
| 705 | |
| 706 | if (mapping < 0) { |
| 707 | pr_debug("event %x:%llx not supported\n", event->attr.type, |
| 708 | event->attr.config); |
| 709 | return mapping; |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * We don't assign an index until we actually place the event onto |
| 714 | * hardware. Use -1 to signify that we haven't decided where to put it |
| 715 | * yet. |
| 716 | */ |
| 717 | hwc->idx = -1; |
| 718 | hwc->config_base = 0; |
| 719 | hwc->config = 0; |
| 720 | hwc->event_base = 0; |
| 721 | |
| 722 | /* |
| 723 | * Store the event encoding into the config_base field. |
| 724 | */ |
| 725 | hwc->config_base |= (unsigned long)mapping; |
| 726 | |
| 727 | /* |
| 728 | * Limit the sample_period to half of the counter width. That way, the |
| 729 | * new counter value is far less likely to overtake the previous one |
| 730 | * unless you have some serious IRQ latency issues. |
| 731 | */ |
| 732 | hwc->sample_period = CCI_PMU_CNTR_MASK >> 1; |
| 733 | hwc->last_period = hwc->sample_period; |
| 734 | local64_set(&hwc->period_left, hwc->sample_period); |
| 735 | |
| 736 | if (event->group_leader != event) { |
| 737 | if (validate_group(event) != 0) |
| 738 | return -EINVAL; |
| 739 | } |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | static int cci_pmu_event_init(struct perf_event *event) |
| 745 | { |
| 746 | struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu); |
| 747 | atomic_t *active_events = &cci_pmu->active_events; |
| 748 | int err = 0; |
| 749 | int cpu; |
| 750 | |
| 751 | if (event->attr.type != event->pmu->type) |
| 752 | return -ENOENT; |
| 753 | |
| 754 | /* Shared by all CPUs, no meaningful state to sample */ |
| 755 | if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK) |
| 756 | return -EOPNOTSUPP; |
| 757 | |
| 758 | /* We have no filtering of any kind */ |
| 759 | if (event->attr.exclude_user || |
| 760 | event->attr.exclude_kernel || |
| 761 | event->attr.exclude_hv || |
| 762 | event->attr.exclude_idle || |
| 763 | event->attr.exclude_host || |
| 764 | event->attr.exclude_guest) |
| 765 | return -EINVAL; |
| 766 | |
| 767 | /* |
| 768 | * Following the example set by other "uncore" PMUs, we accept any CPU |
| 769 | * and rewrite its affinity dynamically rather than having perf core |
| 770 | * handle cpu == -1 and pid == -1 for this case. |
| 771 | * |
| 772 | * The perf core will pin online CPUs for the duration of this call and |
| 773 | * the event being installed into its context, so the PMU's CPU can't |
| 774 | * change under our feet. |
| 775 | */ |
| 776 | cpu = cpumask_first(&cci_pmu->cpus); |
| 777 | if (event->cpu < 0 || cpu < 0) |
| 778 | return -EINVAL; |
| 779 | event->cpu = cpu; |
| 780 | |
| 781 | event->destroy = hw_perf_event_destroy; |
| 782 | if (!atomic_inc_not_zero(active_events)) { |
| 783 | mutex_lock(&cci_pmu->reserve_mutex); |
| 784 | if (atomic_read(active_events) == 0) |
| 785 | err = cci_pmu_get_hw(cci_pmu); |
| 786 | if (!err) |
| 787 | atomic_inc(active_events); |
| 788 | mutex_unlock(&cci_pmu->reserve_mutex); |
| 789 | } |
| 790 | if (err) |
| 791 | return err; |
| 792 | |
| 793 | err = __hw_perf_event_init(event); |
| 794 | if (err) |
| 795 | hw_perf_event_destroy(event); |
| 796 | |
| 797 | return err; |
| 798 | } |
| 799 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 800 | static ssize_t pmu_cpumask_attr_show(struct device *dev, |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 801 | struct device_attribute *attr, char *buf) |
| 802 | { |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 803 | struct dev_ext_attribute *eattr = container_of(attr, |
| 804 | struct dev_ext_attribute, attr); |
| 805 | struct cci_pmu *cci_pmu = eattr->var; |
| 806 | |
Tejun Heo | 660e5ec | 2015-02-13 14:37:20 -0800 | [diff] [blame] | 807 | int n = scnprintf(buf, PAGE_SIZE - 1, "%*pbl", |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 808 | cpumask_pr_args(&cci_pmu->cpus)); |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 809 | buf[n++] = '\n'; |
| 810 | buf[n] = '\0'; |
| 811 | return n; |
| 812 | } |
| 813 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 814 | static struct dev_ext_attribute pmu_cpumask_attr = { |
| 815 | __ATTR(cpumask, S_IRUGO, pmu_cpumask_attr_show, NULL), |
| 816 | NULL, /* Populated in cci_pmu_init */ |
| 817 | }; |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 818 | |
| 819 | static struct attribute *pmu_attrs[] = { |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 820 | &pmu_cpumask_attr.attr.attr, |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 821 | NULL, |
| 822 | }; |
| 823 | |
| 824 | static struct attribute_group pmu_attr_group = { |
| 825 | .attrs = pmu_attrs, |
| 826 | }; |
| 827 | |
| 828 | static const struct attribute_group *pmu_attr_groups[] = { |
| 829 | &pmu_attr_group, |
| 830 | NULL |
| 831 | }; |
| 832 | |
| 833 | static int cci_pmu_init(struct cci_pmu *cci_pmu, struct platform_device *pdev) |
| 834 | { |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 835 | char *name = cci_pmu->model->name; |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 836 | u32 num_cntrs; |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 837 | |
| 838 | pmu_cpumask_attr.var = cci_pmu; |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 839 | cci_pmu->pmu = (struct pmu) { |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 840 | .name = cci_pmu->model->name, |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 841 | .task_ctx_nr = perf_invalid_context, |
| 842 | .pmu_enable = cci_pmu_enable, |
| 843 | .pmu_disable = cci_pmu_disable, |
| 844 | .event_init = cci_pmu_event_init, |
| 845 | .add = cci_pmu_add, |
| 846 | .del = cci_pmu_del, |
| 847 | .start = cci_pmu_start, |
| 848 | .stop = cci_pmu_stop, |
| 849 | .read = pmu_read, |
| 850 | .attr_groups = pmu_attr_groups, |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 851 | }; |
| 852 | |
| 853 | cci_pmu->plat_device = pdev; |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 854 | num_cntrs = pmu_get_max_counters(); |
| 855 | if (num_cntrs > cci_pmu->model->num_hw_cntrs) { |
| 856 | dev_warn(&pdev->dev, |
| 857 | "PMU implements more counters(%d) than supported by" |
| 858 | " the model(%d), truncated.", |
| 859 | num_cntrs, cci_pmu->model->num_hw_cntrs); |
| 860 | num_cntrs = cci_pmu->model->num_hw_cntrs; |
| 861 | } |
| 862 | cci_pmu->num_cntrs = num_cntrs + cci_pmu->model->fixed_hw_cntrs; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 863 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 864 | return perf_pmu_register(&cci_pmu->pmu, name, -1); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 865 | } |
| 866 | |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 867 | static int cci_pmu_cpu_notifier(struct notifier_block *self, |
| 868 | unsigned long action, void *hcpu) |
| 869 | { |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 870 | struct cci_pmu *cci_pmu = container_of(self, |
| 871 | struct cci_pmu, cpu_nb); |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 872 | unsigned int cpu = (long)hcpu; |
| 873 | unsigned int target; |
| 874 | |
| 875 | switch (action & ~CPU_TASKS_FROZEN) { |
| 876 | case CPU_DOWN_PREPARE: |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 877 | if (!cpumask_test_and_clear_cpu(cpu, &cci_pmu->cpus)) |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 878 | break; |
| 879 | target = cpumask_any_but(cpu_online_mask, cpu); |
| 880 | if (target < 0) // UP, last CPU |
| 881 | break; |
| 882 | /* |
| 883 | * TODO: migrate context once core races on event->ctx have |
| 884 | * been fixed. |
| 885 | */ |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 886 | cpumask_set_cpu(target, &cci_pmu->cpus); |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 887 | default: |
| 888 | break; |
| 889 | } |
| 890 | |
| 891 | return NOTIFY_OK; |
| 892 | } |
| 893 | |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 894 | static struct cci_pmu_model cci_pmu_models[] = { |
| 895 | [CCI_REV_R0] = { |
| 896 | .name = "CCI_400", |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 897 | .fixed_hw_cntrs = 1, /* Cycle counter */ |
| 898 | .num_hw_cntrs = 4, |
| 899 | .cntr_size = SZ_4K, |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 900 | .event_ranges = { |
| 901 | [CCI_IF_SLAVE] = { |
| 902 | CCI_REV_R0_SLAVE_PORT_MIN_EV, |
| 903 | CCI_REV_R0_SLAVE_PORT_MAX_EV, |
| 904 | }, |
| 905 | [CCI_IF_MASTER] = { |
| 906 | CCI_REV_R0_MASTER_PORT_MIN_EV, |
| 907 | CCI_REV_R0_MASTER_PORT_MAX_EV, |
| 908 | }, |
| 909 | }, |
| 910 | }, |
| 911 | [CCI_REV_R1] = { |
| 912 | .name = "CCI_400_r1", |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 913 | .fixed_hw_cntrs = 1, /* Cycle counter */ |
| 914 | .num_hw_cntrs = 4, |
| 915 | .cntr_size = SZ_4K, |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 916 | .event_ranges = { |
| 917 | [CCI_IF_SLAVE] = { |
| 918 | CCI_REV_R1_SLAVE_PORT_MIN_EV, |
| 919 | CCI_REV_R1_SLAVE_PORT_MAX_EV, |
| 920 | }, |
| 921 | [CCI_IF_MASTER] = { |
| 922 | CCI_REV_R1_MASTER_PORT_MIN_EV, |
| 923 | CCI_REV_R1_MASTER_PORT_MAX_EV, |
| 924 | }, |
| 925 | }, |
| 926 | }, |
| 927 | }; |
| 928 | |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 929 | static const struct of_device_id arm_cci_pmu_matches[] = { |
| 930 | { |
| 931 | .compatible = "arm,cci-400-pmu", |
Suzuki K. Poulose | 772742a | 2015-03-18 12:24:40 +0000 | [diff] [blame] | 932 | .data = NULL, |
| 933 | }, |
| 934 | { |
| 935 | .compatible = "arm,cci-400-pmu,r0", |
| 936 | .data = &cci_pmu_models[CCI_REV_R0], |
| 937 | }, |
| 938 | { |
| 939 | .compatible = "arm,cci-400-pmu,r1", |
| 940 | .data = &cci_pmu_models[CCI_REV_R1], |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 941 | }, |
| 942 | {}, |
| 943 | }; |
| 944 | |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 945 | static inline const struct cci_pmu_model *get_cci_model(struct platform_device *pdev) |
| 946 | { |
| 947 | const struct of_device_id *match = of_match_node(arm_cci_pmu_matches, |
| 948 | pdev->dev.of_node); |
| 949 | if (!match) |
| 950 | return NULL; |
Suzuki K. Poulose | 772742a | 2015-03-18 12:24:40 +0000 | [diff] [blame] | 951 | if (match->data) |
| 952 | return match->data; |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 953 | |
Suzuki K. Poulose | 772742a | 2015-03-18 12:24:40 +0000 | [diff] [blame] | 954 | dev_warn(&pdev->dev, "DEPRECATED compatible property," |
| 955 | "requires secure access to CCI registers"); |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 956 | return probe_cci_model(pdev); |
| 957 | } |
| 958 | |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 959 | static bool is_duplicate_irq(int irq, int *irqs, int nr_irqs) |
| 960 | { |
| 961 | int i; |
| 962 | |
| 963 | for (i = 0; i < nr_irqs; i++) |
| 964 | if (irq == irqs[i]) |
| 965 | return true; |
| 966 | |
| 967 | return false; |
| 968 | } |
| 969 | |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 970 | static struct cci_pmu *cci_pmu_alloc(struct platform_device *pdev) |
| 971 | { |
| 972 | struct cci_pmu *cci_pmu; |
| 973 | const struct cci_pmu_model *model; |
| 974 | |
| 975 | /* |
| 976 | * All allocations are devm_* hence we don't have to free |
| 977 | * them explicitly on an error, as it would end up in driver |
| 978 | * detach. |
| 979 | */ |
| 980 | model = get_cci_model(pdev); |
| 981 | if (!model) { |
| 982 | dev_warn(&pdev->dev, "CCI PMU version not supported\n"); |
| 983 | return ERR_PTR(-ENODEV); |
| 984 | } |
| 985 | |
| 986 | cci_pmu = devm_kzalloc(&pdev->dev, sizeof(*cci_pmu), GFP_KERNEL); |
| 987 | if (!cci_pmu) |
| 988 | return ERR_PTR(-ENOMEM); |
| 989 | |
| 990 | cci_pmu->model = model; |
| 991 | cci_pmu->irqs = devm_kcalloc(&pdev->dev, CCI_PMU_MAX_HW_CNTRS(model), |
| 992 | sizeof(*cci_pmu->irqs), GFP_KERNEL); |
| 993 | if (!cci_pmu->irqs) |
| 994 | return ERR_PTR(-ENOMEM); |
| 995 | cci_pmu->hw_events.events = devm_kcalloc(&pdev->dev, |
| 996 | CCI_PMU_MAX_HW_CNTRS(model), |
| 997 | sizeof(*cci_pmu->hw_events.events), |
| 998 | GFP_KERNEL); |
| 999 | if (!cci_pmu->hw_events.events) |
| 1000 | return ERR_PTR(-ENOMEM); |
| 1001 | cci_pmu->hw_events.used_mask = devm_kcalloc(&pdev->dev, |
| 1002 | BITS_TO_LONGS(CCI_PMU_MAX_HW_CNTRS(model)), |
| 1003 | sizeof(*cci_pmu->hw_events.used_mask), |
| 1004 | GFP_KERNEL); |
| 1005 | if (!cci_pmu->hw_events.used_mask) |
| 1006 | return ERR_PTR(-ENOMEM); |
| 1007 | |
| 1008 | return cci_pmu; |
| 1009 | } |
| 1010 | |
| 1011 | |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1012 | static int cci_pmu_probe(struct platform_device *pdev) |
| 1013 | { |
| 1014 | struct resource *res; |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 1015 | struct cci_pmu *cci_pmu; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1016 | int i, ret, irq; |
Suzuki K. Poulose | fc17c83 | 2015-03-18 12:24:39 +0000 | [diff] [blame] | 1017 | |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 1018 | cci_pmu = cci_pmu_alloc(pdev); |
| 1019 | if (IS_ERR(cci_pmu)) |
| 1020 | return PTR_ERR(cci_pmu); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1021 | |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1022 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 1023 | cci_pmu->base = devm_ioremap_resource(&pdev->dev, res); |
| 1024 | if (IS_ERR(cci_pmu->base)) |
Wei Yongjun | fee4f2c | 2013-09-22 06:04:23 +0100 | [diff] [blame] | 1025 | return -ENOMEM; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1026 | |
| 1027 | /* |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 1028 | * CCI PMU has one overflow interrupt per counter; but some may be tied |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1029 | * together to a common interrupt. |
| 1030 | */ |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 1031 | cci_pmu->nr_irqs = 0; |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 1032 | for (i = 0; i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model); i++) { |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1033 | irq = platform_get_irq(pdev, i); |
| 1034 | if (irq < 0) |
| 1035 | break; |
| 1036 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 1037 | if (is_duplicate_irq(irq, cci_pmu->irqs, cci_pmu->nr_irqs)) |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1038 | continue; |
| 1039 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 1040 | cci_pmu->irqs[cci_pmu->nr_irqs++] = irq; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | /* |
| 1044 | * Ensure that the device tree has as many interrupts as the number |
| 1045 | * of counters. |
| 1046 | */ |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 1047 | if (i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model)) { |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1048 | dev_warn(&pdev->dev, "In-correct number of interrupts: %d, should be %d\n", |
Suzuki K. Poulose | ab5b316 | 2015-05-26 10:53:12 +0100 | [diff] [blame^] | 1049 | i, CCI_PMU_MAX_HW_CNTRS(cci_pmu->model)); |
Wei Yongjun | fee4f2c | 2013-09-22 06:04:23 +0100 | [diff] [blame] | 1050 | return -EINVAL; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1051 | } |
| 1052 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 1053 | raw_spin_lock_init(&cci_pmu->hw_events.pmu_lock); |
| 1054 | mutex_init(&cci_pmu->reserve_mutex); |
| 1055 | atomic_set(&cci_pmu->active_events, 0); |
| 1056 | cpumask_set_cpu(smp_processor_id(), &cci_pmu->cpus); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1057 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 1058 | cci_pmu->cpu_nb = (struct notifier_block) { |
| 1059 | .notifier_call = cci_pmu_cpu_notifier, |
| 1060 | /* |
| 1061 | * to migrate uncore events, our notifier should be executed |
| 1062 | * before perf core's notifier. |
| 1063 | */ |
| 1064 | .priority = CPU_PRI_PERF + 1, |
| 1065 | }; |
| 1066 | |
| 1067 | ret = register_cpu_notifier(&cci_pmu->cpu_nb); |
Mark Rutland | c6f85cb | 2014-06-30 12:20:21 +0100 | [diff] [blame] | 1068 | if (ret) |
| 1069 | return ret; |
| 1070 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 1071 | ret = cci_pmu_init(cci_pmu, pdev); |
| 1072 | if (ret) { |
| 1073 | unregister_cpu_notifier(&cci_pmu->cpu_nb); |
Wei Yongjun | fee4f2c | 2013-09-22 06:04:23 +0100 | [diff] [blame] | 1074 | return ret; |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 1075 | } |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1076 | |
Suzuki K. Poulose | a1a076d | 2015-05-26 10:53:11 +0100 | [diff] [blame] | 1077 | pr_info("ARM %s PMU driver probed", cci_pmu->model->name); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1078 | return 0; |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | static int cci_platform_probe(struct platform_device *pdev) |
| 1082 | { |
| 1083 | if (!cci_probed()) |
| 1084 | return -ENODEV; |
| 1085 | |
| 1086 | return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev); |
| 1087 | } |
| 1088 | |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 1089 | static struct platform_driver cci_pmu_driver = { |
| 1090 | .driver = { |
| 1091 | .name = DRIVER_NAME_PMU, |
| 1092 | .of_match_table = arm_cci_pmu_matches, |
| 1093 | }, |
| 1094 | .probe = cci_pmu_probe, |
| 1095 | }; |
| 1096 | |
| 1097 | static struct platform_driver cci_platform_driver = { |
| 1098 | .driver = { |
| 1099 | .name = DRIVER_NAME, |
| 1100 | .of_match_table = arm_cci_matches, |
| 1101 | }, |
| 1102 | .probe = cci_platform_probe, |
| 1103 | }; |
| 1104 | |
| 1105 | static int __init cci_platform_init(void) |
| 1106 | { |
| 1107 | int ret; |
| 1108 | |
| 1109 | ret = platform_driver_register(&cci_pmu_driver); |
| 1110 | if (ret) |
| 1111 | return ret; |
| 1112 | |
| 1113 | return platform_driver_register(&cci_platform_driver); |
| 1114 | } |
| 1115 | |
Suzuki K. Poulose | ee8e5d5 | 2015-03-18 12:24:41 +0000 | [diff] [blame] | 1116 | #else /* !CONFIG_ARM_CCI400_PMU */ |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 1117 | |
| 1118 | static int __init cci_platform_init(void) |
| 1119 | { |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
Suzuki K. Poulose | ee8e5d5 | 2015-03-18 12:24:41 +0000 | [diff] [blame] | 1123 | #endif /* CONFIG_ARM_CCI400_PMU */ |
| 1124 | |
| 1125 | #ifdef CONFIG_ARM_CCI400_PORT_CTRL |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1126 | |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 1127 | #define CCI_PORT_CTRL 0x0 |
| 1128 | #define CCI_CTRL_STATUS 0xc |
| 1129 | |
| 1130 | #define CCI_ENABLE_SNOOP_REQ 0x1 |
| 1131 | #define CCI_ENABLE_DVM_REQ 0x2 |
| 1132 | #define CCI_ENABLE_REQ (CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ) |
| 1133 | |
| 1134 | enum cci_ace_port_type { |
| 1135 | ACE_INVALID_PORT = 0x0, |
| 1136 | ACE_PORT, |
| 1137 | ACE_LITE_PORT, |
| 1138 | }; |
| 1139 | |
| 1140 | struct cci_ace_port { |
| 1141 | void __iomem *base; |
| 1142 | unsigned long phys; |
| 1143 | enum cci_ace_port_type type; |
| 1144 | struct device_node *dn; |
| 1145 | }; |
| 1146 | |
| 1147 | static struct cci_ace_port *ports; |
| 1148 | static unsigned int nb_cci_ports; |
| 1149 | |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1150 | struct cpu_port { |
| 1151 | u64 mpidr; |
| 1152 | u32 port; |
| 1153 | }; |
Nicolas Pitre | 62158f8 | 2013-05-21 23:34:41 -0400 | [diff] [blame] | 1154 | |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1155 | /* |
| 1156 | * Use the port MSB as valid flag, shift can be made dynamic |
| 1157 | * by computing number of bits required for port indexes. |
| 1158 | * Code disabling CCI cpu ports runs with D-cache invalidated |
| 1159 | * and SCTLR bit clear so data accesses must be kept to a minimum |
| 1160 | * to improve performance; for now shift is left static to |
| 1161 | * avoid one more data access while disabling the CCI port. |
| 1162 | */ |
| 1163 | #define PORT_VALID_SHIFT 31 |
| 1164 | #define PORT_VALID (0x1 << PORT_VALID_SHIFT) |
| 1165 | |
| 1166 | static inline void init_cpu_port(struct cpu_port *port, u32 index, u64 mpidr) |
| 1167 | { |
| 1168 | port->port = PORT_VALID | index; |
| 1169 | port->mpidr = mpidr; |
| 1170 | } |
| 1171 | |
| 1172 | static inline bool cpu_port_is_valid(struct cpu_port *port) |
| 1173 | { |
| 1174 | return !!(port->port & PORT_VALID); |
| 1175 | } |
| 1176 | |
| 1177 | static inline bool cpu_port_match(struct cpu_port *port, u64 mpidr) |
| 1178 | { |
| 1179 | return port->mpidr == (mpidr & MPIDR_HWID_BITMASK); |
| 1180 | } |
| 1181 | |
| 1182 | static struct cpu_port cpu_port[NR_CPUS]; |
| 1183 | |
| 1184 | /** |
| 1185 | * __cci_ace_get_port - Function to retrieve the port index connected to |
| 1186 | * a cpu or device. |
| 1187 | * |
| 1188 | * @dn: device node of the device to look-up |
| 1189 | * @type: port type |
| 1190 | * |
| 1191 | * Return value: |
| 1192 | * - CCI port index if success |
| 1193 | * - -ENODEV if failure |
| 1194 | */ |
| 1195 | static int __cci_ace_get_port(struct device_node *dn, int type) |
| 1196 | { |
| 1197 | int i; |
| 1198 | bool ace_match; |
| 1199 | struct device_node *cci_portn; |
| 1200 | |
| 1201 | cci_portn = of_parse_phandle(dn, "cci-control-port", 0); |
| 1202 | for (i = 0; i < nb_cci_ports; i++) { |
| 1203 | ace_match = ports[i].type == type; |
| 1204 | if (ace_match && cci_portn == ports[i].dn) |
| 1205 | return i; |
| 1206 | } |
| 1207 | return -ENODEV; |
| 1208 | } |
| 1209 | |
| 1210 | int cci_ace_get_port(struct device_node *dn) |
| 1211 | { |
| 1212 | return __cci_ace_get_port(dn, ACE_LITE_PORT); |
| 1213 | } |
| 1214 | EXPORT_SYMBOL_GPL(cci_ace_get_port); |
| 1215 | |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1216 | static void cci_ace_init_ports(void) |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1217 | { |
Sudeep KarkadaNagesha | 78b4d6e | 2013-06-17 14:51:48 +0100 | [diff] [blame] | 1218 | int port, cpu; |
| 1219 | struct device_node *cpun; |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1220 | |
| 1221 | /* |
| 1222 | * Port index look-up speeds up the function disabling ports by CPU, |
| 1223 | * since the logical to port index mapping is done once and does |
| 1224 | * not change after system boot. |
| 1225 | * The stashed index array is initialized for all possible CPUs |
| 1226 | * at probe time. |
| 1227 | */ |
Sudeep KarkadaNagesha | 78b4d6e | 2013-06-17 14:51:48 +0100 | [diff] [blame] | 1228 | for_each_possible_cpu(cpu) { |
| 1229 | /* too early to use cpu->of_node */ |
| 1230 | cpun = of_get_cpu_node(cpu, NULL); |
| 1231 | |
| 1232 | if (WARN(!cpun, "Missing cpu device node\n")) |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1233 | continue; |
| 1234 | |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1235 | port = __cci_ace_get_port(cpun, ACE_PORT); |
| 1236 | if (port < 0) |
| 1237 | continue; |
| 1238 | |
| 1239 | init_cpu_port(&cpu_port[cpu], port, cpu_logical_map(cpu)); |
| 1240 | } |
| 1241 | |
| 1242 | for_each_possible_cpu(cpu) { |
| 1243 | WARN(!cpu_port_is_valid(&cpu_port[cpu]), |
| 1244 | "CPU %u does not have an associated CCI port\n", |
| 1245 | cpu); |
| 1246 | } |
| 1247 | } |
| 1248 | /* |
| 1249 | * Functions to enable/disable a CCI interconnect slave port |
| 1250 | * |
| 1251 | * They are called by low-level power management code to disable slave |
| 1252 | * interfaces snoops and DVM broadcast. |
| 1253 | * Since they may execute with cache data allocation disabled and |
| 1254 | * after the caches have been cleaned and invalidated the functions provide |
| 1255 | * no explicit locking since they may run with D-cache disabled, so normal |
| 1256 | * cacheable kernel locks based on ldrex/strex may not work. |
| 1257 | * Locking has to be provided by BSP implementations to ensure proper |
| 1258 | * operations. |
| 1259 | */ |
| 1260 | |
| 1261 | /** |
| 1262 | * cci_port_control() - function to control a CCI port |
| 1263 | * |
| 1264 | * @port: index of the port to setup |
| 1265 | * @enable: if true enables the port, if false disables it |
| 1266 | */ |
| 1267 | static void notrace cci_port_control(unsigned int port, bool enable) |
| 1268 | { |
| 1269 | void __iomem *base = ports[port].base; |
| 1270 | |
| 1271 | writel_relaxed(enable ? CCI_ENABLE_REQ : 0, base + CCI_PORT_CTRL); |
| 1272 | /* |
| 1273 | * This function is called from power down procedures |
| 1274 | * and must not execute any instruction that might |
| 1275 | * cause the processor to be put in a quiescent state |
| 1276 | * (eg wfi). Hence, cpu_relax() can not be added to this |
| 1277 | * read loop to optimize power, since it might hide possibly |
| 1278 | * disruptive operations. |
| 1279 | */ |
| 1280 | while (readl_relaxed(cci_ctrl_base + CCI_CTRL_STATUS) & 0x1) |
| 1281 | ; |
| 1282 | } |
| 1283 | |
| 1284 | /** |
| 1285 | * cci_disable_port_by_cpu() - function to disable a CCI port by CPU |
| 1286 | * reference |
| 1287 | * |
| 1288 | * @mpidr: mpidr of the CPU whose CCI port should be disabled |
| 1289 | * |
| 1290 | * Disabling a CCI port for a CPU implies disabling the CCI port |
| 1291 | * controlling that CPU cluster. Code disabling CPU CCI ports |
| 1292 | * must make sure that the CPU running the code is the last active CPU |
| 1293 | * in the cluster ie all other CPUs are quiescent in a low power state. |
| 1294 | * |
| 1295 | * Return: |
| 1296 | * 0 on success |
| 1297 | * -ENODEV on port look-up failure |
| 1298 | */ |
| 1299 | int notrace cci_disable_port_by_cpu(u64 mpidr) |
| 1300 | { |
| 1301 | int cpu; |
| 1302 | bool is_valid; |
| 1303 | for (cpu = 0; cpu < nr_cpu_ids; cpu++) { |
| 1304 | is_valid = cpu_port_is_valid(&cpu_port[cpu]); |
| 1305 | if (is_valid && cpu_port_match(&cpu_port[cpu], mpidr)) { |
| 1306 | cci_port_control(cpu_port[cpu].port, false); |
| 1307 | return 0; |
| 1308 | } |
| 1309 | } |
| 1310 | return -ENODEV; |
| 1311 | } |
| 1312 | EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu); |
| 1313 | |
| 1314 | /** |
Nicolas Pitre | 62158f8 | 2013-05-21 23:34:41 -0400 | [diff] [blame] | 1315 | * cci_enable_port_for_self() - enable a CCI port for calling CPU |
| 1316 | * |
| 1317 | * Enabling a CCI port for the calling CPU implies enabling the CCI |
| 1318 | * port controlling that CPU's cluster. Caller must make sure that the |
| 1319 | * CPU running the code is the first active CPU in the cluster and all |
| 1320 | * other CPUs are quiescent in a low power state or waiting for this CPU |
| 1321 | * to complete the CCI initialization. |
| 1322 | * |
| 1323 | * Because this is called when the MMU is still off and with no stack, |
| 1324 | * the code must be position independent and ideally rely on callee |
| 1325 | * clobbered registers only. To achieve this we must code this function |
| 1326 | * entirely in assembler. |
| 1327 | * |
| 1328 | * On success this returns with the proper CCI port enabled. In case of |
| 1329 | * any failure this never returns as the inability to enable the CCI is |
| 1330 | * fatal and there is no possible recovery at this stage. |
| 1331 | */ |
| 1332 | asmlinkage void __naked cci_enable_port_for_self(void) |
| 1333 | { |
| 1334 | asm volatile ("\n" |
Arnd Bergmann | f490249 | 2013-06-03 15:15:36 +0200 | [diff] [blame] | 1335 | " .arch armv7-a\n" |
Nicolas Pitre | 62158f8 | 2013-05-21 23:34:41 -0400 | [diff] [blame] | 1336 | " mrc p15, 0, r0, c0, c0, 5 @ get MPIDR value \n" |
| 1337 | " and r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n" |
| 1338 | " adr r1, 5f \n" |
| 1339 | " ldr r2, [r1] \n" |
| 1340 | " add r1, r1, r2 @ &cpu_port \n" |
| 1341 | " add ip, r1, %[sizeof_cpu_port] \n" |
| 1342 | |
| 1343 | /* Loop over the cpu_port array looking for a matching MPIDR */ |
| 1344 | "1: ldr r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n" |
| 1345 | " cmp r2, r0 @ compare MPIDR \n" |
| 1346 | " bne 2f \n" |
| 1347 | |
| 1348 | /* Found a match, now test port validity */ |
| 1349 | " ldr r3, [r1, %[offsetof_cpu_port_port]] \n" |
| 1350 | " tst r3, #"__stringify(PORT_VALID)" \n" |
| 1351 | " bne 3f \n" |
| 1352 | |
| 1353 | /* no match, loop with the next cpu_port entry */ |
| 1354 | "2: add r1, r1, %[sizeof_struct_cpu_port] \n" |
| 1355 | " cmp r1, ip @ done? \n" |
| 1356 | " blo 1b \n" |
| 1357 | |
| 1358 | /* CCI port not found -- cheaply try to stall this CPU */ |
| 1359 | "cci_port_not_found: \n" |
| 1360 | " wfi \n" |
| 1361 | " wfe \n" |
| 1362 | " b cci_port_not_found \n" |
| 1363 | |
| 1364 | /* Use matched port index to look up the corresponding ports entry */ |
| 1365 | "3: bic r3, r3, #"__stringify(PORT_VALID)" \n" |
| 1366 | " adr r0, 6f \n" |
| 1367 | " ldmia r0, {r1, r2} \n" |
| 1368 | " sub r1, r1, r0 @ virt - phys \n" |
| 1369 | " ldr r0, [r0, r2] @ *(&ports) \n" |
| 1370 | " mov r2, %[sizeof_struct_ace_port] \n" |
| 1371 | " mla r0, r2, r3, r0 @ &ports[index] \n" |
| 1372 | " sub r0, r0, r1 @ virt_to_phys() \n" |
| 1373 | |
| 1374 | /* Enable the CCI port */ |
| 1375 | " ldr r0, [r0, %[offsetof_port_phys]] \n" |
Victor Kamensky | fdb07ae | 2013-10-15 21:50:34 -0700 | [diff] [blame] | 1376 | " mov r3, %[cci_enable_req]\n" |
Nicolas Pitre | 62158f8 | 2013-05-21 23:34:41 -0400 | [diff] [blame] | 1377 | " str r3, [r0, #"__stringify(CCI_PORT_CTRL)"] \n" |
| 1378 | |
| 1379 | /* poll the status reg for completion */ |
| 1380 | " adr r1, 7f \n" |
| 1381 | " ldr r0, [r1] \n" |
| 1382 | " ldr r0, [r0, r1] @ cci_ctrl_base \n" |
| 1383 | "4: ldr r1, [r0, #"__stringify(CCI_CTRL_STATUS)"] \n" |
Victor Kamensky | fdb07ae | 2013-10-15 21:50:34 -0700 | [diff] [blame] | 1384 | " tst r1, %[cci_control_status_bits] \n" |
Nicolas Pitre | 62158f8 | 2013-05-21 23:34:41 -0400 | [diff] [blame] | 1385 | " bne 4b \n" |
| 1386 | |
| 1387 | " mov r0, #0 \n" |
| 1388 | " bx lr \n" |
| 1389 | |
| 1390 | " .align 2 \n" |
| 1391 | "5: .word cpu_port - . \n" |
| 1392 | "6: .word . \n" |
| 1393 | " .word ports - 6b \n" |
| 1394 | "7: .word cci_ctrl_phys - . \n" |
| 1395 | : : |
| 1396 | [sizeof_cpu_port] "i" (sizeof(cpu_port)), |
Victor Kamensky | fdb07ae | 2013-10-15 21:50:34 -0700 | [diff] [blame] | 1397 | [cci_enable_req] "i" cpu_to_le32(CCI_ENABLE_REQ), |
| 1398 | [cci_control_status_bits] "i" cpu_to_le32(1), |
Nicolas Pitre | 62158f8 | 2013-05-21 23:34:41 -0400 | [diff] [blame] | 1399 | #ifndef __ARMEB__ |
| 1400 | [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)), |
| 1401 | #else |
| 1402 | [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4), |
| 1403 | #endif |
| 1404 | [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)), |
| 1405 | [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)), |
| 1406 | [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)), |
| 1407 | [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) ); |
| 1408 | |
| 1409 | unreachable(); |
| 1410 | } |
| 1411 | |
| 1412 | /** |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1413 | * __cci_control_port_by_device() - function to control a CCI port by device |
| 1414 | * reference |
| 1415 | * |
| 1416 | * @dn: device node pointer of the device whose CCI port should be |
| 1417 | * controlled |
| 1418 | * @enable: if true enables the port, if false disables it |
| 1419 | * |
| 1420 | * Return: |
| 1421 | * 0 on success |
| 1422 | * -ENODEV on port look-up failure |
| 1423 | */ |
| 1424 | int notrace __cci_control_port_by_device(struct device_node *dn, bool enable) |
| 1425 | { |
| 1426 | int port; |
| 1427 | |
| 1428 | if (!dn) |
| 1429 | return -ENODEV; |
| 1430 | |
| 1431 | port = __cci_ace_get_port(dn, ACE_LITE_PORT); |
| 1432 | if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n", |
| 1433 | dn->full_name)) |
| 1434 | return -ENODEV; |
| 1435 | cci_port_control(port, enable); |
| 1436 | return 0; |
| 1437 | } |
| 1438 | EXPORT_SYMBOL_GPL(__cci_control_port_by_device); |
| 1439 | |
| 1440 | /** |
| 1441 | * __cci_control_port_by_index() - function to control a CCI port by port index |
| 1442 | * |
| 1443 | * @port: port index previously retrieved with cci_ace_get_port() |
| 1444 | * @enable: if true enables the port, if false disables it |
| 1445 | * |
| 1446 | * Return: |
| 1447 | * 0 on success |
| 1448 | * -ENODEV on port index out of range |
| 1449 | * -EPERM if operation carried out on an ACE PORT |
| 1450 | */ |
| 1451 | int notrace __cci_control_port_by_index(u32 port, bool enable) |
| 1452 | { |
| 1453 | if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT) |
| 1454 | return -ENODEV; |
| 1455 | /* |
| 1456 | * CCI control for ports connected to CPUS is extremely fragile |
| 1457 | * and must be made to go through a specific and controlled |
| 1458 | * interface (ie cci_disable_port_by_cpu(); control by general purpose |
| 1459 | * indexing is therefore disabled for ACE ports. |
| 1460 | */ |
| 1461 | if (ports[port].type == ACE_PORT) |
| 1462 | return -EPERM; |
| 1463 | |
| 1464 | cci_port_control(port, enable); |
| 1465 | return 0; |
| 1466 | } |
| 1467 | EXPORT_SYMBOL_GPL(__cci_control_port_by_index); |
| 1468 | |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1469 | static const struct of_device_id arm_cci_ctrl_if_matches[] = { |
| 1470 | {.compatible = "arm,cci-400-ctrl-if", }, |
| 1471 | {}, |
| 1472 | }; |
| 1473 | |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 1474 | static int cci_probe_ports(struct device_node *np) |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1475 | { |
| 1476 | struct cci_nb_ports const *cci_config; |
| 1477 | int ret, i, nb_ace = 0, nb_ace_lite = 0; |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 1478 | struct device_node *cp; |
Nicolas Pitre | 62158f8 | 2013-05-21 23:34:41 -0400 | [diff] [blame] | 1479 | struct resource res; |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1480 | const char *match_str; |
| 1481 | bool is_ace; |
| 1482 | |
Abhilash Kesavan | 896ddd6 | 2015-01-10 08:41:35 +0530 | [diff] [blame] | 1483 | |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1484 | cci_config = of_match_node(arm_cci_matches, np)->data; |
| 1485 | if (!cci_config) |
| 1486 | return -ENODEV; |
| 1487 | |
| 1488 | nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite; |
| 1489 | |
Lorenzo Pieralisi | 7c76203 | 2014-01-27 10:50:37 +0000 | [diff] [blame] | 1490 | ports = kcalloc(nb_cci_ports, sizeof(*ports), GFP_KERNEL); |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1491 | if (!ports) |
| 1492 | return -ENOMEM; |
| 1493 | |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1494 | for_each_child_of_node(np, cp) { |
| 1495 | if (!of_match_node(arm_cci_ctrl_if_matches, cp)) |
| 1496 | continue; |
| 1497 | |
| 1498 | i = nb_ace + nb_ace_lite; |
| 1499 | |
| 1500 | if (i >= nb_cci_ports) |
| 1501 | break; |
| 1502 | |
| 1503 | if (of_property_read_string(cp, "interface-type", |
| 1504 | &match_str)) { |
| 1505 | WARN(1, "node %s missing interface-type property\n", |
| 1506 | cp->full_name); |
| 1507 | continue; |
| 1508 | } |
| 1509 | is_ace = strcmp(match_str, "ace") == 0; |
| 1510 | if (!is_ace && strcmp(match_str, "ace-lite")) { |
| 1511 | WARN(1, "node %s containing invalid interface-type property, skipping it\n", |
| 1512 | cp->full_name); |
| 1513 | continue; |
| 1514 | } |
| 1515 | |
Nicolas Pitre | 62158f8 | 2013-05-21 23:34:41 -0400 | [diff] [blame] | 1516 | ret = of_address_to_resource(cp, 0, &res); |
| 1517 | if (!ret) { |
| 1518 | ports[i].base = ioremap(res.start, resource_size(&res)); |
| 1519 | ports[i].phys = res.start; |
| 1520 | } |
| 1521 | if (ret || !ports[i].base) { |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1522 | WARN(1, "unable to ioremap CCI port %d\n", i); |
| 1523 | continue; |
| 1524 | } |
| 1525 | |
| 1526 | if (is_ace) { |
| 1527 | if (WARN_ON(nb_ace >= cci_config->nb_ace)) |
| 1528 | continue; |
| 1529 | ports[i].type = ACE_PORT; |
| 1530 | ++nb_ace; |
| 1531 | } else { |
| 1532 | if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite)) |
| 1533 | continue; |
| 1534 | ports[i].type = ACE_LITE_PORT; |
| 1535 | ++nb_ace_lite; |
| 1536 | } |
| 1537 | ports[i].dn = cp; |
| 1538 | } |
| 1539 | |
| 1540 | /* initialize a stashed array of ACE ports to speed-up look-up */ |
| 1541 | cci_ace_init_ports(); |
| 1542 | |
| 1543 | /* |
| 1544 | * Multi-cluster systems may need this data when non-coherent, during |
| 1545 | * cluster power-up/power-down. Make sure it reaches main memory. |
| 1546 | */ |
| 1547 | sync_cache_w(&cci_ctrl_base); |
Nicolas Pitre | 62158f8 | 2013-05-21 23:34:41 -0400 | [diff] [blame] | 1548 | sync_cache_w(&cci_ctrl_phys); |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1549 | sync_cache_w(&ports); |
| 1550 | sync_cache_w(&cpu_port); |
| 1551 | __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports); |
| 1552 | pr_info("ARM CCI driver probed\n"); |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 1553 | |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1554 | return 0; |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 1555 | } |
Suzuki K. Poulose | ee8e5d5 | 2015-03-18 12:24:41 +0000 | [diff] [blame] | 1556 | #else /* !CONFIG_ARM_CCI400_PORT_CTRL */ |
| 1557 | static inline int cci_probe_ports(struct device_node *np) |
| 1558 | { |
| 1559 | return 0; |
| 1560 | } |
| 1561 | #endif /* CONFIG_ARM_CCI400_PORT_CTRL */ |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1562 | |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 1563 | static int cci_probe(void) |
| 1564 | { |
| 1565 | int ret; |
| 1566 | struct device_node *np; |
| 1567 | struct resource res; |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1568 | |
Suzuki K. Poulose | f6b9e83 | 2015-03-18 12:24:38 +0000 | [diff] [blame] | 1569 | np = of_find_matching_node(NULL, arm_cci_matches); |
| 1570 | if(!np || !of_device_is_available(np)) |
| 1571 | return -ENODEV; |
| 1572 | |
| 1573 | ret = of_address_to_resource(np, 0, &res); |
| 1574 | if (!ret) { |
| 1575 | cci_ctrl_base = ioremap(res.start, resource_size(&res)); |
| 1576 | cci_ctrl_phys = res.start; |
| 1577 | } |
| 1578 | if (ret || !cci_ctrl_base) { |
| 1579 | WARN(1, "unable to ioremap CCI ctrl\n"); |
| 1580 | return -ENXIO; |
| 1581 | } |
| 1582 | |
| 1583 | return cci_probe_ports(np); |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | static int cci_init_status = -EAGAIN; |
| 1587 | static DEFINE_MUTEX(cci_probing); |
| 1588 | |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1589 | static int cci_init(void) |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1590 | { |
| 1591 | if (cci_init_status != -EAGAIN) |
| 1592 | return cci_init_status; |
| 1593 | |
| 1594 | mutex_lock(&cci_probing); |
| 1595 | if (cci_init_status == -EAGAIN) |
| 1596 | cci_init_status = cci_probe(); |
| 1597 | mutex_unlock(&cci_probing); |
| 1598 | return cci_init_status; |
| 1599 | } |
| 1600 | |
| 1601 | /* |
| 1602 | * To sort out early init calls ordering a helper function is provided to |
| 1603 | * check if the CCI driver has beed initialized. Function check if the driver |
| 1604 | * has been initialized, if not it calls the init function that probes |
| 1605 | * the driver and updates the return value. |
| 1606 | */ |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1607 | bool cci_probed(void) |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1608 | { |
| 1609 | return cci_init() == 0; |
| 1610 | } |
| 1611 | EXPORT_SYMBOL_GPL(cci_probed); |
| 1612 | |
| 1613 | early_initcall(cci_init); |
Punit Agrawal | b91c8f2 | 2013-08-22 14:41:51 +0100 | [diff] [blame] | 1614 | core_initcall(cci_platform_init); |
Lorenzo Pieralisi | ed69bdd | 2012-07-13 15:55:52 +0100 | [diff] [blame] | 1615 | MODULE_LICENSE("GPL"); |
| 1616 | MODULE_DESCRIPTION("ARM CCI support"); |