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