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