arm-cci.c 36 KB

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