video: rockchip: vop: 3399: add power domain control
[firefly-linux-kernel-4.4.55.git] / drivers / bus / arm-cci.c
index d9d954eb0fa0135e5a868cec4850237ff56f8804..577cc4bf6a9d17987a2f6bf8e62935dc5aa71f01 100644 (file)
 
 #include <linux/arm-cci.h>
 #include <linux/io.h>
+#include <linux/interrupt.h>
 #include <linux/module.h>
-#include <linux/platform_device.h>
 #include <linux/of_address.h>
+#include <linux/of_irq.h>
+#include <linux/of_platform.h>
+#include <linux/perf_event.h>
+#include <linux/platform_device.h>
 #include <linux/slab.h>
+#include <linux/spinlock.h>
 
 #include <asm/cacheflush.h>
-#include <asm/irq_regs.h>
-#include <asm/pmu.h>
 #include <asm/smp_plat.h>
 
-#define DRIVER_NAME            "CCI"
-
-#define CCI_PORT_CTRL          0x0
-#define CCI_CTRL_STATUS                0xc
-
-#define CCI_ENABLE_SNOOP_REQ   0x1
-#define CCI_ENABLE_DVM_REQ     0x2
-#define CCI_ENABLE_REQ         (CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ)
+static void __iomem *cci_ctrl_base;
+static unsigned long cci_ctrl_phys;
 
+#ifdef CONFIG_ARM_CCI400_PORT_CTRL
 struct cci_nb_ports {
        unsigned int nb_ace;
        unsigned int nb_ace_lite;
 };
 
-enum cci_ace_port_type {
-       ACE_INVALID_PORT = 0x0,
-       ACE_PORT,
-       ACE_LITE_PORT,
+static const struct cci_nb_ports cci400_ports = {
+       .nb_ace = 2,
+       .nb_ace_lite = 3
 };
 
-struct cci_ace_port {
+#define CCI400_PORTS_DATA      (&cci400_ports)
+#else
+#define CCI400_PORTS_DATA      (NULL)
+#endif
+
+static const struct of_device_id arm_cci_matches[] = {
+#ifdef CONFIG_ARM_CCI400_COMMON
+       {.compatible = "arm,cci-400", .data = CCI400_PORTS_DATA },
+#endif
+#ifdef CONFIG_ARM_CCI500_PMU
+       { .compatible = "arm,cci-500", },
+#endif
+       {},
+};
+
+#ifdef CONFIG_ARM_CCI_PMU
+
+#define DRIVER_NAME            "ARM-CCI"
+#define DRIVER_NAME_PMU                DRIVER_NAME " PMU"
+
+#define CCI_PMCR               0x0100
+#define CCI_PID2               0x0fe8
+
+#define CCI_PMCR_CEN           0x00000001
+#define CCI_PMCR_NCNT_MASK     0x0000f800
+#define CCI_PMCR_NCNT_SHIFT    11
+
+#define CCI_PID2_REV_MASK      0xf0
+#define CCI_PID2_REV_SHIFT     4
+
+#define CCI_PMU_EVT_SEL                0x000
+#define CCI_PMU_CNTR           0x004
+#define CCI_PMU_CNTR_CTRL      0x008
+#define CCI_PMU_OVRFLW         0x00c
+
+#define CCI_PMU_OVRFLW_FLAG    1
+
+#define CCI_PMU_CNTR_SIZE(model)       ((model)->cntr_size)
+#define CCI_PMU_CNTR_BASE(model, idx)  ((idx) * CCI_PMU_CNTR_SIZE(model))
+#define CCI_PMU_CNTR_MASK              ((1ULL << 32) -1)
+#define CCI_PMU_CNTR_LAST(cci_pmu)     (cci_pmu->num_cntrs - 1)
+
+#define CCI_PMU_MAX_HW_CNTRS(model) \
+       ((model)->num_hw_cntrs + (model)->fixed_hw_cntrs)
+
+/* Types of interfaces that can generate events */
+enum {
+       CCI_IF_SLAVE,
+       CCI_IF_MASTER,
+#ifdef CONFIG_ARM_CCI500_PMU
+       CCI_IF_GLOBAL,
+#endif
+       CCI_IF_MAX,
+};
+
+struct event_range {
+       u32 min;
+       u32 max;
+};
+
+struct cci_pmu_hw_events {
+       struct perf_event **events;
+       unsigned long *used_mask;
+       raw_spinlock_t pmu_lock;
+};
+
+struct cci_pmu;
+/*
+ * struct cci_pmu_model:
+ * @fixed_hw_cntrs - Number of fixed event counters
+ * @num_hw_cntrs - Maximum number of programmable event counters
+ * @cntr_size - Size of an event counter mapping
+ */
+struct cci_pmu_model {
+       char *name;
+       u32 fixed_hw_cntrs;
+       u32 num_hw_cntrs;
+       u32 cntr_size;
+       u64 nformat_attrs;
+       u64 nevent_attrs;
+       struct dev_ext_attribute *format_attrs;
+       struct dev_ext_attribute *event_attrs;
+       struct event_range event_ranges[CCI_IF_MAX];
+       int (*validate_hw_event)(struct cci_pmu *, unsigned long);
+       int (*get_event_idx)(struct cci_pmu *, struct cci_pmu_hw_events *, unsigned long);
+};
+
+static struct cci_pmu_model cci_pmu_models[];
+
+struct cci_pmu {
        void __iomem *base;
-       unsigned long phys;
-       enum cci_ace_port_type type;
-       struct device_node *dn;
+       struct pmu pmu;
+       int nr_irqs;
+       int *irqs;
+       unsigned long active_irqs;
+       const struct cci_pmu_model *model;
+       struct cci_pmu_hw_events hw_events;
+       struct platform_device *plat_device;
+       int num_cntrs;
+       atomic_t active_events;
+       struct mutex reserve_mutex;
+       struct notifier_block cpu_nb;
+       cpumask_t cpus;
 };
 
-static struct cci_ace_port *ports;
-static unsigned int nb_cci_ports;
+#define to_cci_pmu(c)  (container_of(c, struct cci_pmu, pmu))
 
-static void __iomem *cci_ctrl_base;
-static unsigned long cci_ctrl_phys;
+enum cci_models {
+#ifdef CONFIG_ARM_CCI400_PMU
+       CCI400_R0,
+       CCI400_R1,
+#endif
+#ifdef CONFIG_ARM_CCI500_PMU
+       CCI500_R0,
+#endif
+       CCI_MODEL_MAX
+};
 
-#ifdef CONFIG_HW_PERF_EVENTS
+static ssize_t cci_pmu_format_show(struct device *dev,
+                       struct device_attribute *attr, char *buf);
+static ssize_t cci_pmu_event_show(struct device *dev,
+                       struct device_attribute *attr, char *buf);
 
-static void __iomem *cci_pmu_base;
+#define CCI_EXT_ATTR_ENTRY(_name, _func, _config) \
+       { __ATTR(_name, S_IRUGO, _func, NULL), (void *)_config }
 
-#define CCI400_PMCR            0x0100
+#define CCI_FORMAT_EXT_ATTR_ENTRY(_name, _config) \
+       CCI_EXT_ATTR_ENTRY(_name, cci_pmu_format_show, (char *)_config)
+#define CCI_EVENT_EXT_ATTR_ENTRY(_name, _config) \
+       CCI_EXT_ATTR_ENTRY(_name, cci_pmu_event_show, (unsigned long)_config)
 
-#define CCI400_PMU_CYCLE_CNTR_BASE    0x0000
-#define CCI400_PMU_CNTR_BASE(idx)     (CCI400_PMU_CYCLE_CNTR_BASE + (idx) * 0x1000)
+/* CCI400 PMU Specific definitions */
 
-#define CCI400_PMCR_CEN          0x00000001
-#define CCI400_PMCR_RST          0x00000002
-#define CCI400_PMCR_CCR          0x00000004
-#define CCI400_PMCR_CCD          0x00000008
-#define CCI400_PMCR_EX           0x00000010
-#define CCI400_PMCR_DP           0x00000020
-#define CCI400_PMCR_NCNT_MASK    0x0000F800
-#define CCI400_PMCR_NCNT_SHIFT   11
+#ifdef CONFIG_ARM_CCI400_PMU
 
-#define CCI400_PMU_EVT_SEL       0x000
-#define CCI400_PMU_CNTR          0x004
-#define CCI400_PMU_CNTR_CTRL     0x008
-#define CCI400_PMU_OVERFLOW      0x00C
+/* Port ids */
+#define CCI400_PORT_S0         0
+#define CCI400_PORT_S1         1
+#define CCI400_PORT_S2         2
+#define CCI400_PORT_S3         3
+#define CCI400_PORT_S4         4
+#define CCI400_PORT_M0         5
+#define CCI400_PORT_M1         6
+#define CCI400_PORT_M2         7
 
-#define CCI400_PMU_OVERFLOW_FLAG 1
+#define CCI400_R1_PX           5
 
+/*
+ * Instead of an event id to monitor CCI cycles, a dedicated counter is
+ * provided. Use 0xff to represent CCI cycles and hope that no future revisions
+ * make use of this event in hardware.
+ */
 enum cci400_perf_events {
-       CCI400_PMU_CYCLES = 0xFF
+       CCI400_PMU_CYCLES = 0xff
 };
 
-#define CCI400_PMU_EVENT_MASK   0xff
-#define CCI400_PMU_EVENT_SOURCE(event) ((event >> 5) & 0x7)
-#define CCI400_PMU_EVENT_CODE(event) (event & 0x1f)
+#define CCI400_PMU_CYCLE_CNTR_IDX      0
+#define CCI400_PMU_CNTR0_IDX           1
 
-#define CCI400_PMU_EVENT_SOURCE_S0 0
-#define CCI400_PMU_EVENT_SOURCE_S4 4
-#define CCI400_PMU_EVENT_SOURCE_M0 5
-#define CCI400_PMU_EVENT_SOURCE_M2 7
+/*
+ * CCI PMU event id is an 8-bit value made of two parts - bits 7:5 for one of 8
+ * ports and bits 4:0 are event codes. There are different event codes
+ * associated with each port type.
+ *
+ * Additionally, the range of events associated with the port types changed
+ * between Rev0 and Rev1.
+ *
+ * The constants below define the range of valid codes for each port type for
+ * the different revisions and are used to validate the event to be monitored.
+ */
 
-#define CCI400_PMU_EVENT_SLAVE_MIN 0x0
-#define CCI400_PMU_EVENT_SLAVE_MAX 0x13
+#define CCI400_PMU_EVENT_MASK          0xffUL
+#define CCI400_PMU_EVENT_SOURCE_SHIFT  5
+#define CCI400_PMU_EVENT_SOURCE_MASK   0x7
+#define CCI400_PMU_EVENT_CODE_SHIFT    0
+#define CCI400_PMU_EVENT_CODE_MASK     0x1f
+#define CCI400_PMU_EVENT_SOURCE(event) \
+       ((event >> CCI400_PMU_EVENT_SOURCE_SHIFT) & \
+                       CCI400_PMU_EVENT_SOURCE_MASK)
+#define CCI400_PMU_EVENT_CODE(event) \
+       ((event >> CCI400_PMU_EVENT_CODE_SHIFT) & CCI400_PMU_EVENT_CODE_MASK)
+
+#define CCI400_R0_SLAVE_PORT_MIN_EV    0x00
+#define CCI400_R0_SLAVE_PORT_MAX_EV    0x13
+#define CCI400_R0_MASTER_PORT_MIN_EV   0x14
+#define CCI400_R0_MASTER_PORT_MAX_EV   0x1a
+
+#define CCI400_R1_SLAVE_PORT_MIN_EV    0x00
+#define CCI400_R1_SLAVE_PORT_MAX_EV    0x14
+#define CCI400_R1_MASTER_PORT_MIN_EV   0x00
+#define CCI400_R1_MASTER_PORT_MAX_EV   0x11
+
+#define CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(_name, _config) \
+       CCI_EXT_ATTR_ENTRY(_name, cci400_pmu_cycle_event_show, \
+                                       (unsigned long)_config)
+
+static ssize_t cci400_pmu_cycle_event_show(struct device *dev,
+                       struct device_attribute *attr, char *buf);
+
+static struct dev_ext_attribute cci400_pmu_format_attrs[] = {
+       CCI_FORMAT_EXT_ATTR_ENTRY(event, "config:0-4"),
+       CCI_FORMAT_EXT_ATTR_ENTRY(source, "config:5-7"),
+};
 
-#define CCI400_PMU_EVENT_MASTER_MIN 0x14
-#define CCI400_PMU_EVENT_MASTER_MAX 0x1A
+static struct dev_ext_attribute cci400_r0_pmu_event_attrs[] = {
+       /* Slave events */
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_any, 0x0),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_device, 0x01),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_normal_or_nonshareable, 0x2),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_inner_or_outershareable, 0x3),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maintenance, 0x4),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_mem_barrier, 0x5),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_sync_barrier, 0x6),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg_sync, 0x8),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_tt_full, 0x9),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_last_hs_snoop, 0xA),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall_rvalids_h_rready_l, 0xB),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_any, 0xC),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_device, 0xD),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_normal_or_nonshareable, 0xE),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_inner_or_outershare_wback_wclean, 0xF),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_unique, 0x10),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_line_unique, 0x11),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_evict, 0x12),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall_tt_full, 0x13),
+       /* Master events */
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_retry_speculative_fetch, 0x14),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_addr_hazard, 0x15),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_id_hazard, 0x16),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_tt_full, 0x17),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_barrier_hazard, 0x18),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_barrier_hazard, 0x19),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_tt_full, 0x1A),
+       /* Special event for cycles counter */
+       CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(cycles, 0xff),
+};
 
-#define CCI400_PMU_MAX_HW_EVENTS 5   /* CCI PMU has 4 counters + 1 cycle counter */
+static struct dev_ext_attribute cci400_r1_pmu_event_attrs[] = {
+       /* Slave events */
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_any, 0x0),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_device, 0x01),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_normal_or_nonshareable, 0x2),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_inner_or_outershareable, 0x3),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maintenance, 0x4),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_mem_barrier, 0x5),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_sync_barrier, 0x6),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg_sync, 0x8),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_tt_full, 0x9),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_last_hs_snoop, 0xA),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall_rvalids_h_rready_l, 0xB),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_any, 0xC),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_device, 0xD),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_normal_or_nonshareable, 0xE),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_inner_or_outershare_wback_wclean, 0xF),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_unique, 0x10),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_write_line_unique, 0x11),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_evict, 0x12),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall_tt_full, 0x13),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_slave_id_hazard, 0x14),
+       /* Master events */
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_retry_speculative_fetch, 0x0),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_stall_cycle_addr_hazard, 0x1),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_master_id_hazard, 0x2),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_hi_prio_rtq_full, 0x3),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_barrier_hazard, 0x4),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_barrier_hazard, 0x5),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_wtq_full, 0x6),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_low_prio_rtq_full, 0x7),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_mid_prio_rtq_full, 0x8),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn0, 0x9),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn1, 0xA),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn2, 0xB),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall_qvn_vn3, 0xC),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn0, 0xD),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn1, 0xE),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn2, 0xF),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall_qvn_vn3, 0x10),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_unique_or_line_unique_addr_hazard, 0x11),
+       /* Special event for cycles counter */
+       CCI400_CYCLE_EVENT_EXT_ATTR_ENTRY(cycles, 0xff),
+};
 
-#define CCI400_PMU_CYCLE_COUNTER_IDX 0
-#define CCI400_PMU_COUNTER0_IDX      1
-#define CCI400_PMU_COUNTER_LAST(cci_pmu) (CCI400_PMU_CYCLE_COUNTER_IDX + cci_pmu->num_events - 1)
+static ssize_t cci400_pmu_cycle_event_show(struct device *dev,
+                       struct device_attribute *attr, char *buf)
+{
+       struct dev_ext_attribute *eattr = container_of(attr,
+                               struct dev_ext_attribute, attr);
+       return snprintf(buf, PAGE_SIZE, "config=0x%lx\n", (unsigned long)eattr->var);
+}
 
+static int cci400_get_event_idx(struct cci_pmu *cci_pmu,
+                               struct cci_pmu_hw_events *hw,
+                               unsigned long cci_event)
+{
+       int idx;
 
-static struct perf_event *events[CCI400_PMU_MAX_HW_EVENTS];
-static unsigned long used_mask[BITS_TO_LONGS(CCI400_PMU_MAX_HW_EVENTS)];
-static struct pmu_hw_events cci_hw_events = {
-       .events    = events,
-       .used_mask = used_mask,
-};
+       /* cycles event idx is fixed */
+       if (cci_event == CCI400_PMU_CYCLES) {
+               if (test_and_set_bit(CCI400_PMU_CYCLE_CNTR_IDX, hw->used_mask))
+                       return -EAGAIN;
 
-static int cci_pmu_validate_hw_event(u8 hw_event)
+               return CCI400_PMU_CYCLE_CNTR_IDX;
+       }
+
+       for (idx = CCI400_PMU_CNTR0_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); ++idx)
+               if (!test_and_set_bit(idx, hw->used_mask))
+                       return idx;
+
+       /* No counters available */
+       return -EAGAIN;
+}
+
+static int cci400_validate_hw_event(struct cci_pmu *cci_pmu, unsigned long hw_event)
 {
        u8 ev_source = CCI400_PMU_EVENT_SOURCE(hw_event);
        u8 ev_code = CCI400_PMU_EVENT_CODE(hw_event);
+       int if_type;
 
-       if (ev_source <= CCI400_PMU_EVENT_SOURCE_S4 &&
-           ev_code <= CCI400_PMU_EVENT_SLAVE_MAX)
-                       return hw_event;
-       else if (CCI400_PMU_EVENT_SOURCE_M0 <= ev_source &&
-                  ev_source <= CCI400_PMU_EVENT_SOURCE_M2 &&
-                  CCI400_PMU_EVENT_MASTER_MIN <= ev_code &&
-                   ev_code <= CCI400_PMU_EVENT_MASTER_MAX)
-                       return hw_event;
+       if (hw_event & ~CCI400_PMU_EVENT_MASK)
+               return -ENOENT;
 
-       return -EINVAL;
+       if (hw_event == CCI400_PMU_CYCLES)
+               return hw_event;
+
+       switch (ev_source) {
+       case CCI400_PORT_S0:
+       case CCI400_PORT_S1:
+       case CCI400_PORT_S2:
+       case CCI400_PORT_S3:
+       case CCI400_PORT_S4:
+               /* Slave Interface */
+               if_type = CCI_IF_SLAVE;
+               break;
+       case CCI400_PORT_M0:
+       case CCI400_PORT_M1:
+       case CCI400_PORT_M2:
+               /* Master Interface */
+               if_type = CCI_IF_MASTER;
+               break;
+       default:
+               return -ENOENT;
+       }
+
+       if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
+               ev_code <= cci_pmu->model->event_ranges[if_type].max)
+               return hw_event;
+
+       return -ENOENT;
 }
 
-static inline int cci_pmu_counter_is_valid(struct arm_pmu *cci_pmu, int idx)
+static int probe_cci400_revision(void)
 {
-       return CCI400_PMU_CYCLE_COUNTER_IDX <= idx &&
-               idx <= CCI400_PMU_COUNTER_LAST(cci_pmu);
+       int rev;
+       rev = readl_relaxed(cci_ctrl_base + CCI_PID2) & CCI_PID2_REV_MASK;
+       rev >>= CCI_PID2_REV_SHIFT;
+
+       if (rev < CCI400_R1_PX)
+               return CCI400_R0;
+       else
+               return CCI400_R1;
 }
 
-static inline u32 cci_pmu_read_register(int idx, unsigned int offset)
+static const struct cci_pmu_model *probe_cci_model(struct platform_device *pdev)
+{
+       if (platform_has_secure_cci_access())
+               return &cci_pmu_models[probe_cci400_revision()];
+       return NULL;
+}
+#else  /* !CONFIG_ARM_CCI400_PMU */
+static inline struct cci_pmu_model *probe_cci_model(struct platform_device *pdev)
 {
-       return readl_relaxed(cci_pmu_base + CCI400_PMU_CNTR_BASE(idx) + offset);
+       return NULL;
 }
+#endif /* CONFIG_ARM_CCI400_PMU */
+
+#ifdef CONFIG_ARM_CCI500_PMU
 
-static inline void cci_pmu_write_register(u32 value, int idx, unsigned int offset)
+/*
+ * CCI500 provides 8 independent event counters that can count
+ * any of the events available.
+ *
+ * CCI500 PMU event id is an 9-bit value made of two parts.
+ *      bits [8:5] - Source for the event
+ *                   0x0-0x6 - Slave interfaces
+ *                   0x8-0xD - Master interfaces
+ *                   0xf     - Global Events
+ *                   0x7,0xe - Reserved
+ *
+ *      bits [4:0] - Event code (specific to type of interface)
+ */
+
+/* Port ids */
+#define CCI500_PORT_S0                 0x0
+#define CCI500_PORT_S1                 0x1
+#define CCI500_PORT_S2                 0x2
+#define CCI500_PORT_S3                 0x3
+#define CCI500_PORT_S4                 0x4
+#define CCI500_PORT_S5                 0x5
+#define CCI500_PORT_S6                 0x6
+
+#define CCI500_PORT_M0                 0x8
+#define CCI500_PORT_M1                 0x9
+#define CCI500_PORT_M2                 0xa
+#define CCI500_PORT_M3                 0xb
+#define CCI500_PORT_M4                 0xc
+#define CCI500_PORT_M5                 0xd
+
+#define CCI500_PORT_GLOBAL             0xf
+
+#define CCI500_PMU_EVENT_MASK          0x1ffUL
+#define CCI500_PMU_EVENT_SOURCE_SHIFT  0x5
+#define CCI500_PMU_EVENT_SOURCE_MASK   0xf
+#define CCI500_PMU_EVENT_CODE_SHIFT    0x0
+#define CCI500_PMU_EVENT_CODE_MASK     0x1f
+
+#define CCI500_PMU_EVENT_SOURCE(event) \
+       ((event >> CCI500_PMU_EVENT_SOURCE_SHIFT) & CCI500_PMU_EVENT_SOURCE_MASK)
+#define CCI500_PMU_EVENT_CODE(event)   \
+       ((event >> CCI500_PMU_EVENT_CODE_SHIFT) & CCI500_PMU_EVENT_CODE_MASK)
+
+#define CCI500_SLAVE_PORT_MIN_EV       0x00
+#define CCI500_SLAVE_PORT_MAX_EV       0x1f
+#define CCI500_MASTER_PORT_MIN_EV      0x00
+#define CCI500_MASTER_PORT_MAX_EV      0x06
+#define CCI500_GLOBAL_PORT_MIN_EV      0x00
+#define CCI500_GLOBAL_PORT_MAX_EV      0x0f
+
+
+#define CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(_name, _config) \
+       CCI_EXT_ATTR_ENTRY(_name, cci500_pmu_global_event_show, \
+                                       (unsigned long) _config)
+
+static ssize_t cci500_pmu_global_event_show(struct device *dev,
+                               struct device_attribute *attr, char *buf);
+
+static struct dev_ext_attribute cci500_pmu_format_attrs[] = {
+       CCI_FORMAT_EXT_ATTR_ENTRY(event, "config:0-4"),
+       CCI_FORMAT_EXT_ATTR_ENTRY(source, "config:5-8"),
+};
+
+static struct dev_ext_attribute cci500_pmu_event_attrs[] = {
+       /* Slave events */
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_arvalid, 0x0),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_dev, 0x1),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_nonshareable, 0x2),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_shareable_non_alloc, 0x3),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_shareable_alloc, 0x4),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_invalidate, 0x5),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_cache_maint, 0x6),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_dvm_msg, 0x7),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_rval, 0x8),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_hs_rlast_snoop, 0x9),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_hs_awalid, 0xA),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_dev, 0xB),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_non_shareable, 0xC),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wb, 0xD),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wlu, 0xE),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_share_wunique, 0xF),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_evict, 0x10),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_wrevict, 0x11),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_w_data_beat, 0x12),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_srq_acvalid, 0x13),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_srq_read, 0x14),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_srq_clean, 0x15),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_srq_data_transfer_low, 0x16),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rrq_stall_arvalid, 0x17),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_r_data_stall, 0x18),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_wrq_stall, 0x19),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_w_data_stall, 0x1A),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_w_resp_stall, 0x1B),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_srq_stall, 0x1C),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_s_data_stall, 0x1D),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_rq_stall_ot_limit, 0x1E),
+       CCI_EVENT_EXT_ATTR_ENTRY(si_r_stall_arbit, 0x1F),
+
+       /* Master events */
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_r_data_beat_any, 0x0),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_w_data_beat_any, 0x1),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_rrq_stall, 0x2),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_r_data_stall, 0x3),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_wrq_stall, 0x4),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_w_data_stall, 0x5),
+       CCI_EVENT_EXT_ATTR_ENTRY(mi_w_resp_stall, 0x6),
+
+       /* Global events */
+       CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_0_1, 0x0),
+       CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_2_3, 0x1),
+       CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_4_5, 0x2),
+       CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_filter_bank_6_7, 0x3),
+       CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_0_1, 0x4),
+       CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_2_3, 0x5),
+       CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_4_5, 0x6),
+       CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_access_miss_filter_bank_6_7, 0x7),
+       CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_back_invalidation, 0x8),
+       CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_stall_alloc_busy, 0x9),
+       CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_stall_tt_full, 0xA),
+       CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_wrq, 0xB),
+       CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_cd_hs, 0xC),
+       CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_rq_stall_addr_hazard, 0xD),
+       CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snopp_rq_stall_tt_full, 0xE),
+       CCI500_GLOBAL_EVENT_EXT_ATTR_ENTRY(cci_snoop_rq_tzmp1_prot, 0xF),
+};
+
+static ssize_t cci500_pmu_global_event_show(struct device *dev,
+                               struct device_attribute *attr, char *buf)
 {
-       return writel_relaxed(value, cci_pmu_base + CCI400_PMU_CNTR_BASE(idx) + offset);
+       struct dev_ext_attribute *eattr = container_of(attr,
+                                       struct dev_ext_attribute, attr);
+       /* Global events have single fixed source code */
+       return snprintf(buf, PAGE_SIZE, "event=0x%lx,source=0x%x\n",
+                               (unsigned long)eattr->var, CCI500_PORT_GLOBAL);
 }
 
-static inline void cci_pmu_disable_counter(int idx)
+static int cci500_validate_hw_event(struct cci_pmu *cci_pmu,
+                                       unsigned long hw_event)
 {
-       cci_pmu_write_register(0, idx, CCI400_PMU_CNTR_CTRL);
+       u32 ev_source = CCI500_PMU_EVENT_SOURCE(hw_event);
+       u32 ev_code = CCI500_PMU_EVENT_CODE(hw_event);
+       int if_type;
+
+       if (hw_event & ~CCI500_PMU_EVENT_MASK)
+               return -ENOENT;
+
+       switch (ev_source) {
+       case CCI500_PORT_S0:
+       case CCI500_PORT_S1:
+       case CCI500_PORT_S2:
+       case CCI500_PORT_S3:
+       case CCI500_PORT_S4:
+       case CCI500_PORT_S5:
+       case CCI500_PORT_S6:
+               if_type = CCI_IF_SLAVE;
+               break;
+       case CCI500_PORT_M0:
+       case CCI500_PORT_M1:
+       case CCI500_PORT_M2:
+       case CCI500_PORT_M3:
+       case CCI500_PORT_M4:
+       case CCI500_PORT_M5:
+               if_type = CCI_IF_MASTER;
+               break;
+       case CCI500_PORT_GLOBAL:
+               if_type = CCI_IF_GLOBAL;
+               break;
+       default:
+               return -ENOENT;
+       }
+
+       if (ev_code >= cci_pmu->model->event_ranges[if_type].min &&
+               ev_code <= cci_pmu->model->event_ranges[if_type].max)
+               return hw_event;
+
+       return -ENOENT;
+}
+#endif /* CONFIG_ARM_CCI500_PMU */
+
+static ssize_t cci_pmu_format_show(struct device *dev,
+                       struct device_attribute *attr, char *buf)
+{
+       struct dev_ext_attribute *eattr = container_of(attr,
+                               struct dev_ext_attribute, attr);
+       return snprintf(buf, PAGE_SIZE, "%s\n", (char *)eattr->var);
 }
 
-static inline void cci_pmu_enable_counter(int idx)
+static ssize_t cci_pmu_event_show(struct device *dev,
+                       struct device_attribute *attr, char *buf)
 {
-       cci_pmu_write_register(1, idx, CCI400_PMU_CNTR_CTRL);
+       struct dev_ext_attribute *eattr = container_of(attr,
+                               struct dev_ext_attribute, attr);
+       /* source parameter is mandatory for normal PMU events */
+       return snprintf(buf, PAGE_SIZE, "source=?,event=0x%lx\n",
+                                        (unsigned long)eattr->var);
 }
 
-static inline void cci_pmu_select_event(int idx, unsigned long event)
+static int pmu_is_valid_counter(struct cci_pmu *cci_pmu, int idx)
 {
-       event &= CCI400_PMU_EVENT_MASK;
-       cci_pmu_write_register(event, idx, CCI400_PMU_EVT_SEL);
+       return 0 <= idx && idx <= CCI_PMU_CNTR_LAST(cci_pmu);
 }
 
-static u32 cci_pmu_get_max_counters(void)
+static u32 pmu_read_register(struct cci_pmu *cci_pmu, int idx, unsigned int offset)
 {
-       u32 n_cnts = (readl_relaxed(cci_ctrl_base + CCI400_PMCR) &
-                     CCI400_PMCR_NCNT_MASK) >> CCI400_PMCR_NCNT_SHIFT;
+       return readl_relaxed(cci_pmu->base +
+                            CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
+}
 
-       /* add 1 for cycle counter */
-       return n_cnts + 1;
+static void pmu_write_register(struct cci_pmu *cci_pmu, u32 value,
+                              int idx, unsigned int offset)
+{
+       return writel_relaxed(value, cci_pmu->base +
+                             CCI_PMU_CNTR_BASE(cci_pmu->model, idx) + offset);
 }
 
-static struct pmu_hw_events *cci_pmu_get_hw_events(void)
+static void pmu_disable_counter(struct cci_pmu *cci_pmu, int idx)
 {
-       return &cci_hw_events;
+       pmu_write_register(cci_pmu, 0, idx, CCI_PMU_CNTR_CTRL);
 }
 
-static int cci_pmu_get_event_idx(struct pmu_hw_events *hw, struct perf_event *event)
+static void pmu_enable_counter(struct cci_pmu *cci_pmu, int idx)
 {
-       struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
-       struct hw_perf_event *hw_event = &event->hw;
-       unsigned long cci_event = hw_event->config_base & CCI400_PMU_EVENT_MASK;
-       int idx;
+       pmu_write_register(cci_pmu, 1, idx, CCI_PMU_CNTR_CTRL);
+}
 
-       if (cci_event == CCI400_PMU_CYCLES) {
-               if (test_and_set_bit(CCI400_PMU_CYCLE_COUNTER_IDX, hw->used_mask))
-                       return -EAGAIN;
+static void pmu_set_event(struct cci_pmu *cci_pmu, int idx, unsigned long event)
+{
+       pmu_write_register(cci_pmu, event, idx, CCI_PMU_EVT_SEL);
+}
 
-                return CCI400_PMU_CYCLE_COUNTER_IDX;
-        }
+/*
+ * Returns the number of programmable counters actually implemented
+ * by the cci
+ */
+static u32 pmu_get_max_counters(void)
+{
+       return (readl_relaxed(cci_ctrl_base + CCI_PMCR) &
+               CCI_PMCR_NCNT_MASK) >> CCI_PMCR_NCNT_SHIFT;
+}
 
-       for (idx = CCI400_PMU_COUNTER0_IDX; idx <= CCI400_PMU_COUNTER_LAST(cci_pmu); ++idx) {
+static int pmu_get_event_idx(struct cci_pmu_hw_events *hw, struct perf_event *event)
+{
+       struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
+       unsigned long cci_event = event->hw.config_base;
+       int idx;
+
+       if (cci_pmu->model->get_event_idx)
+               return cci_pmu->model->get_event_idx(cci_pmu, hw, cci_event);
+
+       /* Generic code to find an unused idx from the mask */
+       for(idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++)
                if (!test_and_set_bit(idx, hw->used_mask))
                        return idx;
-       }
 
        /* No counters available */
        return -EAGAIN;
 }
 
-static int cci_pmu_map_event(struct perf_event *event)
+static int pmu_map_event(struct perf_event *event)
 {
-       int mapping;
-       u8 config = event->attr.config & CCI400_PMU_EVENT_MASK;
+       struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
 
-       if (event->attr.type < PERF_TYPE_MAX)
+       if (event->attr.type < PERF_TYPE_MAX ||
+                       !cci_pmu->model->validate_hw_event)
                return -ENOENT;
 
-       /* 0xff is used to represent CCI Cycles */
-       if (config == 0xff)
-               mapping = config;
-       else
-               mapping = cci_pmu_validate_hw_event(config);
-
-       return mapping;
+       return  cci_pmu->model->validate_hw_event(cci_pmu, event->attr.config);
 }
 
-static int cci_pmu_request_irq(struct arm_pmu *cci_pmu, irq_handler_t handler)
+static int pmu_request_irq(struct cci_pmu *cci_pmu, irq_handler_t handler)
 {
-       int irq, err, i = 0;
+       int i;
        struct platform_device *pmu_device = cci_pmu->plat_device;
 
        if (unlikely(!pmu_device))
                return -ENODEV;
 
-       /* CCI exports 6 interrupts - 1 nERRORIRQ + 5 nEVNTCNTOVERFLOW (PMU)
-          nERRORIRQ will be handled by secure firmware on TC2. So we
-          assume that all CCI interrupts listed in the linux device
-          tree are PMU interrupts.
+       if (cci_pmu->nr_irqs < 1) {
+               dev_err(&pmu_device->dev, "no irqs for CCI PMUs defined\n");
+               return -ENODEV;
+       }
 
-          The following code should then be able to handle different routing
-          of the CCI PMU interrupts.
-       */
-       while ((irq = platform_get_irq(pmu_device, i)) > 0) {
-               err = request_irq(irq, handler, 0, "arm-cci-pmu", cci_pmu);
+       /*
+        * Register all available CCI PMU interrupts. In the interrupt handler
+        * we iterate over the counters checking for interrupt source (the
+        * overflowing counter) and clear it.
+        *
+        * This should allow handling of non-unique interrupt for the counters.
+        */
+       for (i = 0; i < cci_pmu->nr_irqs; i++) {
+               int err = request_irq(cci_pmu->irqs[i], handler, IRQF_SHARED,
+                               "arm-cci-pmu", cci_pmu);
                if (err) {
                        dev_err(&pmu_device->dev, "unable to request IRQ%d for ARM CCI PMU counters\n",
-                               irq);
+                               cci_pmu->irqs[i]);
                        return err;
                }
-               i++;
+
+               set_bit(i, &cci_pmu->active_irqs);
        }
 
        return 0;
 }
 
-static irqreturn_t cci_pmu_handle_irq(int irq_num, void *dev)
+static void pmu_free_irq(struct cci_pmu *cci_pmu)
 {
-       struct arm_pmu *cci_pmu = (struct arm_pmu *)dev;
-       struct pmu_hw_events *events = cci_pmu->get_hw_events();
-       struct perf_sample_data data;
-       struct pt_regs *regs;
-       int idx;
+       int i;
+
+       for (i = 0; i < cci_pmu->nr_irqs; i++) {
+               if (!test_and_clear_bit(i, &cci_pmu->active_irqs))
+                       continue;
+
+               free_irq(cci_pmu->irqs[i], cci_pmu);
+       }
+}
+
+static u32 pmu_read_counter(struct perf_event *event)
+{
+       struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
+       struct hw_perf_event *hw_counter = &event->hw;
+       int idx = hw_counter->idx;
+       u32 value;
+
+       if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
+               dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
+               return 0;
+       }
+       value = pmu_read_register(cci_pmu, idx, CCI_PMU_CNTR);
+
+       return value;
+}
+
+static void pmu_write_counter(struct perf_event *event, u32 value)
+{
+       struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
+       struct hw_perf_event *hw_counter = &event->hw;
+       int idx = hw_counter->idx;
+
+       if (unlikely(!pmu_is_valid_counter(cci_pmu, idx)))
+               dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
+       else
+               pmu_write_register(cci_pmu, value, idx, CCI_PMU_CNTR);
+}
+
+static u64 pmu_event_update(struct perf_event *event)
+{
+       struct hw_perf_event *hwc = &event->hw;
+       u64 delta, prev_raw_count, new_raw_count;
 
-       regs = get_irq_regs();
+       do {
+               prev_raw_count = local64_read(&hwc->prev_count);
+               new_raw_count = pmu_read_counter(event);
+       } while (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
+                new_raw_count) != prev_raw_count);
+
+       delta = (new_raw_count - prev_raw_count) & CCI_PMU_CNTR_MASK;
+
+       local64_add(delta, &event->count);
+
+       return new_raw_count;
+}
 
-       /* Iterate over counters and update the corresponding perf events.
-          This should work regardless of whether we have per-counter overflow
-          interrupt or a combined overflow interrupt. */
-       for (idx = CCI400_PMU_CYCLE_COUNTER_IDX; idx <= CCI400_PMU_COUNTER_LAST(cci_pmu); idx++) {
+static void pmu_read(struct perf_event *event)
+{
+       pmu_event_update(event);
+}
+
+void pmu_event_set_period(struct perf_event *event)
+{
+       struct hw_perf_event *hwc = &event->hw;
+       /*
+        * The CCI PMU counters have a period of 2^32. To account for the
+        * possiblity of extreme interrupt latency we program for a period of
+        * half that. Hopefully we can handle the interrupt before another 2^31
+        * events occur and the counter overtakes its previous value.
+        */
+       u64 val = 1ULL << 31;
+       local64_set(&hwc->prev_count, val);
+       pmu_write_counter(event, val);
+}
+
+static irqreturn_t pmu_handle_irq(int irq_num, void *dev)
+{
+       unsigned long flags;
+       struct cci_pmu *cci_pmu = dev;
+       struct cci_pmu_hw_events *events = &cci_pmu->hw_events;
+       int idx, handled = IRQ_NONE;
+
+       raw_spin_lock_irqsave(&events->pmu_lock, flags);
+       /*
+        * Iterate over counters and update the corresponding perf events.
+        * This should work regardless of whether we have per-counter overflow
+        * interrupt or a combined overflow interrupt.
+        */
+       for (idx = 0; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++) {
                struct perf_event *event = events->events[idx];
                struct hw_perf_event *hw_counter;
 
@@ -272,198 +826,812 @@ static irqreturn_t cci_pmu_handle_irq(int irq_num, void *dev)
                hw_counter = &event->hw;
 
                /* Did this counter overflow? */
-               if (!(cci_pmu_read_register(idx, CCI400_PMU_OVERFLOW) & CCI400_PMU_OVERFLOW_FLAG))
+               if (!(pmu_read_register(cci_pmu, idx, CCI_PMU_OVRFLW) &
+                     CCI_PMU_OVRFLW_FLAG))
                        continue;
-               cci_pmu_write_register(CCI400_PMU_OVERFLOW_FLAG, idx, CCI400_PMU_OVERFLOW);
 
-               armpmu_event_update(event);
-               perf_sample_data_init(&data, 0, hw_counter->last_period);
-               if (!armpmu_event_set_period(event))
-                       continue;
+               pmu_write_register(cci_pmu, CCI_PMU_OVRFLW_FLAG, idx,
+                                                       CCI_PMU_OVRFLW);
 
-               if (perf_event_overflow(event, &data, regs))
-                       cci_pmu->disable(event);
+               pmu_event_update(event);
+               pmu_event_set_period(event);
+               handled = IRQ_HANDLED;
        }
+       raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
 
-       irq_work_run();
-       return IRQ_HANDLED;
+       return IRQ_RETVAL(handled);
 }
 
-static void cci_pmu_free_irq(struct arm_pmu *cci_pmu)
+static int cci_pmu_get_hw(struct cci_pmu *cci_pmu)
 {
-       int irq, i = 0;
-       struct platform_device *pmu_device = cci_pmu->plat_device;
+       int ret = pmu_request_irq(cci_pmu, pmu_handle_irq);
+       if (ret) {
+               pmu_free_irq(cci_pmu);
+               return ret;
+       }
+       return 0;
+}
+
+static void cci_pmu_put_hw(struct cci_pmu *cci_pmu)
+{
+       pmu_free_irq(cci_pmu);
+}
+
+static void hw_perf_event_destroy(struct perf_event *event)
+{
+       struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
+       atomic_t *active_events = &cci_pmu->active_events;
+       struct mutex *reserve_mutex = &cci_pmu->reserve_mutex;
 
-       while ((irq = platform_get_irq(pmu_device, i)) > 0) {
-               free_irq(irq, cci_pmu);
-               i++;
+       if (atomic_dec_and_mutex_lock(active_events, reserve_mutex)) {
+               cci_pmu_put_hw(cci_pmu);
+               mutex_unlock(reserve_mutex);
        }
 }
 
-static void cci_pmu_enable_event(struct perf_event *event)
+static void cci_pmu_enable(struct pmu *pmu)
 {
+       struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
+       struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
+       int enabled = bitmap_weight(hw_events->used_mask, cci_pmu->num_cntrs);
        unsigned long flags;
-       struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
-       struct pmu_hw_events *events = cci_pmu->get_hw_events();
-       struct hw_perf_event *hw_counter = &event->hw;
-       int idx = hw_counter->idx;
+       u32 val;
 
-       if (unlikely(!cci_pmu_counter_is_valid(cci_pmu, idx))) {
-               dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
+       if (!enabled)
                return;
-       }
 
-       raw_spin_lock_irqsave(&events->pmu_lock, flags);
+       raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
 
-       /* Configure the event to count, unless you are counting cycles */
-       if (idx != CCI400_PMU_CYCLE_COUNTER_IDX)
-               cci_pmu_select_event(idx, hw_counter->config_base);
+       /* Enable all the PMU counters. */
+       val = readl_relaxed(cci_ctrl_base + CCI_PMCR) | CCI_PMCR_CEN;
+       writel(val, cci_ctrl_base + CCI_PMCR);
+       raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
 
-       cci_pmu_enable_counter(idx);
+}
 
-       raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
+static void cci_pmu_disable(struct pmu *pmu)
+{
+       struct cci_pmu *cci_pmu = to_cci_pmu(pmu);
+       struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
+       unsigned long flags;
+       u32 val;
+
+       raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
+
+       /* Disable all the PMU counters. */
+       val = readl_relaxed(cci_ctrl_base + CCI_PMCR) & ~CCI_PMCR_CEN;
+       writel(val, cci_ctrl_base + CCI_PMCR);
+       raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
+}
+
+/*
+ * Check if the idx represents a non-programmable counter.
+ * All the fixed event counters are mapped before the programmable
+ * counters.
+ */
+static bool pmu_fixed_hw_idx(struct cci_pmu *cci_pmu, int idx)
+{
+       return (idx >= 0) && (idx < cci_pmu->model->fixed_hw_cntrs);
 }
 
-static void cci_pmu_disable_event(struct perf_event *event)
+static void cci_pmu_start(struct perf_event *event, int pmu_flags)
 {
+       struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
+       struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
+       struct hw_perf_event *hwc = &event->hw;
+       int idx = hwc->idx;
        unsigned long flags;
-       struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
-       struct pmu_hw_events *events = cci_pmu->get_hw_events();
-       struct hw_perf_event *hw_counter = &event->hw;
-       int idx = hw_counter->idx;
 
-       if (unlikely(!cci_pmu_counter_is_valid(cci_pmu, idx))) {
+       /*
+        * To handle interrupt latency, we always reprogram the period
+        * regardlesss of PERF_EF_RELOAD.
+        */
+       if (pmu_flags & PERF_EF_RELOAD)
+               WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
+
+       hwc->state = 0;
+
+       if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
                dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
                return;
        }
 
-       raw_spin_lock_irqsave(&events->pmu_lock, flags);
+       raw_spin_lock_irqsave(&hw_events->pmu_lock, flags);
 
-       cci_pmu_disable_counter(idx);
+       /* Configure the counter unless you are counting a fixed event */
+       if (!pmu_fixed_hw_idx(cci_pmu, idx))
+               pmu_set_event(cci_pmu, idx, hwc->config_base);
 
-       raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
+       pmu_event_set_period(event);
+       pmu_enable_counter(cci_pmu, idx);
+
+       raw_spin_unlock_irqrestore(&hw_events->pmu_lock, flags);
 }
 
-static void cci_pmu_start(struct arm_pmu *cci_pmu)
+static void cci_pmu_stop(struct perf_event *event, int pmu_flags)
 {
-       u32 val;
-       unsigned long flags;
-       struct pmu_hw_events *events = cci_pmu->get_hw_events();
+       struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
+       struct hw_perf_event *hwc = &event->hw;
+       int idx = hwc->idx;
 
-       raw_spin_lock_irqsave(&events->pmu_lock, flags);
+       if (hwc->state & PERF_HES_STOPPED)
+               return;
 
-       /* Enable all the PMU counters. */
-       val = readl(cci_ctrl_base + CCI400_PMCR) | CCI400_PMCR_CEN;
-       writel(val, cci_ctrl_base + CCI400_PMCR);
+       if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
+               dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
+               return;
+       }
 
-       raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
+       /*
+        * We always reprogram the counter, so ignore PERF_EF_UPDATE. See
+        * cci_pmu_start()
+        */
+       pmu_disable_counter(cci_pmu, idx);
+       pmu_event_update(event);
+       hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
 }
 
-static void cci_pmu_stop(struct arm_pmu *cci_pmu)
+static int cci_pmu_add(struct perf_event *event, int flags)
 {
-       u32 val;
-       unsigned long flags;
-       struct pmu_hw_events *events = cci_pmu->get_hw_events();
+       struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
+       struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
+       struct hw_perf_event *hwc = &event->hw;
+       int idx;
+       int err = 0;
 
-       raw_spin_lock_irqsave(&events->pmu_lock, flags);
+       perf_pmu_disable(event->pmu);
 
-       /* Disable all the PMU counters. */
-       val = readl(cci_ctrl_base + CCI400_PMCR) & ~CCI400_PMCR_CEN;
-       writel(val, cci_ctrl_base + CCI400_PMCR);
+       /* If we don't have a space for the counter then finish early. */
+       idx = pmu_get_event_idx(hw_events, event);
+       if (idx < 0) {
+               err = idx;
+               goto out;
+       }
 
-       raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
+       event->hw.idx = idx;
+       hw_events->events[idx] = event;
+
+       hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
+       if (flags & PERF_EF_START)
+               cci_pmu_start(event, PERF_EF_RELOAD);
+
+       /* Propagate our changes to the userspace mapping. */
+       perf_event_update_userpage(event);
+
+out:
+       perf_pmu_enable(event->pmu);
+       return err;
 }
 
-static u32 cci_pmu_read_counter(struct perf_event *event)
+static void cci_pmu_del(struct perf_event *event, int flags)
 {
-       struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
-       struct hw_perf_event *hw_counter = &event->hw;
-       int idx = hw_counter->idx;
-       u32 value;
+       struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
+       struct cci_pmu_hw_events *hw_events = &cci_pmu->hw_events;
+       struct hw_perf_event *hwc = &event->hw;
+       int idx = hwc->idx;
 
-       if (unlikely(!cci_pmu_counter_is_valid(cci_pmu, idx))) {
-               dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
+       cci_pmu_stop(event, PERF_EF_UPDATE);
+       hw_events->events[idx] = NULL;
+       clear_bit(idx, hw_events->used_mask);
+
+       perf_event_update_userpage(event);
+}
+
+static int
+validate_event(struct pmu *cci_pmu,
+               struct cci_pmu_hw_events *hw_events,
+               struct perf_event *event)
+{
+       if (is_software_event(event))
+               return 1;
+
+       /*
+        * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
+        * core perf code won't check that the pmu->ctx == leader->ctx
+        * until after pmu->event_init(event).
+        */
+       if (event->pmu != cci_pmu)
                return 0;
+
+       if (event->state < PERF_EVENT_STATE_OFF)
+               return 1;
+
+       if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
+               return 1;
+
+       return pmu_get_event_idx(hw_events, event) >= 0;
+}
+
+static int
+validate_group(struct perf_event *event)
+{
+       struct perf_event *sibling, *leader = event->group_leader;
+       struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
+       unsigned long mask[BITS_TO_LONGS(cci_pmu->num_cntrs)];
+       struct cci_pmu_hw_events fake_pmu = {
+               /*
+                * Initialise the fake PMU. We only need to populate the
+                * used_mask for the purposes of validation.
+                */
+               .used_mask = mask,
+       };
+       memset(mask, 0, BITS_TO_LONGS(cci_pmu->num_cntrs) * sizeof(unsigned long));
+
+       if (!validate_event(event->pmu, &fake_pmu, leader))
+               return -EINVAL;
+
+       list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
+               if (!validate_event(event->pmu, &fake_pmu, sibling))
+                       return -EINVAL;
        }
-       value = cci_pmu_read_register(idx, CCI400_PMU_CNTR);
 
-       return value;
+       if (!validate_event(event->pmu, &fake_pmu, event))
+               return -EINVAL;
+
+       return 0;
 }
 
-static void cci_pmu_write_counter(struct perf_event *event, u32 value)
+static int
+__hw_perf_event_init(struct perf_event *event)
 {
-       struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
-       struct hw_perf_event *hw_counter = &event->hw;
-       int idx = hw_counter->idx;
+       struct hw_perf_event *hwc = &event->hw;
+       int mapping;
 
-       if (unlikely(!cci_pmu_counter_is_valid(cci_pmu, idx)))
-               dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
-       else
-               cci_pmu_write_register(value, idx, CCI400_PMU_CNTR);
-}
-
-static struct arm_pmu cci_pmu = {
-       .name             = DRIVER_NAME,
-       .max_period       = (1LLU << 32) - 1,
-       .get_hw_events    = cci_pmu_get_hw_events,
-       .get_event_idx    = cci_pmu_get_event_idx,
-       .map_event        = cci_pmu_map_event,
-       .request_irq      = cci_pmu_request_irq,
-       .handle_irq       = cci_pmu_handle_irq,
-       .free_irq         = cci_pmu_free_irq,
-       .enable           = cci_pmu_enable_event,
-       .disable          = cci_pmu_disable_event,
-       .start            = cci_pmu_start,
-       .stop             = cci_pmu_stop,
-       .read_counter     = cci_pmu_read_counter,
-       .write_counter    = cci_pmu_write_counter,
+       mapping = pmu_map_event(event);
+
+       if (mapping < 0) {
+               pr_debug("event %x:%llx not supported\n", event->attr.type,
+                        event->attr.config);
+               return mapping;
+       }
+
+       /*
+        * We don't assign an index until we actually place the event onto
+        * hardware. Use -1 to signify that we haven't decided where to put it
+        * yet.
+        */
+       hwc->idx                = -1;
+       hwc->config_base        = 0;
+       hwc->config             = 0;
+       hwc->event_base         = 0;
+
+       /*
+        * Store the event encoding into the config_base field.
+        */
+       hwc->config_base            |= (unsigned long)mapping;
+
+       /*
+        * Limit the sample_period to half of the counter width. That way, the
+        * new counter value is far less likely to overtake the previous one
+        * unless you have some serious IRQ latency issues.
+        */
+       hwc->sample_period  = CCI_PMU_CNTR_MASK >> 1;
+       hwc->last_period    = hwc->sample_period;
+       local64_set(&hwc->period_left, hwc->sample_period);
+
+       if (event->group_leader != event) {
+               if (validate_group(event) != 0)
+                       return -EINVAL;
+       }
+
+       return 0;
+}
+
+static int cci_pmu_event_init(struct perf_event *event)
+{
+       struct cci_pmu *cci_pmu = to_cci_pmu(event->pmu);
+       atomic_t *active_events = &cci_pmu->active_events;
+       int err = 0;
+       int cpu;
+
+       if (event->attr.type != event->pmu->type)
+               return -ENOENT;
+
+       /* Shared by all CPUs, no meaningful state to sample */
+       if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
+               return -EOPNOTSUPP;
+
+       /* We have no filtering of any kind */
+       if (event->attr.exclude_user    ||
+           event->attr.exclude_kernel  ||
+           event->attr.exclude_hv      ||
+           event->attr.exclude_idle    ||
+           event->attr.exclude_host    ||
+           event->attr.exclude_guest)
+               return -EINVAL;
+
+       /*
+        * Following the example set by other "uncore" PMUs, we accept any CPU
+        * and rewrite its affinity dynamically rather than having perf core
+        * handle cpu == -1 and pid == -1 for this case.
+        *
+        * The perf core will pin online CPUs for the duration of this call and
+        * the event being installed into its context, so the PMU's CPU can't
+        * change under our feet.
+        */
+       cpu = cpumask_first(&cci_pmu->cpus);
+       if (event->cpu < 0 || cpu < 0)
+               return -EINVAL;
+       event->cpu = cpu;
+
+       event->destroy = hw_perf_event_destroy;
+       if (!atomic_inc_not_zero(active_events)) {
+               mutex_lock(&cci_pmu->reserve_mutex);
+               if (atomic_read(active_events) == 0)
+                       err = cci_pmu_get_hw(cci_pmu);
+               if (!err)
+                       atomic_inc(active_events);
+               mutex_unlock(&cci_pmu->reserve_mutex);
+       }
+       if (err)
+               return err;
+
+       err = __hw_perf_event_init(event);
+       if (err)
+               hw_perf_event_destroy(event);
+
+       return err;
+}
+
+static ssize_t pmu_cpumask_attr_show(struct device *dev,
+                                    struct device_attribute *attr, char *buf)
+{
+       struct dev_ext_attribute *eattr = container_of(attr,
+                                       struct dev_ext_attribute, attr);
+       struct cci_pmu *cci_pmu = eattr->var;
+
+       int n = scnprintf(buf, PAGE_SIZE - 1, "%*pbl",
+                         cpumask_pr_args(&cci_pmu->cpus));
+       buf[n++] = '\n';
+       buf[n] = '\0';
+       return n;
+}
+
+static struct dev_ext_attribute pmu_cpumask_attr = {
+       __ATTR(cpumask, S_IRUGO, pmu_cpumask_attr_show, NULL),
+       NULL,           /* Populated in cci_pmu_init */
 };
 
-static int cci_pmu_probe(struct platform_device *pdev)
+static struct attribute *pmu_attrs[] = {
+       &pmu_cpumask_attr.attr.attr,
+       NULL,
+};
+
+static struct attribute_group pmu_attr_group = {
+       .attrs = pmu_attrs,
+};
+
+static struct attribute_group pmu_format_attr_group = {
+       .name = "format",
+       .attrs = NULL,          /* Filled in cci_pmu_init_attrs */
+};
+
+static struct attribute_group pmu_event_attr_group = {
+       .name = "events",
+       .attrs = NULL,          /* Filled in cci_pmu_init_attrs */
+};
+
+static const struct attribute_group *pmu_attr_groups[] = {
+       &pmu_attr_group,
+       &pmu_format_attr_group,
+       &pmu_event_attr_group,
+       NULL
+};
+
+static struct attribute **alloc_attrs(struct platform_device *pdev,
+                               int n, struct dev_ext_attribute *source)
 {
-       struct resource *res;
+       int i;
+       struct attribute **attrs;
+
+       /* Alloc n + 1 (for terminating NULL) */
+       attrs  = devm_kcalloc(&pdev->dev, n + 1, sizeof(struct attribute *),
+                                                               GFP_KERNEL);
+       if (!attrs)
+               return attrs;
+       for(i = 0; i < n; i++)
+               attrs[i] = &source[i].attr.attr;
+       return attrs;
+}
 
-       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-       cci_pmu_base = devm_ioremap_resource(&pdev->dev, res);
-       if (IS_ERR(cci_pmu_base))
-               return PTR_ERR(cci_pmu_base);
+static int cci_pmu_init_attrs(struct cci_pmu *cci_pmu, struct platform_device *pdev)
+{
+       const struct cci_pmu_model *model = cci_pmu->model;
+       struct attribute **attrs;
 
-       cci_pmu.plat_device = pdev;
-       cci_pmu.num_events = cci_pmu_get_max_counters();
-       raw_spin_lock_init(&cci_hw_events.pmu_lock);
-       cpumask_setall(&cci_pmu.valid_cpus);
+       /*
+        * All allocations below are managed, hence doesn't need to be
+        * free'd explicitly in case of an error.
+        */
 
-       return armpmu_register(&cci_pmu, -1);
+       if (model->nevent_attrs) {
+               attrs = alloc_attrs(pdev, model->nevent_attrs,
+                                               model->event_attrs);
+               if (!attrs)
+                       return -ENOMEM;
+               pmu_event_attr_group.attrs = attrs;
+       }
+       if (model->nformat_attrs) {
+               attrs = alloc_attrs(pdev, model->nformat_attrs,
+                                                model->format_attrs);
+               if (!attrs)
+                       return -ENOMEM;
+               pmu_format_attr_group.attrs = attrs;
+       }
+       pmu_cpumask_attr.var = cci_pmu;
+
+       return 0;
 }
 
+static int cci_pmu_init(struct cci_pmu *cci_pmu, struct platform_device *pdev)
+{
+       char *name = cci_pmu->model->name;
+       u32 num_cntrs;
+       int rc;
+
+       rc = cci_pmu_init_attrs(cci_pmu, pdev);
+       if (rc)
+               return rc;
+
+       cci_pmu->pmu = (struct pmu) {
+               .name           = cci_pmu->model->name,
+               .task_ctx_nr    = perf_invalid_context,
+               .pmu_enable     = cci_pmu_enable,
+               .pmu_disable    = cci_pmu_disable,
+               .event_init     = cci_pmu_event_init,
+               .add            = cci_pmu_add,
+               .del            = cci_pmu_del,
+               .start          = cci_pmu_start,
+               .stop           = cci_pmu_stop,
+               .read           = pmu_read,
+               .attr_groups    = pmu_attr_groups,
+       };
+
+       cci_pmu->plat_device = pdev;
+       num_cntrs = pmu_get_max_counters();
+       if (num_cntrs > cci_pmu->model->num_hw_cntrs) {
+               dev_warn(&pdev->dev,
+                       "PMU implements more counters(%d) than supported by"
+                       " the model(%d), truncated.",
+                       num_cntrs, cci_pmu->model->num_hw_cntrs);
+               num_cntrs = cci_pmu->model->num_hw_cntrs;
+       }
+       cci_pmu->num_cntrs = num_cntrs + cci_pmu->model->fixed_hw_cntrs;
+
+       return perf_pmu_register(&cci_pmu->pmu, name, -1);
+}
+
+static int cci_pmu_cpu_notifier(struct notifier_block *self,
+                               unsigned long action, void *hcpu)
+{
+       struct cci_pmu *cci_pmu = container_of(self,
+                                       struct cci_pmu, cpu_nb);
+       unsigned int cpu = (long)hcpu;
+       unsigned int target;
+
+       switch (action & ~CPU_TASKS_FROZEN) {
+       case CPU_DOWN_PREPARE:
+               if (!cpumask_test_and_clear_cpu(cpu, &cci_pmu->cpus))
+                       break;
+               target = cpumask_any_but(cpu_online_mask, cpu);
+               if (target < 0) // UP, last CPU
+                       break;
+               /*
+                * TODO: migrate context once core races on event->ctx have
+                * been fixed.
+                */
+               cpumask_set_cpu(target, &cci_pmu->cpus);
+       default:
+               break;
+       }
+
+       return NOTIFY_OK;
+}
+
+static struct cci_pmu_model cci_pmu_models[] = {
+#ifdef CONFIG_ARM_CCI400_PMU
+       [CCI400_R0] = {
+               .name = "CCI_400",
+               .fixed_hw_cntrs = 1,    /* Cycle counter */
+               .num_hw_cntrs = 4,
+               .cntr_size = SZ_4K,
+               .format_attrs = cci400_pmu_format_attrs,
+               .nformat_attrs = ARRAY_SIZE(cci400_pmu_format_attrs),
+               .event_attrs = cci400_r0_pmu_event_attrs,
+               .nevent_attrs = ARRAY_SIZE(cci400_r0_pmu_event_attrs),
+               .event_ranges = {
+                       [CCI_IF_SLAVE] = {
+                               CCI400_R0_SLAVE_PORT_MIN_EV,
+                               CCI400_R0_SLAVE_PORT_MAX_EV,
+                       },
+                       [CCI_IF_MASTER] = {
+                               CCI400_R0_MASTER_PORT_MIN_EV,
+                               CCI400_R0_MASTER_PORT_MAX_EV,
+                       },
+               },
+               .validate_hw_event = cci400_validate_hw_event,
+               .get_event_idx = cci400_get_event_idx,
+       },
+       [CCI400_R1] = {
+               .name = "CCI_400_r1",
+               .fixed_hw_cntrs = 1,    /* Cycle counter */
+               .num_hw_cntrs = 4,
+               .cntr_size = SZ_4K,
+               .format_attrs = cci400_pmu_format_attrs,
+               .nformat_attrs = ARRAY_SIZE(cci400_pmu_format_attrs),
+               .event_attrs = cci400_r1_pmu_event_attrs,
+               .nevent_attrs = ARRAY_SIZE(cci400_r1_pmu_event_attrs),
+               .event_ranges = {
+                       [CCI_IF_SLAVE] = {
+                               CCI400_R1_SLAVE_PORT_MIN_EV,
+                               CCI400_R1_SLAVE_PORT_MAX_EV,
+                       },
+                       [CCI_IF_MASTER] = {
+                               CCI400_R1_MASTER_PORT_MIN_EV,
+                               CCI400_R1_MASTER_PORT_MAX_EV,
+                       },
+               },
+               .validate_hw_event = cci400_validate_hw_event,
+               .get_event_idx = cci400_get_event_idx,
+       },
+#endif
+#ifdef CONFIG_ARM_CCI500_PMU
+       [CCI500_R0] = {
+               .name = "CCI_500",
+               .fixed_hw_cntrs = 0,
+               .num_hw_cntrs = 8,
+               .cntr_size = SZ_64K,
+               .format_attrs = cci500_pmu_format_attrs,
+               .nformat_attrs = ARRAY_SIZE(cci500_pmu_format_attrs),
+               .event_attrs = cci500_pmu_event_attrs,
+               .nevent_attrs = ARRAY_SIZE(cci500_pmu_event_attrs),
+               .event_ranges = {
+                       [CCI_IF_SLAVE] = {
+                               CCI500_SLAVE_PORT_MIN_EV,
+                               CCI500_SLAVE_PORT_MAX_EV,
+                       },
+                       [CCI_IF_MASTER] = {
+                               CCI500_MASTER_PORT_MIN_EV,
+                               CCI500_MASTER_PORT_MAX_EV,
+                       },
+                       [CCI_IF_GLOBAL] = {
+                               CCI500_GLOBAL_PORT_MIN_EV,
+                               CCI500_GLOBAL_PORT_MAX_EV,
+                       },
+               },
+               .validate_hw_event = cci500_validate_hw_event,
+       },
+#endif
+};
+
 static const struct of_device_id arm_cci_pmu_matches[] = {
-       {.compatible = "arm,cci-400-pmu"},
+#ifdef CONFIG_ARM_CCI400_PMU
+       {
+               .compatible = "arm,cci-400-pmu",
+               .data   = NULL,
+       },
+       {
+               .compatible = "arm,cci-400-pmu,r0",
+               .data   = &cci_pmu_models[CCI400_R0],
+       },
+       {
+               .compatible = "arm,cci-400-pmu,r1",
+               .data   = &cci_pmu_models[CCI400_R1],
+       },
+#endif
+#ifdef CONFIG_ARM_CCI500_PMU
+       {
+               .compatible = "arm,cci-500-pmu,r0",
+               .data = &cci_pmu_models[CCI500_R0],
+       },
+#endif
        {},
 };
 
-static struct platform_driver cci_pmu_platform_driver = {
+static inline const struct cci_pmu_model *get_cci_model(struct platform_device *pdev)
+{
+       const struct of_device_id *match = of_match_node(arm_cci_pmu_matches,
+                                                       pdev->dev.of_node);
+       if (!match)
+               return NULL;
+       if (match->data)
+               return match->data;
+
+       dev_warn(&pdev->dev, "DEPRECATED compatible property,"
+                        "requires secure access to CCI registers");
+       return probe_cci_model(pdev);
+}
+
+static bool is_duplicate_irq(int irq, int *irqs, int nr_irqs)
+{
+       int i;
+
+       for (i = 0; i < nr_irqs; i++)
+               if (irq == irqs[i])
+                       return true;
+
+       return false;
+}
+
+static struct cci_pmu *cci_pmu_alloc(struct platform_device *pdev)
+{
+       struct cci_pmu *cci_pmu;
+       const struct cci_pmu_model *model;
+
+       /*
+        * All allocations are devm_* hence we don't have to free
+        * them explicitly on an error, as it would end up in driver
+        * detach.
+        */
+       model = get_cci_model(pdev);
+       if (!model) {
+               dev_warn(&pdev->dev, "CCI PMU version not supported\n");
+               return ERR_PTR(-ENODEV);
+       }
+
+       cci_pmu = devm_kzalloc(&pdev->dev, sizeof(*cci_pmu), GFP_KERNEL);
+       if (!cci_pmu)
+               return ERR_PTR(-ENOMEM);
+
+       cci_pmu->model = model;
+       cci_pmu->irqs = devm_kcalloc(&pdev->dev, CCI_PMU_MAX_HW_CNTRS(model),
+                                       sizeof(*cci_pmu->irqs), GFP_KERNEL);
+       if (!cci_pmu->irqs)
+               return ERR_PTR(-ENOMEM);
+       cci_pmu->hw_events.events = devm_kcalloc(&pdev->dev,
+                                            CCI_PMU_MAX_HW_CNTRS(model),
+                                            sizeof(*cci_pmu->hw_events.events),
+                                            GFP_KERNEL);
+       if (!cci_pmu->hw_events.events)
+               return ERR_PTR(-ENOMEM);
+       cci_pmu->hw_events.used_mask = devm_kcalloc(&pdev->dev,
+                                               BITS_TO_LONGS(CCI_PMU_MAX_HW_CNTRS(model)),
+                                               sizeof(*cci_pmu->hw_events.used_mask),
+                                               GFP_KERNEL);
+       if (!cci_pmu->hw_events.used_mask)
+               return ERR_PTR(-ENOMEM);
+
+       return cci_pmu;
+}
+
+
+static int cci_pmu_probe(struct platform_device *pdev)
+{
+       struct resource *res;
+       struct cci_pmu *cci_pmu;
+       int i, ret, irq;
+
+       cci_pmu = cci_pmu_alloc(pdev);
+       if (IS_ERR(cci_pmu))
+               return PTR_ERR(cci_pmu);
+
+       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+       cci_pmu->base = devm_ioremap_resource(&pdev->dev, res);
+       if (IS_ERR(cci_pmu->base))
+               return -ENOMEM;
+
+       /*
+        * CCI PMU has one overflow interrupt per counter; but some may be tied
+        * together to a common interrupt.
+        */
+       cci_pmu->nr_irqs = 0;
+       for (i = 0; i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model); i++) {
+               irq = platform_get_irq(pdev, i);
+               if (irq < 0)
+                       break;
+
+               if (is_duplicate_irq(irq, cci_pmu->irqs, cci_pmu->nr_irqs))
+                       continue;
+
+               cci_pmu->irqs[cci_pmu->nr_irqs++] = irq;
+       }
+
+       /*
+        * Ensure that the device tree has as many interrupts as the number
+        * of counters.
+        */
+       if (i < CCI_PMU_MAX_HW_CNTRS(cci_pmu->model)) {
+               dev_warn(&pdev->dev, "In-correct number of interrupts: %d, should be %d\n",
+                       i, CCI_PMU_MAX_HW_CNTRS(cci_pmu->model));
+               return -EINVAL;
+       }
+
+       raw_spin_lock_init(&cci_pmu->hw_events.pmu_lock);
+       mutex_init(&cci_pmu->reserve_mutex);
+       atomic_set(&cci_pmu->active_events, 0);
+       cpumask_set_cpu(smp_processor_id(), &cci_pmu->cpus);
+
+       cci_pmu->cpu_nb = (struct notifier_block) {
+               .notifier_call  = cci_pmu_cpu_notifier,
+               /*
+                * to migrate uncore events, our notifier should be executed
+                * before perf core's notifier.
+                */
+               .priority       = CPU_PRI_PERF + 1,
+       };
+
+       ret = register_cpu_notifier(&cci_pmu->cpu_nb);
+       if (ret)
+               return ret;
+
+       ret = cci_pmu_init(cci_pmu, pdev);
+       if (ret) {
+               unregister_cpu_notifier(&cci_pmu->cpu_nb);
+               return ret;
+       }
+
+       pr_info("ARM %s PMU driver probed", cci_pmu->model->name);
+       return 0;
+}
+
+static int cci_platform_probe(struct platform_device *pdev)
+{
+       if (!cci_probed())
+               return -ENODEV;
+
+       return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
+}
+
+static struct platform_driver cci_pmu_driver = {
        .driver = {
-                  .name = DRIVER_NAME,
+                  .name = DRIVER_NAME_PMU,
                   .of_match_table = arm_cci_pmu_matches,
                  },
        .probe = cci_pmu_probe,
 };
 
-static int __init cci_pmu_init(void)
+static struct platform_driver cci_platform_driver = {
+       .driver = {
+                  .name = DRIVER_NAME,
+                  .of_match_table = arm_cci_matches,
+                 },
+       .probe = cci_platform_probe,
+};
+
+static int __init cci_platform_init(void)
 {
-       if (platform_driver_register(&cci_pmu_platform_driver))
-               WARN(1, "unable to register CCI platform driver\n");
-       return 0;
+       int ret;
+
+       ret = platform_driver_register(&cci_pmu_driver);
+       if (ret)
+               return ret;
+
+       return platform_driver_register(&cci_platform_driver);
 }
 
-#else
+#else /* !CONFIG_ARM_CCI_PMU */
 
-static int __init cci_pmu_init(void)
+static int __init cci_platform_init(void)
 {
        return 0;
 }
 
-#endif /* CONFIG_HW_PERF_EVENTS */
+#endif /* CONFIG_ARM_CCI_PMU */
+
+#ifdef CONFIG_ARM_CCI400_PORT_CTRL
+
+#define CCI_PORT_CTRL          0x0
+#define CCI_CTRL_STATUS                0xc
+
+#define CCI_ENABLE_SNOOP_REQ   0x1
+#define CCI_ENABLE_DVM_REQ     0x2
+#define CCI_ENABLE_REQ         (CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ)
+
+enum cci_ace_port_type {
+       ACE_INVALID_PORT = 0x0,
+       ACE_PORT,
+       ACE_LITE_PORT,
+};
+
+struct cci_ace_port {
+       void __iomem *base;
+       unsigned long phys;
+       enum cci_ace_port_type type;
+       struct device_node *dn;
+};
+
+static struct cci_ace_port *ports;
+static unsigned int nb_cci_ports;
 
 struct cpu_port {
        u64 mpidr;
@@ -531,19 +1699,10 @@ int cci_ace_get_port(struct device_node *dn)
 }
 EXPORT_SYMBOL_GPL(cci_ace_get_port);
 
-static void __init cci_ace_init_ports(void)
+static void cci_ace_init_ports(void)
 {
-       int port, ac, cpu;
-       u64 hwid;
-       const u32 *cell;
-       struct device_node *cpun, *cpus;
-
-       cpus = of_find_node_by_path("/cpus");
-       if (WARN(!cpus, "Missing cpus node, bailing out\n"))
-               return;
-
-       if (WARN_ON(of_property_read_u32(cpus, "#address-cells", &ac)))
-               ac = of_n_addr_cells(cpus);
+       int port, cpu;
+       struct device_node *cpun;
 
        /*
         * Port index look-up speeds up the function disabling ports by CPU,
@@ -552,18 +1711,13 @@ static void __init cci_ace_init_ports(void)
         * The stashed index array is initialized for all possible CPUs
         * at probe time.
         */
-       for_each_child_of_node(cpus, cpun) {
-               if (of_node_cmp(cpun->type, "cpu"))
-                       continue;
-               cell = of_get_property(cpun, "reg", NULL);
-               if (WARN(!cell, "%s: missing reg property\n", cpun->full_name))
-                       continue;
-
-               hwid = of_read_number(cell, ac);
-               cpu = get_logical_index(hwid & MPIDR_HWID_BITMASK);
+       for_each_possible_cpu(cpu) {
+               /* too early to use cpu->of_node */
+               cpun = of_get_cpu_node(cpu, NULL);
 
-               if (cpu < 0 || !cpu_possible(cpu))
+               if (WARN(!cpun, "Missing cpu device node\n"))
                        continue;
+
                port = __cci_ace_get_port(cpun, ACE_PORT);
                if (port < 0)
                        continue;
@@ -664,7 +1818,7 @@ EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu);
 asmlinkage void __naked cci_enable_port_for_self(void)
 {
        asm volatile ("\n"
-
+"      .arch armv7-a\n"
 "      mrc     p15, 0, r0, c0, c0, 5   @ get MPIDR value \n"
 "      and     r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n"
 "      adr     r1, 5f \n"
@@ -798,33 +1952,20 @@ int notrace __cci_control_port_by_index(u32 port, bool enable)
 }
 EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
 
-static const struct cci_nb_ports cci400_ports = {
-       .nb_ace = 2,
-       .nb_ace_lite = 3
-};
-
-static const struct of_device_id arm_cci_matches[] = {
-       {.compatible = "arm,cci-400", .data = &cci400_ports },
-       {},
-};
-
 static const struct of_device_id arm_cci_ctrl_if_matches[] = {
        {.compatible = "arm,cci-400-ctrl-if", },
        {},
 };
 
-static int __init cci_probe(void)
+static int cci_probe_ports(struct device_node *np)
 {
        struct cci_nb_ports const *cci_config;
        int ret, i, nb_ace = 0, nb_ace_lite = 0;
-       struct device_node *np, *cp;
+       struct device_node *cp;
        struct resource res;
        const char *match_str;
        bool is_ace;
 
-       np = of_find_matching_node(NULL, arm_cci_matches);
-       if (!np)
-               return -ENODEV;
 
        cci_config = of_match_node(arm_cci_matches, np)->data;
        if (!cci_config)
@@ -832,21 +1973,10 @@ static int __init cci_probe(void)
 
        nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
 
-       ports = kcalloc(sizeof(*ports), nb_cci_ports, GFP_KERNEL);
+       ports = kcalloc(nb_cci_ports, sizeof(*ports), GFP_KERNEL);
        if (!ports)
                return -ENOMEM;
 
-       ret = of_address_to_resource(np, 0, &res);
-       if (!ret) {
-               cci_ctrl_base = ioremap(res.start, resource_size(&res));
-               cci_ctrl_phys = res.start;
-       }
-       if (ret || !cci_ctrl_base) {
-               WARN(1, "unable to ioremap CCI ctrl\n");
-               ret = -ENXIO;
-               goto memalloc_err;
-       }
-
        for_each_child_of_node(np, cp) {
                if (!of_match_node(arm_cci_ctrl_if_matches, cp))
                        continue;
@@ -906,18 +2036,43 @@ static int __init cci_probe(void)
        sync_cache_w(&cpu_port);
        __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
        pr_info("ARM CCI driver probed\n");
+
        return 0;
+}
+#else /* !CONFIG_ARM_CCI400_PORT_CTRL */
+static inline int cci_probe_ports(struct device_node *np)
+{
+       return 0;
+}
+#endif /* CONFIG_ARM_CCI400_PORT_CTRL */
 
-memalloc_err:
+static int cci_probe(void)
+{
+       int ret;
+       struct device_node *np;
+       struct resource res;
+
+       np = of_find_matching_node(NULL, arm_cci_matches);
+       if(!np || !of_device_is_available(np))
+               return -ENODEV;
+
+       ret = of_address_to_resource(np, 0, &res);
+       if (!ret) {
+               cci_ctrl_base = ioremap(res.start, resource_size(&res));
+               cci_ctrl_phys = res.start;
+       }
+       if (ret || !cci_ctrl_base) {
+               WARN(1, "unable to ioremap CCI ctrl\n");
+               return -ENXIO;
+       }
 
-       kfree(ports);
-       return ret;
+       return cci_probe_ports(np);
 }
 
 static int cci_init_status = -EAGAIN;
 static DEFINE_MUTEX(cci_probing);
 
-static int __init cci_init(void)
+static int cci_init(void)
 {
        if (cci_init_status != -EAGAIN)
                return cci_init_status;
@@ -935,13 +2090,13 @@ static int __init cci_init(void)
  * has been initialized, if not it calls the init function that probes
  * the driver and updates the return value.
  */
-bool __init cci_probed(void)
+bool cci_probed(void)
 {
        return cci_init() == 0;
 }
 EXPORT_SYMBOL_GPL(cci_probed);
 
 early_initcall(cci_init);
-core_initcall(cci_pmu_init);
+core_initcall(cci_platform_init);
 MODULE_LICENSE("GPL");
 MODULE_DESCRIPTION("ARM CCI support");