LCOV - code coverage report
Current view: top level - drivers/bus - arm-cci.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 8 264 3.0 %
Date: 2014-02-18 Functions: 1 27 3.7 %
Branches: 7 203 3.4 %

           Branch data     Line data    Source code
       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>
      20                 :            : #include <linux/of_address.h>
      21                 :            : #include <linux/of_irq.h>
      22                 :            : #include <linux/of_platform.h>
      23                 :            : #include <linux/platform_device.h>
      24                 :            : #include <linux/slab.h>
      25                 :            : #include <linux/spinlock.h>
      26                 :            : 
      27                 :            : #include <asm/cacheflush.h>
      28                 :            : #include <asm/irq_regs.h>
      29                 :            : #include <asm/psci.h>
      30                 :            : #include <asm/pmu.h>
      31                 :            : #include <asm/smp_plat.h>
      32                 :            : 
      33                 :            : #define DRIVER_NAME             "CCI-400"
      34                 :            : #define DRIVER_NAME_PMU         DRIVER_NAME " PMU"
      35                 :            : #define PMU_NAME                "CCI_400"
      36                 :            : 
      37                 :            : #define CCI_PORT_CTRL           0x0
      38                 :            : #define CCI_CTRL_STATUS         0xc
      39                 :            : 
      40                 :            : #define CCI_ENABLE_SNOOP_REQ    0x1
      41                 :            : #define CCI_ENABLE_DVM_REQ      0x2
      42                 :            : #define CCI_ENABLE_REQ          (CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ)
      43                 :            : 
      44                 :            : struct cci_nb_ports {
      45                 :            :         unsigned int nb_ace;
      46                 :            :         unsigned int nb_ace_lite;
      47                 :            : };
      48                 :            : 
      49                 :            : enum cci_ace_port_type {
      50                 :            :         ACE_INVALID_PORT = 0x0,
      51                 :            :         ACE_PORT,
      52                 :            :         ACE_LITE_PORT,
      53                 :            : };
      54                 :            : 
      55                 :            : struct cci_ace_port {
      56                 :            :         void __iomem *base;
      57                 :            :         unsigned long phys;
      58                 :            :         enum cci_ace_port_type type;
      59                 :            :         struct device_node *dn;
      60                 :            : };
      61                 :            : 
      62                 :            : static struct cci_ace_port *ports;
      63                 :            : static unsigned int nb_cci_ports;
      64                 :            : 
      65                 :            : static void __iomem *cci_ctrl_base;
      66                 :            : static unsigned long cci_ctrl_phys;
      67                 :            : 
      68                 :            : #ifdef CONFIG_HW_PERF_EVENTS
      69                 :            : 
      70                 :            : #define CCI_PMCR                0x0100
      71                 :            : #define CCI_PID2                0x0fe8
      72                 :            : 
      73                 :            : #define CCI_PMCR_CEN            0x00000001
      74                 :            : #define CCI_PMCR_NCNT_MASK      0x0000f800
      75                 :            : #define CCI_PMCR_NCNT_SHIFT     11
      76                 :            : 
      77                 :            : #define CCI_PID2_REV_MASK       0xf0
      78                 :            : #define CCI_PID2_REV_SHIFT      4
      79                 :            : 
      80                 :            : /* Port ids */
      81                 :            : #define CCI_PORT_S0     0
      82                 :            : #define CCI_PORT_S1     1
      83                 :            : #define CCI_PORT_S2     2
      84                 :            : #define CCI_PORT_S3     3
      85                 :            : #define CCI_PORT_S4     4
      86                 :            : #define CCI_PORT_M0     5
      87                 :            : #define CCI_PORT_M1     6
      88                 :            : #define CCI_PORT_M2     7
      89                 :            : 
      90                 :            : #define CCI_REV_R0              0
      91                 :            : #define CCI_REV_R1              1
      92                 :            : #define CCI_REV_R0_P4           4
      93                 :            : #define CCI_REV_R1_P2           6
      94                 :            : 
      95                 :            : #define CCI_PMU_EVT_SEL         0x000
      96                 :            : #define CCI_PMU_CNTR            0x004
      97                 :            : #define CCI_PMU_CNTR_CTRL       0x008
      98                 :            : #define CCI_PMU_OVRFLW          0x00c
      99                 :            : 
     100                 :            : #define CCI_PMU_OVRFLW_FLAG     1
     101                 :            : 
     102                 :            : #define CCI_PMU_CNTR_BASE(idx)  ((idx) * SZ_4K)
     103                 :            : 
     104                 :            : /*
     105                 :            :  * Instead of an event id to monitor CCI cycles, a dedicated counter is
     106                 :            :  * provided. Use 0xff to represent CCI cycles and hope that no future revisions
     107                 :            :  * make use of this event in hardware.
     108                 :            :  */
     109                 :            : enum cci400_perf_events {
     110                 :            :         CCI_PMU_CYCLES = 0xff
     111                 :            : };
     112                 :            : 
     113                 :            : #define CCI_PMU_EVENT_MASK              0xff
     114                 :            : #define CCI_PMU_EVENT_SOURCE(event)     ((event >> 5) & 0x7)
     115                 :            : #define CCI_PMU_EVENT_CODE(event)       (event & 0x1f)
     116                 :            : 
     117                 :            : #define CCI_PMU_MAX_HW_EVENTS 5   /* CCI PMU has 4 counters + 1 cycle counter */
     118                 :            : 
     119                 :            : #define CCI_PMU_CYCLE_CNTR_IDX          0
     120                 :            : #define CCI_PMU_CNTR0_IDX               1
     121                 :            : #define CCI_PMU_CNTR_LAST(cci_pmu)      (CCI_PMU_CYCLE_CNTR_IDX + cci_pmu->num_events - 1)
     122                 :            : 
     123                 :            : /*
     124                 :            :  * CCI PMU event id is an 8-bit value made of two parts - bits 7:5 for one of 8
     125                 :            :  * ports and bits 4:0 are event codes. There are different event codes
     126                 :            :  * associated with each port type.
     127                 :            :  *
     128                 :            :  * Additionally, the range of events associated with the port types changed
     129                 :            :  * between Rev0 and Rev1.
     130                 :            :  *
     131                 :            :  * The constants below define the range of valid codes for each port type for
     132                 :            :  * the different revisions and are used to validate the event to be monitored.
     133                 :            :  */
     134                 :            : 
     135                 :            : #define CCI_REV_R0_SLAVE_PORT_MIN_EV    0x00
     136                 :            : #define CCI_REV_R0_SLAVE_PORT_MAX_EV    0x13
     137                 :            : #define CCI_REV_R0_MASTER_PORT_MIN_EV   0x14
     138                 :            : #define CCI_REV_R0_MASTER_PORT_MAX_EV   0x1a
     139                 :            : 
     140                 :            : #define CCI_REV_R1_SLAVE_PORT_MIN_EV    0x00
     141                 :            : #define CCI_REV_R1_SLAVE_PORT_MAX_EV    0x14
     142                 :            : #define CCI_REV_R1_MASTER_PORT_MIN_EV   0x00
     143                 :            : #define CCI_REV_R1_MASTER_PORT_MAX_EV   0x11
     144                 :            : 
     145                 :            : struct pmu_port_event_ranges {
     146                 :            :         u8 slave_min;
     147                 :            :         u8 slave_max;
     148                 :            :         u8 master_min;
     149                 :            :         u8 master_max;
     150                 :            : };
     151                 :            : 
     152                 :            : static struct pmu_port_event_ranges port_event_range[] = {
     153                 :            :         [CCI_REV_R0] = {
     154                 :            :                 .slave_min = CCI_REV_R0_SLAVE_PORT_MIN_EV,
     155                 :            :                 .slave_max = CCI_REV_R0_SLAVE_PORT_MAX_EV,
     156                 :            :                 .master_min = CCI_REV_R0_MASTER_PORT_MIN_EV,
     157                 :            :                 .master_max = CCI_REV_R0_MASTER_PORT_MAX_EV,
     158                 :            :         },
     159                 :            :         [CCI_REV_R1] = {
     160                 :            :                 .slave_min = CCI_REV_R1_SLAVE_PORT_MIN_EV,
     161                 :            :                 .slave_max = CCI_REV_R1_SLAVE_PORT_MAX_EV,
     162                 :            :                 .master_min = CCI_REV_R1_MASTER_PORT_MIN_EV,
     163                 :            :                 .master_max = CCI_REV_R1_MASTER_PORT_MAX_EV,
     164                 :            :         },
     165                 :            : };
     166                 :            : 
     167                 :            : struct cci_pmu_drv_data {
     168                 :            :         void __iomem *base;
     169                 :            :         struct arm_pmu *cci_pmu;
     170                 :            :         int nr_irqs;
     171                 :            :         int irqs[CCI_PMU_MAX_HW_EVENTS];
     172                 :            :         unsigned long active_irqs;
     173                 :            :         struct perf_event *events[CCI_PMU_MAX_HW_EVENTS];
     174                 :            :         unsigned long used_mask[BITS_TO_LONGS(CCI_PMU_MAX_HW_EVENTS)];
     175                 :            :         struct pmu_port_event_ranges *port_ranges;
     176                 :            :         struct pmu_hw_events hw_events;
     177                 :            : };
     178                 :            : static struct cci_pmu_drv_data *pmu;
     179                 :            : 
     180                 :            : static bool is_duplicate_irq(int irq, int *irqs, int nr_irqs)
     181                 :            : {
     182                 :            :         int i;
     183                 :            : 
     184         [ #  # ]:          0 :         for (i = 0; i < nr_irqs; i++)
     185         [ #  # ]:          0 :                 if (irq == irqs[i])
     186                 :            :                         return true;
     187                 :            : 
     188                 :            :         return false;
     189                 :            : }
     190                 :            : 
     191                 :            : static int probe_cci_revision(void)
     192                 :            : {
     193                 :            :         int rev;
     194                 :          0 :         rev = readl_relaxed(cci_ctrl_base + CCI_PID2) & CCI_PID2_REV_MASK;
     195                 :          0 :         rev >>= CCI_PID2_REV_SHIFT;
     196                 :            : 
     197         [ #  # ]:          0 :         if (rev <= CCI_REV_R0_P4)
     198                 :            :                 return CCI_REV_R0;
     199         [ #  # ]:          0 :         else if (rev <= CCI_REV_R1_P2)
     200                 :            :                 return CCI_REV_R1;
     201                 :            : 
     202                 :            :         return -ENOENT;
     203                 :            : }
     204                 :            : 
     205                 :            : static struct pmu_port_event_ranges *port_range_by_rev(void)
     206                 :            : {
     207                 :            :         int rev = probe_cci_revision();
     208                 :            : 
     209         [ #  # ]:          0 :         if (rev < 0)
     210                 :            :                 return NULL;
     211                 :            : 
     212                 :          0 :         return &port_event_range[rev];
     213                 :            : }
     214                 :            : 
     215                 :            : static int pmu_is_valid_slave_event(u8 ev_code)
     216                 :            : {
     217 [ #  # ][ #  # ]:          0 :         return pmu->port_ranges->slave_min <= ev_code &&
     218                 :          0 :                 ev_code <= pmu->port_ranges->slave_max;
     219                 :            : }
     220                 :            : 
     221                 :            : static int pmu_is_valid_master_event(u8 ev_code)
     222                 :            : {
     223 [ #  # ][ #  # ]:          0 :         return pmu->port_ranges->master_min <= ev_code &&
     224                 :          0 :                 ev_code <= pmu->port_ranges->master_max;
     225                 :            : }
     226                 :            : 
     227                 :          0 : static int pmu_validate_hw_event(u8 hw_event)
     228                 :            : {
     229                 :          0 :         u8 ev_source = CCI_PMU_EVENT_SOURCE(hw_event);
     230                 :          0 :         u8 ev_code = CCI_PMU_EVENT_CODE(hw_event);
     231                 :            : 
     232      [ #  #  # ]:          0 :         switch (ev_source) {
     233                 :            :         case CCI_PORT_S0:
     234                 :            :         case CCI_PORT_S1:
     235                 :            :         case CCI_PORT_S2:
     236                 :            :         case CCI_PORT_S3:
     237                 :            :         case CCI_PORT_S4:
     238                 :            :                 /* Slave Interface */
     239         [ #  # ]:          0 :                 if (pmu_is_valid_slave_event(ev_code))
     240                 :          0 :                         return hw_event;
     241                 :            :                 break;
     242                 :            :         case CCI_PORT_M0:
     243                 :            :         case CCI_PORT_M1:
     244                 :            :         case CCI_PORT_M2:
     245                 :            :                 /* Master Interface */
     246         [ #  # ]:          0 :                 if (pmu_is_valid_master_event(ev_code))
     247                 :          0 :                         return hw_event;
     248                 :            :                 break;
     249                 :            :         }
     250                 :            : 
     251                 :            :         return -ENOENT;
     252                 :            : }
     253                 :            : 
     254                 :            : static int pmu_is_valid_counter(struct arm_pmu *cci_pmu, int idx)
     255                 :            : {
     256 [ #  # ][ #  # ]:          0 :         return CCI_PMU_CYCLE_CNTR_IDX <= idx &&
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     257                 :          0 :                 idx <= CCI_PMU_CNTR_LAST(cci_pmu);
     258                 :            : }
     259                 :            : 
     260                 :            : static u32 pmu_read_register(int idx, unsigned int offset)
     261                 :            : {
     262                 :          0 :         return readl_relaxed(pmu->base + CCI_PMU_CNTR_BASE(idx) + offset);
     263                 :            : }
     264                 :            : 
     265                 :            : static void pmu_write_register(u32 value, int idx, unsigned int offset)
     266                 :            : {
     267                 :          0 :         return writel_relaxed(value, pmu->base + CCI_PMU_CNTR_BASE(idx) + offset);
     268                 :            : }
     269                 :            : 
     270                 :            : static void pmu_disable_counter(int idx)
     271                 :            : {
     272                 :            :         pmu_write_register(0, idx, CCI_PMU_CNTR_CTRL);
     273                 :            : }
     274                 :            : 
     275                 :            : static void pmu_enable_counter(int idx)
     276                 :            : {
     277                 :            :         pmu_write_register(1, idx, CCI_PMU_CNTR_CTRL);
     278                 :            : }
     279                 :            : 
     280                 :            : static void pmu_set_event(int idx, unsigned long event)
     281                 :            : {
     282                 :          0 :         event &= CCI_PMU_EVENT_MASK;
     283                 :            :         pmu_write_register(event, idx, CCI_PMU_EVT_SEL);
     284                 :            : }
     285                 :            : 
     286                 :            : static u32 pmu_get_max_counters(void)
     287                 :            : {
     288                 :          0 :         u32 n_cnts = (readl_relaxed(cci_ctrl_base + CCI_PMCR) &
     289                 :            :                       CCI_PMCR_NCNT_MASK) >> CCI_PMCR_NCNT_SHIFT;
     290                 :            : 
     291                 :            :         /* add 1 for cycle counter */
     292                 :          0 :         return n_cnts + 1;
     293                 :            : }
     294                 :            : 
     295                 :          0 : static struct pmu_hw_events *pmu_get_hw_events(void)
     296                 :            : {
     297                 :          0 :         return &pmu->hw_events;
     298                 :            : }
     299                 :            : 
     300                 :          0 : static int pmu_get_event_idx(struct pmu_hw_events *hw, struct perf_event *event)
     301                 :            : {
     302                 :          0 :         struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
     303                 :            :         struct hw_perf_event *hw_event = &event->hw;
     304                 :          0 :         unsigned long cci_event = hw_event->config_base & CCI_PMU_EVENT_MASK;
     305                 :            :         int idx;
     306                 :            : 
     307         [ #  # ]:          0 :         if (cci_event == CCI_PMU_CYCLES) {
     308         [ #  # ]:          0 :                 if (test_and_set_bit(CCI_PMU_CYCLE_CNTR_IDX, hw->used_mask))
     309                 :            :                         return -EAGAIN;
     310                 :            : 
     311                 :          0 :                 return CCI_PMU_CYCLE_CNTR_IDX;
     312                 :            :         }
     313                 :            : 
     314         [ #  # ]:          0 :         for (idx = CCI_PMU_CNTR0_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); ++idx)
     315         [ #  # ]:          0 :                 if (!test_and_set_bit(idx, hw->used_mask))
     316                 :            :                         return idx;
     317                 :            : 
     318                 :            :         /* No counters available */
     319                 :            :         return -EAGAIN;
     320                 :            : }
     321                 :            : 
     322                 :          0 : static int pmu_map_event(struct perf_event *event)
     323                 :            : {
     324                 :            :         int mapping;
     325                 :          0 :         u8 config = event->attr.config & CCI_PMU_EVENT_MASK;
     326                 :            : 
     327         [ #  # ]:          0 :         if (event->attr.type < PERF_TYPE_MAX)
     328                 :            :                 return -ENOENT;
     329                 :            : 
     330         [ #  # ]:          0 :         if (config == CCI_PMU_CYCLES)
     331                 :          0 :                 mapping = config;
     332                 :            :         else
     333                 :          0 :                 mapping = pmu_validate_hw_event(config);
     334                 :            : 
     335                 :          0 :         return mapping;
     336                 :            : }
     337                 :            : 
     338                 :          0 : static int pmu_request_irq(struct arm_pmu *cci_pmu, irq_handler_t handler)
     339                 :            : {
     340                 :            :         int i;
     341                 :          0 :         struct platform_device *pmu_device = cci_pmu->plat_device;
     342                 :            : 
     343         [ #  # ]:          0 :         if (unlikely(!pmu_device))
     344                 :            :                 return -ENODEV;
     345                 :            : 
     346         [ #  # ]:          0 :         if (pmu->nr_irqs < 1) {
     347                 :          0 :                 dev_err(&pmu_device->dev, "no irqs for CCI PMUs defined\n");
     348                 :          0 :                 return -ENODEV;
     349                 :            :         }
     350                 :            : 
     351                 :            :         /*
     352                 :            :          * Register all available CCI PMU interrupts. In the interrupt handler
     353                 :            :          * we iterate over the counters checking for interrupt source (the
     354                 :            :          * overflowing counter) and clear it.
     355                 :            :          *
     356                 :            :          * This should allow handling of non-unique interrupt for the counters.
     357                 :            :          */
     358         [ #  # ]:          0 :         for (i = 0; i < pmu->nr_irqs; i++) {
     359                 :          0 :                 int err = request_irq(pmu->irqs[i], handler, IRQF_SHARED,
     360                 :            :                                 "arm-cci-pmu", cci_pmu);
     361         [ #  # ]:          0 :                 if (err) {
     362                 :          0 :                         dev_err(&pmu_device->dev, "unable to request IRQ%d for ARM CCI PMU counters\n",
     363                 :          0 :                                 pmu->irqs[i]);
     364                 :          0 :                         return err;
     365                 :            :                 }
     366                 :            : 
     367                 :          0 :                 set_bit(i, &pmu->active_irqs);
     368                 :            :         }
     369                 :            : 
     370                 :            :         return 0;
     371                 :            : }
     372                 :            : 
     373                 :          0 : static irqreturn_t pmu_handle_irq(int irq_num, void *dev)
     374                 :            : {
     375                 :            :         unsigned long flags;
     376                 :            :         struct arm_pmu *cci_pmu = (struct arm_pmu *)dev;
     377                 :          0 :         struct pmu_hw_events *events = cci_pmu->get_hw_events();
     378                 :            :         struct perf_sample_data data;
     379                 :            :         struct pt_regs *regs;
     380                 :            :         int idx, handled = IRQ_NONE;
     381                 :            : 
     382                 :          0 :         raw_spin_lock_irqsave(&events->pmu_lock, flags);
     383                 :            :         regs = get_irq_regs();
     384                 :            :         /*
     385                 :            :          * Iterate over counters and update the corresponding perf events.
     386                 :            :          * This should work regardless of whether we have per-counter overflow
     387                 :            :          * interrupt or a combined overflow interrupt.
     388                 :            :          */
     389         [ #  # ]:          0 :         for (idx = CCI_PMU_CYCLE_CNTR_IDX; idx <= CCI_PMU_CNTR_LAST(cci_pmu); idx++) {
     390                 :          0 :                 struct perf_event *event = events->events[idx];
     391                 :            :                 struct hw_perf_event *hw_counter;
     392                 :            : 
     393         [ #  # ]:          0 :                 if (!event)
     394                 :          0 :                         continue;
     395                 :            : 
     396                 :            :                 hw_counter = &event->hw;
     397                 :            : 
     398                 :            :                 /* Did this counter overflow? */
     399         [ #  # ]:          0 :                 if (!pmu_read_register(idx, CCI_PMU_OVRFLW) & CCI_PMU_OVRFLW_FLAG)
     400                 :          0 :                         continue;
     401                 :            : 
     402                 :            :                 pmu_write_register(CCI_PMU_OVRFLW_FLAG, idx, CCI_PMU_OVRFLW);
     403                 :            : 
     404                 :            :                 handled = IRQ_HANDLED;
     405                 :            : 
     406                 :          0 :                 armpmu_event_update(event);
     407                 :          0 :                 perf_sample_data_init(&data, 0, hw_counter->last_period);
     408         [ #  # ]:          0 :                 if (!armpmu_event_set_period(event))
     409                 :          0 :                         continue;
     410                 :            : 
     411         [ #  # ]:          0 :                 if (perf_event_overflow(event, &data, regs))
     412                 :          0 :                         cci_pmu->disable(event);
     413                 :            :         }
     414                 :          0 :         raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
     415                 :            : 
     416                 :          0 :         return IRQ_RETVAL(handled);
     417                 :            : }
     418                 :            : 
     419                 :          0 : static void pmu_free_irq(struct arm_pmu *cci_pmu)
     420                 :            : {
     421                 :            :         int i;
     422                 :            : 
     423         [ #  # ]:          0 :         for (i = 0; i < pmu->nr_irqs; i++) {
     424         [ #  # ]:          0 :                 if (!test_and_clear_bit(i, &pmu->active_irqs))
     425                 :          0 :                         continue;
     426                 :            : 
     427                 :          0 :                 free_irq(pmu->irqs[i], cci_pmu);
     428                 :            :         }
     429                 :          0 : }
     430                 :            : 
     431                 :          0 : static void pmu_enable_event(struct perf_event *event)
     432                 :            : {
     433                 :            :         unsigned long flags;
     434                 :          0 :         struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
     435                 :          0 :         struct pmu_hw_events *events = cci_pmu->get_hw_events();
     436                 :            :         struct hw_perf_event *hw_counter = &event->hw;
     437                 :          0 :         int idx = hw_counter->idx;
     438                 :            : 
     439         [ #  # ]:          0 :         if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
     440                 :          0 :                 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
     441                 :          0 :                 return;
     442                 :            :         }
     443                 :            : 
     444                 :          0 :         raw_spin_lock_irqsave(&events->pmu_lock, flags);
     445                 :            : 
     446                 :            :         /* Configure the event to count, unless you are counting cycles */
     447         [ #  # ]:          0 :         if (idx != CCI_PMU_CYCLE_CNTR_IDX)
     448                 :          0 :                 pmu_set_event(idx, hw_counter->config_base);
     449                 :            : 
     450                 :            :         pmu_enable_counter(idx);
     451                 :            : 
     452                 :          0 :         raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
     453                 :            : }
     454                 :            : 
     455                 :          0 : static void pmu_disable_event(struct perf_event *event)
     456                 :            : {
     457                 :          0 :         struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
     458                 :            :         struct hw_perf_event *hw_counter = &event->hw;
     459                 :          0 :         int idx = hw_counter->idx;
     460                 :            : 
     461         [ #  # ]:          0 :         if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
     462                 :          0 :                 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
     463                 :          0 :                 return;
     464                 :            :         }
     465                 :            : 
     466                 :            :         pmu_disable_counter(idx);
     467                 :            : }
     468                 :            : 
     469                 :          0 : static void pmu_start(struct arm_pmu *cci_pmu)
     470                 :            : {
     471                 :            :         u32 val;
     472                 :            :         unsigned long flags;
     473                 :          0 :         struct pmu_hw_events *events = cci_pmu->get_hw_events();
     474                 :            : 
     475                 :          0 :         raw_spin_lock_irqsave(&events->pmu_lock, flags);
     476                 :            : 
     477                 :            :         /* Enable all the PMU counters. */
     478                 :          0 :         val = readl_relaxed(cci_ctrl_base + CCI_PMCR) | CCI_PMCR_CEN;
     479                 :          0 :         writel(val, cci_ctrl_base + CCI_PMCR);
     480                 :            : 
     481                 :          0 :         raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
     482                 :          0 : }
     483                 :            : 
     484                 :          0 : static void pmu_stop(struct arm_pmu *cci_pmu)
     485                 :            : {
     486                 :            :         u32 val;
     487                 :            :         unsigned long flags;
     488                 :          0 :         struct pmu_hw_events *events = cci_pmu->get_hw_events();
     489                 :            : 
     490                 :          0 :         raw_spin_lock_irqsave(&events->pmu_lock, flags);
     491                 :            : 
     492                 :            :         /* Disable all the PMU counters. */
     493                 :          0 :         val = readl_relaxed(cci_ctrl_base + CCI_PMCR) & ~CCI_PMCR_CEN;
     494                 :          0 :         writel(val, cci_ctrl_base + CCI_PMCR);
     495                 :            : 
     496                 :          0 :         raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
     497                 :          0 : }
     498                 :            : 
     499                 :          0 : static u32 pmu_read_counter(struct perf_event *event)
     500                 :            : {
     501                 :          0 :         struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
     502                 :            :         struct hw_perf_event *hw_counter = &event->hw;
     503                 :          0 :         int idx = hw_counter->idx;
     504                 :            :         u32 value;
     505                 :            : 
     506         [ #  # ]:          0 :         if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
     507                 :          0 :                 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
     508                 :          0 :                 return 0;
     509                 :            :         }
     510                 :            :         value = pmu_read_register(idx, CCI_PMU_CNTR);
     511                 :            : 
     512                 :          0 :         return value;
     513                 :            : }
     514                 :            : 
     515                 :          0 : static void pmu_write_counter(struct perf_event *event, u32 value)
     516                 :            : {
     517                 :          0 :         struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
     518                 :            :         struct hw_perf_event *hw_counter = &event->hw;
     519                 :          0 :         int idx = hw_counter->idx;
     520                 :            : 
     521         [ #  # ]:          0 :         if (unlikely(!pmu_is_valid_counter(cci_pmu, idx)))
     522                 :          0 :                 dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
     523                 :            :         else
     524                 :            :                 pmu_write_register(value, idx, CCI_PMU_CNTR);
     525                 :          0 : }
     526                 :            : 
     527                 :          0 : static int cci_pmu_init(struct arm_pmu *cci_pmu, struct platform_device *pdev)
     528                 :            : {
     529                 :          0 :         *cci_pmu = (struct arm_pmu){
     530                 :            :                 .name             = PMU_NAME,
     531                 :            :                 .max_period       = (1LLU << 32) - 1,
     532                 :            :                 .get_hw_events    = pmu_get_hw_events,
     533                 :            :                 .get_event_idx    = pmu_get_event_idx,
     534                 :            :                 .map_event        = pmu_map_event,
     535                 :            :                 .request_irq      = pmu_request_irq,
     536                 :            :                 .handle_irq       = pmu_handle_irq,
     537                 :            :                 .free_irq         = pmu_free_irq,
     538                 :            :                 .enable           = pmu_enable_event,
     539                 :            :                 .disable          = pmu_disable_event,
     540                 :            :                 .start            = pmu_start,
     541                 :            :                 .stop             = pmu_stop,
     542                 :            :                 .read_counter     = pmu_read_counter,
     543                 :            :                 .write_counter    = pmu_write_counter,
     544                 :            :         };
     545                 :            : 
     546                 :          0 :         cci_pmu->plat_device = pdev;
     547                 :          0 :         cci_pmu->num_events = pmu_get_max_counters();
     548                 :            :         cpumask_setall(&cci_pmu->valid_cpus);
     549                 :            : 
     550                 :          0 :         return armpmu_register(cci_pmu, -1);
     551                 :            : }
     552                 :            : 
     553                 :            : static const struct of_device_id arm_cci_pmu_matches[] = {
     554                 :            :         {
     555                 :            :                 .compatible = "arm,cci-400-pmu",
     556                 :            :         },
     557                 :            :         {},
     558                 :            : };
     559                 :            : 
     560                 :          0 : static int cci_pmu_probe(struct platform_device *pdev)
     561                 :            : {
     562                 :            :         struct resource *res;
     563                 :            :         int i, ret, irq;
     564                 :            : 
     565                 :          0 :         pmu = devm_kzalloc(&pdev->dev, sizeof(*pmu), GFP_KERNEL);
     566         [ #  # ]:          0 :         if (!pmu)
     567                 :            :                 return -ENOMEM;
     568                 :            : 
     569                 :          0 :         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
     570                 :          0 :         pmu->base = devm_ioremap_resource(&pdev->dev, res);
     571         [ #  # ]:          0 :         if (IS_ERR(pmu->base))
     572                 :            :                 return -ENOMEM;
     573                 :            : 
     574                 :            :         /*
     575                 :            :          * CCI PMU has 5 overflow signals - one per counter; but some may be tied
     576                 :            :          * together to a common interrupt.
     577                 :            :          */
     578                 :          0 :         pmu->nr_irqs = 0;
     579         [ #  # ]:          0 :         for (i = 0; i < CCI_PMU_MAX_HW_EVENTS; i++) {
     580                 :          0 :                 irq = platform_get_irq(pdev, i);
     581         [ #  # ]:          0 :                 if (irq < 0)
     582                 :            :                         break;
     583                 :            : 
     584         [ #  # ]:          0 :                 if (is_duplicate_irq(irq, pmu->irqs, pmu->nr_irqs))
     585                 :          0 :                         continue;
     586                 :            : 
     587                 :          0 :                 pmu->irqs[pmu->nr_irqs++] = irq;
     588                 :            :         }
     589                 :            : 
     590                 :            :         /*
     591                 :            :          * Ensure that the device tree has as many interrupts as the number
     592                 :            :          * of counters.
     593                 :            :          */
     594         [ #  # ]:          0 :         if (i < CCI_PMU_MAX_HW_EVENTS) {
     595                 :          0 :                 dev_warn(&pdev->dev, "In-correct number of interrupts: %d, should be %d\n",
     596                 :            :                         i, CCI_PMU_MAX_HW_EVENTS);
     597                 :          0 :                 return -EINVAL;
     598                 :            :         }
     599                 :            : 
     600                 :          0 :         pmu->port_ranges = port_range_by_rev();
     601         [ #  # ]:          0 :         if (!pmu->port_ranges) {
     602                 :          0 :                 dev_warn(&pdev->dev, "CCI PMU version not supported\n");
     603                 :          0 :                 return -EINVAL;
     604                 :            :         }
     605                 :            : 
     606                 :          0 :         pmu->cci_pmu = devm_kzalloc(&pdev->dev, sizeof(*(pmu->cci_pmu)), GFP_KERNEL);
     607         [ #  # ]:          0 :         if (!pmu->cci_pmu)
     608                 :            :                 return -ENOMEM;
     609                 :            : 
     610                 :          0 :         pmu->hw_events.events = pmu->events;
     611                 :          0 :         pmu->hw_events.used_mask = pmu->used_mask;
     612                 :          0 :         raw_spin_lock_init(&pmu->hw_events.pmu_lock);
     613                 :            : 
     614                 :          0 :         ret = cci_pmu_init(pmu->cci_pmu, pdev);
     615         [ #  # ]:          0 :         if (ret)
     616                 :          0 :                 return ret;
     617                 :            : 
     618                 :            :         return 0;
     619                 :            : }
     620                 :            : 
     621                 :          0 : static int cci_platform_probe(struct platform_device *pdev)
     622                 :            : {
     623         [ #  # ]:          0 :         if (!cci_probed())
     624                 :            :                 return -ENODEV;
     625                 :            : 
     626                 :          0 :         return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
     627                 :            : }
     628                 :            : 
     629                 :            : #endif /* CONFIG_HW_PERF_EVENTS */
     630                 :            : 
     631                 :            : struct cpu_port {
     632                 :            :         u64 mpidr;
     633                 :            :         u32 port;
     634                 :            : };
     635                 :            : 
     636                 :            : /*
     637                 :            :  * Use the port MSB as valid flag, shift can be made dynamic
     638                 :            :  * by computing number of bits required for port indexes.
     639                 :            :  * Code disabling CCI cpu ports runs with D-cache invalidated
     640                 :            :  * and SCTLR bit clear so data accesses must be kept to a minimum
     641                 :            :  * to improve performance; for now shift is left static to
     642                 :            :  * avoid one more data access while disabling the CCI port.
     643                 :            :  */
     644                 :            : #define PORT_VALID_SHIFT        31
     645                 :            : #define PORT_VALID              (0x1 << PORT_VALID_SHIFT)
     646                 :            : 
     647                 :            : static inline void init_cpu_port(struct cpu_port *port, u32 index, u64 mpidr)
     648                 :            : {
     649                 :          0 :         port->port = PORT_VALID | index;
     650                 :          0 :         port->mpidr = mpidr;
     651                 :            : }
     652                 :            : 
     653                 :            : static inline bool cpu_port_is_valid(struct cpu_port *port)
     654                 :            : {
     655                 :    1492101 :         return !!(port->port & PORT_VALID);
     656                 :            : }
     657                 :            : 
     658                 :            : static inline bool cpu_port_match(struct cpu_port *port, u64 mpidr)
     659                 :            : {
     660                 :    1492101 :         return port->mpidr == (mpidr & MPIDR_HWID_BITMASK);
     661                 :            : }
     662                 :            : 
     663                 :            : static struct cpu_port cpu_port[NR_CPUS];
     664                 :            : 
     665                 :            : /**
     666                 :            :  * __cci_ace_get_port - Function to retrieve the port index connected to
     667                 :            :  *                      a cpu or device.
     668                 :            :  *
     669                 :            :  * @dn: device node of the device to look-up
     670                 :            :  * @type: port type
     671                 :            :  *
     672                 :            :  * Return value:
     673                 :            :  *      - CCI port index if success
     674                 :            :  *      - -ENODEV if failure
     675                 :            :  */
     676                 :          0 : static int __cci_ace_get_port(struct device_node *dn, int type)
     677                 :            : {
     678                 :            :         int i;
     679                 :            :         bool ace_match;
     680                 :            :         struct device_node *cci_portn;
     681                 :            : 
     682                 :          0 :         cci_portn = of_parse_phandle(dn, "cci-control-port", 0);
     683         [ #  # ]:          0 :         for (i = 0; i < nb_cci_ports; i++) {
     684                 :          0 :                 ace_match = ports[i].type == type;
     685 [ #  # ][ #  # ]:          0 :                 if (ace_match && cci_portn == ports[i].dn)
     686                 :            :                         return i;
     687                 :            :         }
     688                 :            :         return -ENODEV;
     689                 :            : }
     690                 :            : 
     691                 :          0 : int cci_ace_get_port(struct device_node *dn)
     692                 :            : {
     693                 :          0 :         return __cci_ace_get_port(dn, ACE_LITE_PORT);
     694                 :            : }
     695                 :            : EXPORT_SYMBOL_GPL(cci_ace_get_port);
     696                 :            : 
     697                 :          0 : static void cci_ace_init_ports(void)
     698                 :            : {
     699                 :            :         int port, cpu;
     700                 :            :         struct device_node *cpun;
     701                 :            : 
     702                 :            :         /*
     703                 :            :          * Port index look-up speeds up the function disabling ports by CPU,
     704                 :            :          * since the logical to port index mapping is done once and does
     705                 :            :          * not change after system boot.
     706                 :            :          * The stashed index array is initialized for all possible CPUs
     707                 :            :          * at probe time.
     708                 :            :          */
     709         [ #  # ]:          0 :         for_each_possible_cpu(cpu) {
     710                 :            :                 /* too early to use cpu->of_node */
     711                 :          0 :                 cpun = of_get_cpu_node(cpu, NULL);
     712                 :            : 
     713 [ #  # ][ #  # ]:          0 :                 if (WARN(!cpun, "Missing cpu device node\n"))
     714                 :          0 :                         continue;
     715                 :            : 
     716                 :          0 :                 port = __cci_ace_get_port(cpun, ACE_PORT);
     717         [ #  # ]:          0 :                 if (port < 0)
     718                 :          0 :                         continue;
     719                 :            : 
     720                 :          0 :                 init_cpu_port(&cpu_port[cpu], port, cpu_logical_map(cpu));
     721                 :            :         }
     722                 :            : 
     723         [ #  # ]:          0 :         for_each_possible_cpu(cpu) {
     724         [ #  # ]:          0 :                 WARN(!cpu_port_is_valid(&cpu_port[cpu]),
     725                 :            :                         "CPU %u does not have an associated CCI port\n",
     726                 :            :                         cpu);
     727                 :            :         }
     728                 :          0 : }
     729                 :            : /*
     730                 :            :  * Functions to enable/disable a CCI interconnect slave port
     731                 :            :  *
     732                 :            :  * They are called by low-level power management code to disable slave
     733                 :            :  * interfaces snoops and DVM broadcast.
     734                 :            :  * Since they may execute with cache data allocation disabled and
     735                 :            :  * after the caches have been cleaned and invalidated the functions provide
     736                 :            :  * no explicit locking since they may run with D-cache disabled, so normal
     737                 :            :  * cacheable kernel locks based on ldrex/strex may not work.
     738                 :            :  * Locking has to be provided by BSP implementations to ensure proper
     739                 :            :  * operations.
     740                 :            :  */
     741                 :            : 
     742                 :            : /**
     743                 :            :  * cci_port_control() - function to control a CCI port
     744                 :            :  *
     745                 :            :  * @port: index of the port to setup
     746                 :            :  * @enable: if true enables the port, if false disables it
     747                 :            :  */
     748                 :            : static void notrace cci_port_control(unsigned int port, bool enable)
     749                 :            : {
     750                 :     764148 :         void __iomem *base = ports[port].base;
     751                 :            : 
     752 [ #  # ][ #  # ]:          0 :         writel_relaxed(enable ? CCI_ENABLE_REQ : 0, base + CCI_PORT_CTRL);
     753                 :            :         /*
     754                 :            :          * This function is called from power down procedures
     755                 :            :          * and must not execute any instruction that might
     756                 :            :          * cause the processor to be put in a quiescent state
     757                 :            :          * (eg wfi). Hence, cpu_relax() can not be added to this
     758                 :            :          * read loop to optimize power, since it might hide possibly
     759                 :            :          * disruptive operations.
     760                 :            :          */
     761         [ #  # ]:     764181 :         while (readl_relaxed(cci_ctrl_base + CCI_CTRL_STATUS) & 0x1)
           [ #  #  +  + ]
     762                 :            :                         ;
     763                 :            : }
     764                 :            : 
     765                 :            : /**
     766                 :            :  * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
     767                 :            :  *                             reference
     768                 :            :  *
     769                 :            :  * @mpidr: mpidr of the CPU whose CCI port should be disabled
     770                 :            :  *
     771                 :            :  * Disabling a CCI port for a CPU implies disabling the CCI port
     772                 :            :  * controlling that CPU cluster. Code disabling CPU CCI ports
     773                 :            :  * must make sure that the CPU running the code is the last active CPU
     774                 :            :  * in the cluster ie all other CPUs are quiescent in a low power state.
     775                 :            :  *
     776                 :            :  * Return:
     777                 :            :  *      0 on success
     778                 :            :  *      -ENODEV on port look-up failure
     779                 :            :  */
     780                 :          0 : int notrace cci_disable_port_by_cpu(u64 mpidr)
     781                 :            : {
     782                 :            :         int cpu;
     783                 :            :         bool is_valid;
     784         [ +  + ]:    1492124 :         for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
     785                 :    2984202 :                 is_valid = cpu_port_is_valid(&cpu_port[cpu]);
     786 [ +  - ][ +  + ]:    1492101 :                 if (is_valid && cpu_port_match(&cpu_port[cpu], mpidr)) {
     787                 :     764148 :                         cci_port_control(cpu_port[cpu].port, false);
     788                 :            :                         return 0;
     789                 :            :                 }
     790                 :            :         }
     791                 :            :         return -ENODEV;
     792                 :            : }
     793                 :            : EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu);
     794                 :            : 
     795                 :            : /**
     796                 :            :  * cci_enable_port_for_self() - enable a CCI port for calling CPU
     797                 :            :  *
     798                 :            :  * Enabling a CCI port for the calling CPU implies enabling the CCI
     799                 :            :  * port controlling that CPU's cluster. Caller must make sure that the
     800                 :            :  * CPU running the code is the first active CPU in the cluster and all
     801                 :            :  * other CPUs are quiescent in a low power state  or waiting for this CPU
     802                 :            :  * to complete the CCI initialization.
     803                 :            :  *
     804                 :            :  * Because this is called when the MMU is still off and with no stack,
     805                 :            :  * the code must be position independent and ideally rely on callee
     806                 :            :  * clobbered registers only.  To achieve this we must code this function
     807                 :            :  * entirely in assembler.
     808                 :            :  *
     809                 :            :  * On success this returns with the proper CCI port enabled.  In case of
     810                 :            :  * any failure this never returns as the inability to enable the CCI is
     811                 :            :  * fatal and there is no possible recovery at this stage.
     812                 :            :  */
     813                 :          0 : asmlinkage void __naked cci_enable_port_for_self(void)
     814                 :            : {
     815                 :          0 :         asm volatile ("\n"
     816                 :            : "  .arch armv7-a\n"
     817                 :            : "  mrc     p15, 0, r0, c0, c0, 5   @ get MPIDR value \n"
     818                 :            : "  and     r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n"
     819                 :            : "  adr     r1, 5f \n"
     820                 :            : "  ldr     r2, [r1] \n"
     821                 :            : "  add     r1, r1, r2              @ &cpu_port \n"
     822                 :            : "  add     ip, r1, %[sizeof_cpu_port] \n"
     823                 :            : 
     824                 :            :         /* Loop over the cpu_port array looking for a matching MPIDR */
     825                 :            : "1:        ldr     r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
     826                 :            : "  cmp     r2, r0                  @ compare MPIDR \n"
     827                 :            : "  bne     2f \n"
     828                 :            : 
     829                 :            :         /* Found a match, now test port validity */
     830                 :            : "  ldr     r3, [r1, %[offsetof_cpu_port_port]] \n"
     831                 :            : "  tst     r3, #"__stringify(PORT_VALID)" \n"
     832                 :            : "  bne     3f \n"
     833                 :            : 
     834                 :            :         /* no match, loop with the next cpu_port entry */
     835                 :            : "2:        add     r1, r1, %[sizeof_struct_cpu_port] \n"
     836                 :            : "  cmp     r1, ip                  @ done? \n"
     837                 :            : "  blo     1b \n"
     838                 :            : 
     839                 :            :         /* CCI port not found -- cheaply try to stall this CPU */
     840                 :            : "cci_port_not_found: \n"
     841                 :            : "  wfi \n"
     842                 :            : "  wfe \n"
     843                 :            : "  b       cci_port_not_found \n"
     844                 :            : 
     845                 :            :         /* Use matched port index to look up the corresponding ports entry */
     846                 :            : "3:        bic     r3, r3, #"__stringify(PORT_VALID)" \n"
     847                 :            : "  adr     r0, 6f \n"
     848                 :            : "  ldmia   r0, {r1, r2} \n"
     849                 :            : "  sub     r1, r1, r0              @ virt - phys \n"
     850                 :            : "  ldr     r0, [r0, r2]            @ *(&ports) \n"
     851                 :            : "  mov     r2, %[sizeof_struct_ace_port] \n"
     852                 :            : "  mla     r0, r2, r3, r0          @ &ports[index] \n"
     853                 :            : "  sub     r0, r0, r1              @ virt_to_phys() \n"
     854                 :            : 
     855                 :            :         /* Enable the CCI port */
     856                 :            : "  ldr     r0, [r0, %[offsetof_port_phys]] \n"
     857                 :            : "  mov     r3, %[cci_enable_req]\n"              
     858                 :            : "  str     r3, [r0, #"__stringify(CCI_PORT_CTRL)"] \n"
     859                 :            : 
     860                 :            :         /* poll the status reg for completion */
     861                 :            : "  adr     r1, 7f \n"
     862                 :            : "  ldr     r0, [r1] \n"
     863                 :            : "  ldr     r0, [r0, r1]            @ cci_ctrl_base \n"
     864                 :            : "4:        ldr     r1, [r0, #"__stringify(CCI_CTRL_STATUS)"] \n"
     865                 :            : "  tst     r1, %[cci_control_status_bits] \n"                 
     866                 :            : "  bne     4b \n"
     867                 :            : 
     868                 :            : "  mov     r0, #0 \n"
     869                 :            : "  bx      lr \n"
     870                 :            : 
     871                 :            : "  .align  2 \n"
     872                 :            : "5:        .word   cpu_port - . \n"
     873                 :            : "6:        .word   . \n"
     874                 :            : "  .word   ports - 6b \n"
     875                 :            : "7:        .word   cci_ctrl_phys - . \n"
     876                 :            :         : :
     877                 :            :         [sizeof_cpu_port] "i" (sizeof(cpu_port)),
     878                 :            :         [cci_enable_req] "i" cpu_to_le32(CCI_ENABLE_REQ),
     879                 :            :         [cci_control_status_bits] "i" cpu_to_le32(1),
     880                 :            : #ifndef __ARMEB__
     881                 :            :         [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
     882                 :            : #else
     883                 :            :         [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
     884                 :            : #endif
     885                 :            :         [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
     886                 :            :         [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
     887                 :            :         [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
     888                 :            :         [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
     889                 :            : 
     890                 :          0 :         unreachable();
     891                 :            : }
     892                 :            : 
     893                 :            : /**
     894                 :            :  * __cci_control_port_by_device() - function to control a CCI port by device
     895                 :            :  *                                  reference
     896                 :            :  *
     897                 :            :  * @dn: device node pointer of the device whose CCI port should be
     898                 :            :  *      controlled
     899                 :            :  * @enable: if true enables the port, if false disables it
     900                 :            :  *
     901                 :            :  * Return:
     902                 :            :  *      0 on success
     903                 :            :  *      -ENODEV on port look-up failure
     904                 :            :  */
     905                 :          0 : int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
     906                 :            : {
     907                 :            :         int port;
     908                 :            : 
     909         [ #  # ]:          0 :         if (!dn)
     910                 :            :                 return -ENODEV;
     911                 :            : 
     912                 :          0 :         port = __cci_ace_get_port(dn, ACE_LITE_PORT);
     913 [ #  # ][ #  # ]:          0 :         if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n",
         [ #  # ][ #  # ]
     914                 :            :                                 dn->full_name))
     915                 :            :                 return -ENODEV;
     916                 :          0 :         cci_port_control(port, enable);
     917                 :            :         return 0;
     918                 :            : }
     919                 :            : EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
     920                 :            : 
     921                 :            : /**
     922                 :            :  * __cci_control_port_by_index() - function to control a CCI port by port index
     923                 :            :  *
     924                 :            :  * @port: port index previously retrieved with cci_ace_get_port()
     925                 :            :  * @enable: if true enables the port, if false disables it
     926                 :            :  *
     927                 :            :  * Return:
     928                 :            :  *      0 on success
     929                 :            :  *      -ENODEV on port index out of range
     930                 :            :  *      -EPERM if operation carried out on an ACE PORT
     931                 :            :  */
     932                 :          0 : int notrace __cci_control_port_by_index(u32 port, bool enable)
     933                 :            : {
     934 [ #  # ][ #  # ]:          0 :         if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
     935                 :            :                 return -ENODEV;
     936                 :            :         /*
     937                 :            :          * CCI control for ports connected to CPUS is extremely fragile
     938                 :            :          * and must be made to go through a specific and controlled
     939                 :            :          * interface (ie cci_disable_port_by_cpu(); control by general purpose
     940                 :            :          * indexing is therefore disabled for ACE ports.
     941                 :            :          */
     942         [ #  # ]:          0 :         if (ports[port].type == ACE_PORT)
     943                 :            :                 return -EPERM;
     944                 :            : 
     945                 :            :         cci_port_control(port, enable);
     946                 :            :         return 0;
     947                 :            : }
     948                 :            : EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
     949                 :            : 
     950                 :            : static const struct cci_nb_ports cci400_ports = {
     951                 :            :         .nb_ace = 2,
     952                 :            :         .nb_ace_lite = 3
     953                 :            : };
     954                 :            : 
     955                 :            : static const struct of_device_id arm_cci_matches[] = {
     956                 :            :         {.compatible = "arm,cci-400", .data = &cci400_ports },
     957                 :            :         {},
     958                 :            : };
     959                 :            : 
     960                 :            : static const struct of_device_id arm_cci_ctrl_if_matches[] = {
     961                 :            :         {.compatible = "arm,cci-400-ctrl-if", },
     962                 :            :         {},
     963                 :            : };
     964                 :            : 
     965                 :          0 : static int cci_probe(void)
     966                 :            : {
     967                 :            :         struct cci_nb_ports const *cci_config;
     968                 :            :         int ret, i, nb_ace = 0, nb_ace_lite = 0;
     969                 :            :         struct device_node *np, *cp;
     970                 :            :         struct resource res;
     971                 :            :         const char *match_str;
     972                 :            :         bool is_ace;
     973                 :            : 
     974         [ #  # ]:          0 :         if (psci_probe() == 0) {
     975                 :            :                 pr_debug("psci found. Aborting cci probe\n");
     976                 :            :                 return -ENODEV;
     977                 :            :         }
     978                 :            : 
     979                 :            :         np = of_find_matching_node(NULL, arm_cci_matches);
     980         [ #  # ]:          0 :         if (!np)
     981                 :            :                 return -ENODEV;
     982                 :            : 
     983                 :          0 :         cci_config = of_match_node(arm_cci_matches, np)->data;
     984         [ #  # ]:          0 :         if (!cci_config)
     985                 :            :                 return -ENODEV;
     986                 :            : 
     987                 :          0 :         nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
     988                 :            : 
     989                 :          0 :         ports = kcalloc(sizeof(*ports), nb_cci_ports, GFP_KERNEL);
     990         [ #  # ]:          0 :         if (!ports)
     991                 :            :                 return -ENOMEM;
     992                 :            : 
     993                 :          0 :         ret = of_address_to_resource(np, 0, &res);
     994         [ #  # ]:          0 :         if (!ret) {
     995                 :          0 :                 cci_ctrl_base = ioremap(res.start, resource_size(&res));
     996                 :          0 :                 cci_ctrl_phys = res.start;
     997                 :            :         }
     998 [ #  # ][ #  # ]:          0 :         if (ret || !cci_ctrl_base) {
     999                 :          0 :                 WARN(1, "unable to ioremap CCI ctrl\n");
    1000                 :            :                 ret = -ENXIO;
    1001                 :            :                 goto memalloc_err;
    1002                 :            :         }
    1003                 :            : 
    1004         [ #  # ]:          0 :         for_each_child_of_node(np, cp) {
    1005         [ #  # ]:          0 :                 if (!of_match_node(arm_cci_ctrl_if_matches, cp))
    1006                 :          0 :                         continue;
    1007                 :            : 
    1008                 :          0 :                 i = nb_ace + nb_ace_lite;
    1009                 :            : 
    1010         [ #  # ]:          0 :                 if (i >= nb_cci_ports)
    1011                 :            :                         break;
    1012                 :            : 
    1013         [ #  # ]:          0 :                 if (of_property_read_string(cp, "interface-type",
    1014                 :            :                                         &match_str)) {
    1015                 :          0 :                         WARN(1, "node %s missing interface-type property\n",
    1016                 :            :                                   cp->full_name);
    1017                 :          0 :                         continue;
    1018                 :            :                 }
    1019                 :          0 :                 is_ace = strcmp(match_str, "ace") == 0;
    1020 [ #  # ][ #  # ]:          0 :                 if (!is_ace && strcmp(match_str, "ace-lite")) {
    1021                 :          0 :                         WARN(1, "node %s containing invalid interface-type property, skipping it\n",
    1022                 :            :                                         cp->full_name);
    1023                 :          0 :                         continue;
    1024                 :            :                 }
    1025                 :            : 
    1026                 :          0 :                 ret = of_address_to_resource(cp, 0, &res);
    1027         [ #  # ]:          0 :                 if (!ret) {
    1028                 :          0 :                         ports[i].base = ioremap(res.start, resource_size(&res));
    1029                 :          0 :                         ports[i].phys = res.start;
    1030                 :            :                 }
    1031 [ #  # ][ #  # ]:          0 :                 if (ret || !ports[i].base) {
    1032                 :          0 :                         WARN(1, "unable to ioremap CCI port %d\n", i);
    1033                 :          0 :                         continue;
    1034                 :            :                 }
    1035                 :            : 
    1036         [ #  # ]:          0 :                 if (is_ace) {
    1037 [ #  # ][ #  # ]:          0 :                         if (WARN_ON(nb_ace >= cci_config->nb_ace))
    1038                 :          0 :                                 continue;
    1039                 :          0 :                         ports[i].type = ACE_PORT;
    1040                 :          0 :                         ++nb_ace;
    1041                 :            :                 } else {
    1042 [ #  # ][ #  # ]:          0 :                         if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
    1043                 :          0 :                                 continue;
    1044                 :          0 :                         ports[i].type = ACE_LITE_PORT;
    1045                 :          0 :                         ++nb_ace_lite;
    1046                 :            :                 }
    1047                 :          0 :                 ports[i].dn = cp;
    1048                 :            :         }
    1049                 :            : 
    1050                 :            :          /* initialize a stashed array of ACE ports to speed-up look-up */
    1051                 :          0 :         cci_ace_init_ports();
    1052                 :            : 
    1053                 :            :         /*
    1054                 :            :          * Multi-cluster systems may need this data when non-coherent, during
    1055                 :            :          * cluster power-up/power-down. Make sure it reaches main memory.
    1056                 :            :          */
    1057                 :            :         sync_cache_w(&cci_ctrl_base);
    1058                 :            :         sync_cache_w(&cci_ctrl_phys);
    1059                 :            :         sync_cache_w(&ports);
    1060                 :            :         sync_cache_w(&cpu_port);
    1061                 :          0 :         __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
    1062                 :          0 :         pr_info("ARM CCI driver probed\n");
    1063                 :          0 :         return 0;
    1064                 :            : 
    1065                 :            : memalloc_err:
    1066                 :            : 
    1067                 :          0 :         kfree(ports);
    1068                 :          0 :         return ret;
    1069                 :            : }
    1070                 :            : 
    1071                 :            : static int cci_init_status = -EAGAIN;
    1072                 :            : static DEFINE_MUTEX(cci_probing);
    1073                 :            : 
    1074                 :          0 : static int cci_init(void)
    1075                 :            : {
    1076         [ #  # ]:          0 :         if (cci_init_status != -EAGAIN)
    1077                 :            :                 return cci_init_status;
    1078                 :            : 
    1079                 :          0 :         mutex_lock(&cci_probing);
    1080         [ #  # ]:          0 :         if (cci_init_status == -EAGAIN)
    1081                 :          0 :                 cci_init_status = cci_probe();
    1082                 :          0 :         mutex_unlock(&cci_probing);
    1083                 :          0 :         return cci_init_status;
    1084                 :            : }
    1085                 :            : 
    1086                 :            : #ifdef CONFIG_HW_PERF_EVENTS
    1087                 :            : static struct platform_driver cci_pmu_driver = {
    1088                 :            :         .driver = {
    1089                 :            :                    .name = DRIVER_NAME_PMU,
    1090                 :            :                    .of_match_table = arm_cci_pmu_matches,
    1091                 :            :                   },
    1092                 :            :         .probe = cci_pmu_probe,
    1093                 :            : };
    1094                 :            : 
    1095                 :            : static struct platform_driver cci_platform_driver = {
    1096                 :            :         .driver = {
    1097                 :            :                    .name = DRIVER_NAME,
    1098                 :            :                    .of_match_table = arm_cci_matches,
    1099                 :            :                   },
    1100                 :            :         .probe = cci_platform_probe,
    1101                 :            : };
    1102                 :            : 
    1103                 :          0 : static int __init cci_platform_init(void)
    1104                 :            : {
    1105                 :            :         int ret;
    1106                 :            : 
    1107                 :          0 :         ret = platform_driver_register(&cci_pmu_driver);
    1108         [ #  # ]:          0 :         if (ret)
    1109                 :            :                 return ret;
    1110                 :            : 
    1111                 :          0 :         return platform_driver_register(&cci_platform_driver);
    1112                 :            : }
    1113                 :            : 
    1114                 :            : #else
    1115                 :            : 
    1116                 :            : static int __init cci_platform_init(void)
    1117                 :            : {
    1118                 :            :         return 0;
    1119                 :            : }
    1120                 :            : 
    1121                 :            : #endif
    1122                 :            : /*
    1123                 :            :  * To sort out early init calls ordering a helper function is provided to
    1124                 :            :  * check if the CCI driver has beed initialized. Function check if the driver
    1125                 :            :  * has been initialized, if not it calls the init function that probes
    1126                 :            :  * the driver and updates the return value.
    1127                 :            :  */
    1128                 :          0 : bool cci_probed(void)
    1129                 :            : {
    1130                 :          0 :         return cci_init() == 0;
    1131                 :            : }
    1132                 :            : EXPORT_SYMBOL_GPL(cci_probed);
    1133                 :            : 
    1134                 :            : early_initcall(cci_init);
    1135                 :            : core_initcall(cci_platform_init);
    1136                 :            : MODULE_LICENSE("GPL");
    1137                 :            : MODULE_DESCRIPTION("ARM CCI support");

Generated by: LCOV version 1.9