arm-cci.c 29 KB

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