arm-cci.c 28 KB

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