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