arm-cci.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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) &
  330. CCI_PMU_OVRFLW_FLAG))
  331. continue;
  332. pmu_write_register(CCI_PMU_OVRFLW_FLAG, idx, CCI_PMU_OVRFLW);
  333. handled = IRQ_HANDLED;
  334. armpmu_event_update(event);
  335. perf_sample_data_init(&data, 0, hw_counter->last_period);
  336. if (!armpmu_event_set_period(event))
  337. continue;
  338. if (perf_event_overflow(event, &data, regs))
  339. cci_pmu->disable(event);
  340. }
  341. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  342. return IRQ_RETVAL(handled);
  343. }
  344. static void pmu_free_irq(struct arm_pmu *cci_pmu)
  345. {
  346. int i;
  347. for (i = 0; i < pmu->nr_irqs; i++) {
  348. if (!test_and_clear_bit(i, &pmu->active_irqs))
  349. continue;
  350. free_irq(pmu->irqs[i], cci_pmu);
  351. }
  352. }
  353. static void pmu_enable_event(struct perf_event *event)
  354. {
  355. unsigned long flags;
  356. struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
  357. struct pmu_hw_events *events = cci_pmu->get_hw_events();
  358. struct hw_perf_event *hw_counter = &event->hw;
  359. int idx = hw_counter->idx;
  360. if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
  361. dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
  362. return;
  363. }
  364. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  365. /* Configure the event to count, unless you are counting cycles */
  366. if (idx != CCI_PMU_CYCLE_CNTR_IDX)
  367. pmu_set_event(idx, hw_counter->config_base);
  368. pmu_enable_counter(idx);
  369. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  370. }
  371. static void pmu_disable_event(struct perf_event *event)
  372. {
  373. struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
  374. struct hw_perf_event *hw_counter = &event->hw;
  375. int idx = hw_counter->idx;
  376. if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
  377. dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
  378. return;
  379. }
  380. pmu_disable_counter(idx);
  381. }
  382. static void pmu_start(struct arm_pmu *cci_pmu)
  383. {
  384. u32 val;
  385. unsigned long flags;
  386. struct pmu_hw_events *events = cci_pmu->get_hw_events();
  387. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  388. /* Enable all the PMU counters. */
  389. val = readl_relaxed(cci_ctrl_base + CCI_PMCR) | CCI_PMCR_CEN;
  390. writel(val, cci_ctrl_base + CCI_PMCR);
  391. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  392. }
  393. static void pmu_stop(struct arm_pmu *cci_pmu)
  394. {
  395. u32 val;
  396. unsigned long flags;
  397. struct pmu_hw_events *events = cci_pmu->get_hw_events();
  398. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  399. /* Disable all the PMU counters. */
  400. val = readl_relaxed(cci_ctrl_base + CCI_PMCR) & ~CCI_PMCR_CEN;
  401. writel(val, cci_ctrl_base + CCI_PMCR);
  402. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  403. }
  404. static u32 pmu_read_counter(struct perf_event *event)
  405. {
  406. struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
  407. struct hw_perf_event *hw_counter = &event->hw;
  408. int idx = hw_counter->idx;
  409. u32 value;
  410. if (unlikely(!pmu_is_valid_counter(cci_pmu, idx))) {
  411. dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
  412. return 0;
  413. }
  414. value = pmu_read_register(idx, CCI_PMU_CNTR);
  415. return value;
  416. }
  417. static void pmu_write_counter(struct perf_event *event, u32 value)
  418. {
  419. struct arm_pmu *cci_pmu = to_arm_pmu(event->pmu);
  420. struct hw_perf_event *hw_counter = &event->hw;
  421. int idx = hw_counter->idx;
  422. if (unlikely(!pmu_is_valid_counter(cci_pmu, idx)))
  423. dev_err(&cci_pmu->plat_device->dev, "Invalid CCI PMU counter %d\n", idx);
  424. else
  425. pmu_write_register(value, idx, CCI_PMU_CNTR);
  426. }
  427. static int cci_pmu_init(struct arm_pmu *cci_pmu, struct platform_device *pdev)
  428. {
  429. *cci_pmu = (struct arm_pmu){
  430. .name = pmu_names[probe_cci_revision()],
  431. .max_period = (1LLU << 32) - 1,
  432. .get_hw_events = pmu_get_hw_events,
  433. .get_event_idx = pmu_get_event_idx,
  434. .map_event = pmu_map_event,
  435. .request_irq = pmu_request_irq,
  436. .handle_irq = pmu_handle_irq,
  437. .free_irq = pmu_free_irq,
  438. .enable = pmu_enable_event,
  439. .disable = pmu_disable_event,
  440. .start = pmu_start,
  441. .stop = pmu_stop,
  442. .read_counter = pmu_read_counter,
  443. .write_counter = pmu_write_counter,
  444. };
  445. cci_pmu->plat_device = pdev;
  446. cci_pmu->num_events = pmu_get_max_counters();
  447. return armpmu_register(cci_pmu, -1);
  448. }
  449. static const struct of_device_id arm_cci_pmu_matches[] = {
  450. {
  451. .compatible = "arm,cci-400-pmu",
  452. },
  453. {},
  454. };
  455. static int cci_pmu_probe(struct platform_device *pdev)
  456. {
  457. struct resource *res;
  458. int i, ret, irq;
  459. pmu = devm_kzalloc(&pdev->dev, sizeof(*pmu), GFP_KERNEL);
  460. if (!pmu)
  461. return -ENOMEM;
  462. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  463. pmu->base = devm_ioremap_resource(&pdev->dev, res);
  464. if (IS_ERR(pmu->base))
  465. return -ENOMEM;
  466. /*
  467. * CCI PMU has 5 overflow signals - one per counter; but some may be tied
  468. * together to a common interrupt.
  469. */
  470. pmu->nr_irqs = 0;
  471. for (i = 0; i < CCI_PMU_MAX_HW_EVENTS; i++) {
  472. irq = platform_get_irq(pdev, i);
  473. if (irq < 0)
  474. break;
  475. if (is_duplicate_irq(irq, pmu->irqs, pmu->nr_irqs))
  476. continue;
  477. pmu->irqs[pmu->nr_irqs++] = irq;
  478. }
  479. /*
  480. * Ensure that the device tree has as many interrupts as the number
  481. * of counters.
  482. */
  483. if (i < CCI_PMU_MAX_HW_EVENTS) {
  484. dev_warn(&pdev->dev, "In-correct number of interrupts: %d, should be %d\n",
  485. i, CCI_PMU_MAX_HW_EVENTS);
  486. return -EINVAL;
  487. }
  488. pmu->port_ranges = port_range_by_rev();
  489. if (!pmu->port_ranges) {
  490. dev_warn(&pdev->dev, "CCI PMU version not supported\n");
  491. return -EINVAL;
  492. }
  493. pmu->cci_pmu = devm_kzalloc(&pdev->dev, sizeof(*(pmu->cci_pmu)), GFP_KERNEL);
  494. if (!pmu->cci_pmu)
  495. return -ENOMEM;
  496. pmu->hw_events.events = pmu->events;
  497. pmu->hw_events.used_mask = pmu->used_mask;
  498. raw_spin_lock_init(&pmu->hw_events.pmu_lock);
  499. ret = cci_pmu_init(pmu->cci_pmu, pdev);
  500. if (ret)
  501. return ret;
  502. return 0;
  503. }
  504. static int cci_platform_probe(struct platform_device *pdev)
  505. {
  506. if (!cci_probed())
  507. return -ENODEV;
  508. return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
  509. }
  510. #endif /* CONFIG_HW_PERF_EVENTS */
  511. struct cpu_port {
  512. u64 mpidr;
  513. u32 port;
  514. };
  515. /*
  516. * Use the port MSB as valid flag, shift can be made dynamic
  517. * by computing number of bits required for port indexes.
  518. * Code disabling CCI cpu ports runs with D-cache invalidated
  519. * and SCTLR bit clear so data accesses must be kept to a minimum
  520. * to improve performance; for now shift is left static to
  521. * avoid one more data access while disabling the CCI port.
  522. */
  523. #define PORT_VALID_SHIFT 31
  524. #define PORT_VALID (0x1 << PORT_VALID_SHIFT)
  525. static inline void init_cpu_port(struct cpu_port *port, u32 index, u64 mpidr)
  526. {
  527. port->port = PORT_VALID | index;
  528. port->mpidr = mpidr;
  529. }
  530. static inline bool cpu_port_is_valid(struct cpu_port *port)
  531. {
  532. return !!(port->port & PORT_VALID);
  533. }
  534. static inline bool cpu_port_match(struct cpu_port *port, u64 mpidr)
  535. {
  536. return port->mpidr == (mpidr & MPIDR_HWID_BITMASK);
  537. }
  538. static struct cpu_port cpu_port[NR_CPUS];
  539. /**
  540. * __cci_ace_get_port - Function to retrieve the port index connected to
  541. * a cpu or device.
  542. *
  543. * @dn: device node of the device to look-up
  544. * @type: port type
  545. *
  546. * Return value:
  547. * - CCI port index if success
  548. * - -ENODEV if failure
  549. */
  550. static int __cci_ace_get_port(struct device_node *dn, int type)
  551. {
  552. int i;
  553. bool ace_match;
  554. struct device_node *cci_portn;
  555. cci_portn = of_parse_phandle(dn, "cci-control-port", 0);
  556. for (i = 0; i < nb_cci_ports; i++) {
  557. ace_match = ports[i].type == type;
  558. if (ace_match && cci_portn == ports[i].dn)
  559. return i;
  560. }
  561. return -ENODEV;
  562. }
  563. int cci_ace_get_port(struct device_node *dn)
  564. {
  565. return __cci_ace_get_port(dn, ACE_LITE_PORT);
  566. }
  567. EXPORT_SYMBOL_GPL(cci_ace_get_port);
  568. static void cci_ace_init_ports(void)
  569. {
  570. int port, cpu;
  571. struct device_node *cpun;
  572. /*
  573. * Port index look-up speeds up the function disabling ports by CPU,
  574. * since the logical to port index mapping is done once and does
  575. * not change after system boot.
  576. * The stashed index array is initialized for all possible CPUs
  577. * at probe time.
  578. */
  579. for_each_possible_cpu(cpu) {
  580. /* too early to use cpu->of_node */
  581. cpun = of_get_cpu_node(cpu, NULL);
  582. if (WARN(!cpun, "Missing cpu device node\n"))
  583. continue;
  584. port = __cci_ace_get_port(cpun, ACE_PORT);
  585. if (port < 0)
  586. continue;
  587. init_cpu_port(&cpu_port[cpu], port, cpu_logical_map(cpu));
  588. }
  589. for_each_possible_cpu(cpu) {
  590. WARN(!cpu_port_is_valid(&cpu_port[cpu]),
  591. "CPU %u does not have an associated CCI port\n",
  592. cpu);
  593. }
  594. }
  595. /*
  596. * Functions to enable/disable a CCI interconnect slave port
  597. *
  598. * They are called by low-level power management code to disable slave
  599. * interfaces snoops and DVM broadcast.
  600. * Since they may execute with cache data allocation disabled and
  601. * after the caches have been cleaned and invalidated the functions provide
  602. * no explicit locking since they may run with D-cache disabled, so normal
  603. * cacheable kernel locks based on ldrex/strex may not work.
  604. * Locking has to be provided by BSP implementations to ensure proper
  605. * operations.
  606. */
  607. /**
  608. * cci_port_control() - function to control a CCI port
  609. *
  610. * @port: index of the port to setup
  611. * @enable: if true enables the port, if false disables it
  612. */
  613. static void notrace cci_port_control(unsigned int port, bool enable)
  614. {
  615. void __iomem *base = ports[port].base;
  616. writel_relaxed(enable ? CCI_ENABLE_REQ : 0, base + CCI_PORT_CTRL);
  617. /*
  618. * This function is called from power down procedures
  619. * and must not execute any instruction that might
  620. * cause the processor to be put in a quiescent state
  621. * (eg wfi). Hence, cpu_relax() can not be added to this
  622. * read loop to optimize power, since it might hide possibly
  623. * disruptive operations.
  624. */
  625. while (readl_relaxed(cci_ctrl_base + CCI_CTRL_STATUS) & 0x1)
  626. ;
  627. }
  628. /**
  629. * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
  630. * reference
  631. *
  632. * @mpidr: mpidr of the CPU whose CCI port should be disabled
  633. *
  634. * Disabling a CCI port for a CPU implies disabling the CCI port
  635. * controlling that CPU cluster. Code disabling CPU CCI ports
  636. * must make sure that the CPU running the code is the last active CPU
  637. * in the cluster ie all other CPUs are quiescent in a low power state.
  638. *
  639. * Return:
  640. * 0 on success
  641. * -ENODEV on port look-up failure
  642. */
  643. int notrace cci_disable_port_by_cpu(u64 mpidr)
  644. {
  645. int cpu;
  646. bool is_valid;
  647. for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
  648. is_valid = cpu_port_is_valid(&cpu_port[cpu]);
  649. if (is_valid && cpu_port_match(&cpu_port[cpu], mpidr)) {
  650. cci_port_control(cpu_port[cpu].port, false);
  651. return 0;
  652. }
  653. }
  654. return -ENODEV;
  655. }
  656. EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu);
  657. /**
  658. * cci_enable_port_for_self() - enable a CCI port for calling CPU
  659. *
  660. * Enabling a CCI port for the calling CPU implies enabling the CCI
  661. * port controlling that CPU's cluster. Caller must make sure that the
  662. * CPU running the code is the first active CPU in the cluster and all
  663. * other CPUs are quiescent in a low power state or waiting for this CPU
  664. * to complete the CCI initialization.
  665. *
  666. * Because this is called when the MMU is still off and with no stack,
  667. * the code must be position independent and ideally rely on callee
  668. * clobbered registers only. To achieve this we must code this function
  669. * entirely in assembler.
  670. *
  671. * On success this returns with the proper CCI port enabled. In case of
  672. * any failure this never returns as the inability to enable the CCI is
  673. * fatal and there is no possible recovery at this stage.
  674. */
  675. asmlinkage void __naked cci_enable_port_for_self(void)
  676. {
  677. asm volatile ("\n"
  678. " .arch armv7-a\n"
  679. " mrc p15, 0, r0, c0, c0, 5 @ get MPIDR value \n"
  680. " and r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n"
  681. " adr r1, 5f \n"
  682. " ldr r2, [r1] \n"
  683. " add r1, r1, r2 @ &cpu_port \n"
  684. " add ip, r1, %[sizeof_cpu_port] \n"
  685. /* Loop over the cpu_port array looking for a matching MPIDR */
  686. "1: ldr r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
  687. " cmp r2, r0 @ compare MPIDR \n"
  688. " bne 2f \n"
  689. /* Found a match, now test port validity */
  690. " ldr r3, [r1, %[offsetof_cpu_port_port]] \n"
  691. " tst r3, #"__stringify(PORT_VALID)" \n"
  692. " bne 3f \n"
  693. /* no match, loop with the next cpu_port entry */
  694. "2: add r1, r1, %[sizeof_struct_cpu_port] \n"
  695. " cmp r1, ip @ done? \n"
  696. " blo 1b \n"
  697. /* CCI port not found -- cheaply try to stall this CPU */
  698. "cci_port_not_found: \n"
  699. " wfi \n"
  700. " wfe \n"
  701. " b cci_port_not_found \n"
  702. /* Use matched port index to look up the corresponding ports entry */
  703. "3: bic r3, r3, #"__stringify(PORT_VALID)" \n"
  704. " adr r0, 6f \n"
  705. " ldmia r0, {r1, r2} \n"
  706. " sub r1, r1, r0 @ virt - phys \n"
  707. " ldr r0, [r0, r2] @ *(&ports) \n"
  708. " mov r2, %[sizeof_struct_ace_port] \n"
  709. " mla r0, r2, r3, r0 @ &ports[index] \n"
  710. " sub r0, r0, r1 @ virt_to_phys() \n"
  711. /* Enable the CCI port */
  712. " ldr r0, [r0, %[offsetof_port_phys]] \n"
  713. " mov r3, %[cci_enable_req]\n"
  714. " str r3, [r0, #"__stringify(CCI_PORT_CTRL)"] \n"
  715. /* poll the status reg for completion */
  716. " adr r1, 7f \n"
  717. " ldr r0, [r1] \n"
  718. " ldr r0, [r0, r1] @ cci_ctrl_base \n"
  719. "4: ldr r1, [r0, #"__stringify(CCI_CTRL_STATUS)"] \n"
  720. " tst r1, %[cci_control_status_bits] \n"
  721. " bne 4b \n"
  722. " mov r0, #0 \n"
  723. " bx lr \n"
  724. " .align 2 \n"
  725. "5: .word cpu_port - . \n"
  726. "6: .word . \n"
  727. " .word ports - 6b \n"
  728. "7: .word cci_ctrl_phys - . \n"
  729. : :
  730. [sizeof_cpu_port] "i" (sizeof(cpu_port)),
  731. [cci_enable_req] "i" cpu_to_le32(CCI_ENABLE_REQ),
  732. [cci_control_status_bits] "i" cpu_to_le32(1),
  733. #ifndef __ARMEB__
  734. [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
  735. #else
  736. [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
  737. #endif
  738. [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
  739. [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
  740. [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
  741. [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
  742. unreachable();
  743. }
  744. /**
  745. * __cci_control_port_by_device() - function to control a CCI port by device
  746. * reference
  747. *
  748. * @dn: device node pointer of the device whose CCI port should be
  749. * controlled
  750. * @enable: if true enables the port, if false disables it
  751. *
  752. * Return:
  753. * 0 on success
  754. * -ENODEV on port look-up failure
  755. */
  756. int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
  757. {
  758. int port;
  759. if (!dn)
  760. return -ENODEV;
  761. port = __cci_ace_get_port(dn, ACE_LITE_PORT);
  762. if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n",
  763. dn->full_name))
  764. return -ENODEV;
  765. cci_port_control(port, enable);
  766. return 0;
  767. }
  768. EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
  769. /**
  770. * __cci_control_port_by_index() - function to control a CCI port by port index
  771. *
  772. * @port: port index previously retrieved with cci_ace_get_port()
  773. * @enable: if true enables the port, if false disables it
  774. *
  775. * Return:
  776. * 0 on success
  777. * -ENODEV on port index out of range
  778. * -EPERM if operation carried out on an ACE PORT
  779. */
  780. int notrace __cci_control_port_by_index(u32 port, bool enable)
  781. {
  782. if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
  783. return -ENODEV;
  784. /*
  785. * CCI control for ports connected to CPUS is extremely fragile
  786. * and must be made to go through a specific and controlled
  787. * interface (ie cci_disable_port_by_cpu(); control by general purpose
  788. * indexing is therefore disabled for ACE ports.
  789. */
  790. if (ports[port].type == ACE_PORT)
  791. return -EPERM;
  792. cci_port_control(port, enable);
  793. return 0;
  794. }
  795. EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
  796. static const struct cci_nb_ports cci400_ports = {
  797. .nb_ace = 2,
  798. .nb_ace_lite = 3
  799. };
  800. static const struct of_device_id arm_cci_matches[] = {
  801. {.compatible = "arm,cci-400", .data = &cci400_ports },
  802. {},
  803. };
  804. static const struct of_device_id arm_cci_ctrl_if_matches[] = {
  805. {.compatible = "arm,cci-400-ctrl-if", },
  806. {},
  807. };
  808. static int cci_probe(void)
  809. {
  810. struct cci_nb_ports const *cci_config;
  811. int ret, i, nb_ace = 0, nb_ace_lite = 0;
  812. struct device_node *np, *cp;
  813. struct resource res;
  814. const char *match_str;
  815. bool is_ace;
  816. np = of_find_matching_node(NULL, arm_cci_matches);
  817. if (!np)
  818. return -ENODEV;
  819. cci_config = of_match_node(arm_cci_matches, np)->data;
  820. if (!cci_config)
  821. return -ENODEV;
  822. nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
  823. ports = kcalloc(nb_cci_ports, sizeof(*ports), GFP_KERNEL);
  824. if (!ports)
  825. return -ENOMEM;
  826. ret = of_address_to_resource(np, 0, &res);
  827. if (!ret) {
  828. cci_ctrl_base = ioremap(res.start, resource_size(&res));
  829. cci_ctrl_phys = res.start;
  830. }
  831. if (ret || !cci_ctrl_base) {
  832. WARN(1, "unable to ioremap CCI ctrl\n");
  833. ret = -ENXIO;
  834. goto memalloc_err;
  835. }
  836. for_each_child_of_node(np, cp) {
  837. if (!of_match_node(arm_cci_ctrl_if_matches, cp))
  838. continue;
  839. i = nb_ace + nb_ace_lite;
  840. if (i >= nb_cci_ports)
  841. break;
  842. if (of_property_read_string(cp, "interface-type",
  843. &match_str)) {
  844. WARN(1, "node %s missing interface-type property\n",
  845. cp->full_name);
  846. continue;
  847. }
  848. is_ace = strcmp(match_str, "ace") == 0;
  849. if (!is_ace && strcmp(match_str, "ace-lite")) {
  850. WARN(1, "node %s containing invalid interface-type property, skipping it\n",
  851. cp->full_name);
  852. continue;
  853. }
  854. ret = of_address_to_resource(cp, 0, &res);
  855. if (!ret) {
  856. ports[i].base = ioremap(res.start, resource_size(&res));
  857. ports[i].phys = res.start;
  858. }
  859. if (ret || !ports[i].base) {
  860. WARN(1, "unable to ioremap CCI port %d\n", i);
  861. continue;
  862. }
  863. if (is_ace) {
  864. if (WARN_ON(nb_ace >= cci_config->nb_ace))
  865. continue;
  866. ports[i].type = ACE_PORT;
  867. ++nb_ace;
  868. } else {
  869. if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
  870. continue;
  871. ports[i].type = ACE_LITE_PORT;
  872. ++nb_ace_lite;
  873. }
  874. ports[i].dn = cp;
  875. }
  876. /* initialize a stashed array of ACE ports to speed-up look-up */
  877. cci_ace_init_ports();
  878. /*
  879. * Multi-cluster systems may need this data when non-coherent, during
  880. * cluster power-up/power-down. Make sure it reaches main memory.
  881. */
  882. sync_cache_w(&cci_ctrl_base);
  883. sync_cache_w(&cci_ctrl_phys);
  884. sync_cache_w(&ports);
  885. sync_cache_w(&cpu_port);
  886. __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
  887. pr_info("ARM CCI driver probed\n");
  888. return 0;
  889. memalloc_err:
  890. kfree(ports);
  891. return ret;
  892. }
  893. static int cci_init_status = -EAGAIN;
  894. static DEFINE_MUTEX(cci_probing);
  895. static int cci_init(void)
  896. {
  897. if (cci_init_status != -EAGAIN)
  898. return cci_init_status;
  899. mutex_lock(&cci_probing);
  900. if (cci_init_status == -EAGAIN)
  901. cci_init_status = cci_probe();
  902. mutex_unlock(&cci_probing);
  903. return cci_init_status;
  904. }
  905. #ifdef CONFIG_HW_PERF_EVENTS
  906. static struct platform_driver cci_pmu_driver = {
  907. .driver = {
  908. .name = DRIVER_NAME_PMU,
  909. .of_match_table = arm_cci_pmu_matches,
  910. },
  911. .probe = cci_pmu_probe,
  912. };
  913. static struct platform_driver cci_platform_driver = {
  914. .driver = {
  915. .name = DRIVER_NAME,
  916. .of_match_table = arm_cci_matches,
  917. },
  918. .probe = cci_platform_probe,
  919. };
  920. static int __init cci_platform_init(void)
  921. {
  922. int ret;
  923. ret = platform_driver_register(&cci_pmu_driver);
  924. if (ret)
  925. return ret;
  926. return platform_driver_register(&cci_platform_driver);
  927. }
  928. #else
  929. static int __init cci_platform_init(void)
  930. {
  931. return 0;
  932. }
  933. #endif
  934. /*
  935. * To sort out early init calls ordering a helper function is provided to
  936. * check if the CCI driver has beed initialized. Function check if the driver
  937. * has been initialized, if not it calls the init function that probes
  938. * the driver and updates the return value.
  939. */
  940. bool cci_probed(void)
  941. {
  942. return cci_init() == 0;
  943. }
  944. EXPORT_SYMBOL_GPL(cci_probed);
  945. early_initcall(cci_init);
  946. core_initcall(cci_platform_init);
  947. MODULE_LICENSE("GPL");
  948. MODULE_DESCRIPTION("ARM CCI support");