arm-cci.c 38 KB

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