arm-cci.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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, %[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, %[cci_control_status_bits] \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. [cci_enable_req] "i" cpu_to_le32(CCI_ENABLE_REQ),
  728. [cci_control_status_bits] "i" cpu_to_le32(1),
  729. #ifndef __ARMEB__
  730. [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
  731. #else
  732. [offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
  733. #endif
  734. [offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
  735. [sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
  736. [sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
  737. [offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
  738. unreachable();
  739. }
  740. /**
  741. * __cci_control_port_by_device() - function to control a CCI port by device
  742. * reference
  743. *
  744. * @dn: device node pointer of the device whose CCI port should be
  745. * controlled
  746. * @enable: if true enables the port, if false disables it
  747. *
  748. * Return:
  749. * 0 on success
  750. * -ENODEV on port look-up failure
  751. */
  752. int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
  753. {
  754. int port;
  755. if (!dn)
  756. return -ENODEV;
  757. port = __cci_ace_get_port(dn, ACE_LITE_PORT);
  758. if (WARN_ONCE(port < 0, "node %s ACE lite port look-up failure\n",
  759. dn->full_name))
  760. return -ENODEV;
  761. cci_port_control(port, enable);
  762. return 0;
  763. }
  764. EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
  765. /**
  766. * __cci_control_port_by_index() - function to control a CCI port by port index
  767. *
  768. * @port: port index previously retrieved with cci_ace_get_port()
  769. * @enable: if true enables the port, if false disables it
  770. *
  771. * Return:
  772. * 0 on success
  773. * -ENODEV on port index out of range
  774. * -EPERM if operation carried out on an ACE PORT
  775. */
  776. int notrace __cci_control_port_by_index(u32 port, bool enable)
  777. {
  778. if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
  779. return -ENODEV;
  780. /*
  781. * CCI control for ports connected to CPUS is extremely fragile
  782. * and must be made to go through a specific and controlled
  783. * interface (ie cci_disable_port_by_cpu(); control by general purpose
  784. * indexing is therefore disabled for ACE ports.
  785. */
  786. if (ports[port].type == ACE_PORT)
  787. return -EPERM;
  788. cci_port_control(port, enable);
  789. return 0;
  790. }
  791. EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
  792. static const struct cci_nb_ports cci400_ports = {
  793. .nb_ace = 2,
  794. .nb_ace_lite = 3
  795. };
  796. static const struct of_device_id arm_cci_matches[] = {
  797. {.compatible = "arm,cci-400", .data = &cci400_ports },
  798. {},
  799. };
  800. static const struct of_device_id arm_cci_ctrl_if_matches[] = {
  801. {.compatible = "arm,cci-400-ctrl-if", },
  802. {},
  803. };
  804. static int cci_probe(void)
  805. {
  806. struct cci_nb_ports const *cci_config;
  807. int ret, i, nb_ace = 0, nb_ace_lite = 0;
  808. struct device_node *np, *cp;
  809. struct resource res;
  810. const char *match_str;
  811. bool is_ace;
  812. np = of_find_matching_node(NULL, arm_cci_matches);
  813. if (!np)
  814. return -ENODEV;
  815. cci_config = of_match_node(arm_cci_matches, np)->data;
  816. if (!cci_config)
  817. return -ENODEV;
  818. nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
  819. ports = kcalloc(sizeof(*ports), nb_cci_ports, GFP_KERNEL);
  820. if (!ports)
  821. return -ENOMEM;
  822. ret = of_address_to_resource(np, 0, &res);
  823. if (!ret) {
  824. cci_ctrl_base = ioremap(res.start, resource_size(&res));
  825. cci_ctrl_phys = res.start;
  826. }
  827. if (ret || !cci_ctrl_base) {
  828. WARN(1, "unable to ioremap CCI ctrl\n");
  829. ret = -ENXIO;
  830. goto memalloc_err;
  831. }
  832. for_each_child_of_node(np, cp) {
  833. if (!of_match_node(arm_cci_ctrl_if_matches, cp))
  834. continue;
  835. i = nb_ace + nb_ace_lite;
  836. if (i >= nb_cci_ports)
  837. break;
  838. if (of_property_read_string(cp, "interface-type",
  839. &match_str)) {
  840. WARN(1, "node %s missing interface-type property\n",
  841. cp->full_name);
  842. continue;
  843. }
  844. is_ace = strcmp(match_str, "ace") == 0;
  845. if (!is_ace && strcmp(match_str, "ace-lite")) {
  846. WARN(1, "node %s containing invalid interface-type property, skipping it\n",
  847. cp->full_name);
  848. continue;
  849. }
  850. ret = of_address_to_resource(cp, 0, &res);
  851. if (!ret) {
  852. ports[i].base = ioremap(res.start, resource_size(&res));
  853. ports[i].phys = res.start;
  854. }
  855. if (ret || !ports[i].base) {
  856. WARN(1, "unable to ioremap CCI port %d\n", i);
  857. continue;
  858. }
  859. if (is_ace) {
  860. if (WARN_ON(nb_ace >= cci_config->nb_ace))
  861. continue;
  862. ports[i].type = ACE_PORT;
  863. ++nb_ace;
  864. } else {
  865. if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
  866. continue;
  867. ports[i].type = ACE_LITE_PORT;
  868. ++nb_ace_lite;
  869. }
  870. ports[i].dn = cp;
  871. }
  872. /* initialize a stashed array of ACE ports to speed-up look-up */
  873. cci_ace_init_ports();
  874. /*
  875. * Multi-cluster systems may need this data when non-coherent, during
  876. * cluster power-up/power-down. Make sure it reaches main memory.
  877. */
  878. sync_cache_w(&cci_ctrl_base);
  879. sync_cache_w(&cci_ctrl_phys);
  880. sync_cache_w(&ports);
  881. sync_cache_w(&cpu_port);
  882. __sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
  883. pr_info("ARM CCI driver probed\n");
  884. return 0;
  885. memalloc_err:
  886. kfree(ports);
  887. return ret;
  888. }
  889. static int cci_init_status = -EAGAIN;
  890. static DEFINE_MUTEX(cci_probing);
  891. static int cci_init(void)
  892. {
  893. if (cci_init_status != -EAGAIN)
  894. return cci_init_status;
  895. mutex_lock(&cci_probing);
  896. if (cci_init_status == -EAGAIN)
  897. cci_init_status = cci_probe();
  898. mutex_unlock(&cci_probing);
  899. return cci_init_status;
  900. }
  901. #ifdef CONFIG_HW_PERF_EVENTS
  902. static struct platform_driver cci_pmu_driver = {
  903. .driver = {
  904. .name = DRIVER_NAME_PMU,
  905. .of_match_table = arm_cci_pmu_matches,
  906. },
  907. .probe = cci_pmu_probe,
  908. };
  909. static struct platform_driver cci_platform_driver = {
  910. .driver = {
  911. .name = DRIVER_NAME,
  912. .of_match_table = arm_cci_matches,
  913. },
  914. .probe = cci_platform_probe,
  915. };
  916. static int __init cci_platform_init(void)
  917. {
  918. int ret;
  919. ret = platform_driver_register(&cci_pmu_driver);
  920. if (ret)
  921. return ret;
  922. return platform_driver_register(&cci_platform_driver);
  923. }
  924. #else
  925. static int __init cci_platform_init(void)
  926. {
  927. return 0;
  928. }
  929. #endif
  930. /*
  931. * To sort out early init calls ordering a helper function is provided to
  932. * check if the CCI driver has beed initialized. Function check if the driver
  933. * has been initialized, if not it calls the init function that probes
  934. * the driver and updates the return value.
  935. */
  936. bool cci_probed(void)
  937. {
  938. return cci_init() == 0;
  939. }
  940. EXPORT_SYMBOL_GPL(cci_probed);
  941. early_initcall(cci_init);
  942. core_initcall(cci_platform_init);
  943. MODULE_LICENSE("GPL");
  944. MODULE_DESCRIPTION("ARM CCI support");