arm_smmuv3_pmu.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This driver adds support for perf events to use the Performance
  4. * Monitor Counter Groups (PMCG) associated with an SMMUv3 node
  5. * to monitor that node.
  6. *
  7. * SMMUv3 PMCG devices are named as smmuv3_pmcg_<phys_addr_page> where
  8. * <phys_addr_page> is the physical page address of the SMMU PMCG wrapped
  9. * to 4K boundary. For example, the PMCG at 0xff88840000 is named
  10. * smmuv3_pmcg_ff88840
  11. *
  12. * Filtering by stream id is done by specifying filtering parameters
  13. * with the event. options are:
  14. * filter_enable - 0 = no filtering, 1 = filtering enabled
  15. * filter_span - 0 = exact match, 1 = pattern match
  16. * filter_stream_id - pattern to filter against
  17. *
  18. * To match a partial StreamID where the X most-significant bits must match
  19. * but the Y least-significant bits might differ, STREAMID is programmed
  20. * with a value that contains:
  21. * STREAMID[Y - 1] == 0.
  22. * STREAMID[Y - 2:0] == 1 (where Y > 1).
  23. * The remainder of implemented bits of STREAMID (X bits, from bit Y upwards)
  24. * contain a value to match from the corresponding bits of event StreamID.
  25. *
  26. * Example: perf stat -e smmuv3_pmcg_ff88840/transaction,filter_enable=1,
  27. * filter_span=1,filter_stream_id=0x42/ -a netperf
  28. * Applies filter pattern 0x42 to transaction events, which means events
  29. * matching stream ids 0x42 and 0x43 are counted. Further filtering
  30. * information is available in the SMMU documentation.
  31. *
  32. * SMMU events are not attributable to a CPU, so task mode and sampling
  33. * are not supported.
  34. */
  35. #include <linux/acpi.h>
  36. #include <linux/acpi_iort.h>
  37. #include <linux/bitfield.h>
  38. #include <linux/bitops.h>
  39. #include <linux/cpuhotplug.h>
  40. #include <linux/cpumask.h>
  41. #include <linux/device.h>
  42. #include <linux/errno.h>
  43. #include <linux/interrupt.h>
  44. #include <linux/irq.h>
  45. #include <linux/kernel.h>
  46. #include <linux/list.h>
  47. #include <linux/msi.h>
  48. #include <linux/of.h>
  49. #include <linux/perf_event.h>
  50. #include <linux/platform_device.h>
  51. #include <linux/smp.h>
  52. #include <linux/sysfs.h>
  53. #include <linux/types.h>
  54. #define SMMU_PMCG_EVCNTR0 0x0
  55. #define SMMU_PMCG_EVCNTR(n, stride) (SMMU_PMCG_EVCNTR0 + (n) * (stride))
  56. #define SMMU_PMCG_EVTYPER0 0x400
  57. #define SMMU_PMCG_EVTYPER(n) (SMMU_PMCG_EVTYPER0 + (n) * 4)
  58. #define SMMU_PMCG_SID_SPAN_SHIFT 29
  59. #define SMMU_PMCG_SMR0 0xA00
  60. #define SMMU_PMCG_SMR(n) (SMMU_PMCG_SMR0 + (n) * 4)
  61. #define SMMU_PMCG_CNTENSET0 0xC00
  62. #define SMMU_PMCG_CNTENCLR0 0xC20
  63. #define SMMU_PMCG_INTENSET0 0xC40
  64. #define SMMU_PMCG_INTENCLR0 0xC60
  65. #define SMMU_PMCG_OVSCLR0 0xC80
  66. #define SMMU_PMCG_OVSSET0 0xCC0
  67. #define SMMU_PMCG_CFGR 0xE00
  68. #define SMMU_PMCG_CFGR_SID_FILTER_TYPE BIT(23)
  69. #define SMMU_PMCG_CFGR_MSI BIT(21)
  70. #define SMMU_PMCG_CFGR_RELOC_CTRS BIT(20)
  71. #define SMMU_PMCG_CFGR_SIZE GENMASK(13, 8)
  72. #define SMMU_PMCG_CFGR_NCTR GENMASK(5, 0)
  73. #define SMMU_PMCG_CR 0xE04
  74. #define SMMU_PMCG_CR_ENABLE BIT(0)
  75. #define SMMU_PMCG_IIDR 0xE08
  76. #define SMMU_PMCG_IIDR_PRODUCTID GENMASK(31, 20)
  77. #define SMMU_PMCG_IIDR_VARIANT GENMASK(19, 16)
  78. #define SMMU_PMCG_IIDR_REVISION GENMASK(15, 12)
  79. #define SMMU_PMCG_IIDR_IMPLEMENTER GENMASK(11, 0)
  80. #define SMMU_PMCG_CEID0 0xE20
  81. #define SMMU_PMCG_CEID1 0xE28
  82. #define SMMU_PMCG_IRQ_CTRL 0xE50
  83. #define SMMU_PMCG_IRQ_CTRL_IRQEN BIT(0)
  84. #define SMMU_PMCG_IRQ_CFG0 0xE58
  85. #define SMMU_PMCG_IRQ_CFG1 0xE60
  86. #define SMMU_PMCG_IRQ_CFG2 0xE64
  87. /* IMP-DEF ID registers */
  88. #define SMMU_PMCG_PIDR0 0xFE0
  89. #define SMMU_PMCG_PIDR0_PART_0 GENMASK(7, 0)
  90. #define SMMU_PMCG_PIDR1 0xFE4
  91. #define SMMU_PMCG_PIDR1_DES_0 GENMASK(7, 4)
  92. #define SMMU_PMCG_PIDR1_PART_1 GENMASK(3, 0)
  93. #define SMMU_PMCG_PIDR2 0xFE8
  94. #define SMMU_PMCG_PIDR2_REVISION GENMASK(7, 4)
  95. #define SMMU_PMCG_PIDR2_DES_1 GENMASK(2, 0)
  96. #define SMMU_PMCG_PIDR3 0xFEC
  97. #define SMMU_PMCG_PIDR3_REVAND GENMASK(7, 4)
  98. #define SMMU_PMCG_PIDR4 0xFD0
  99. #define SMMU_PMCG_PIDR4_DES_2 GENMASK(3, 0)
  100. /* MSI config fields */
  101. #define MSI_CFG0_ADDR_MASK GENMASK_ULL(51, 2)
  102. #define MSI_CFG2_MEMATTR_DEVICE_nGnRE 0x1
  103. #define SMMU_PMCG_DEFAULT_FILTER_SPAN 1
  104. #define SMMU_PMCG_DEFAULT_FILTER_SID GENMASK(31, 0)
  105. #define SMMU_PMCG_MAX_COUNTERS 64
  106. #define SMMU_PMCG_ARCH_MAX_EVENTS 128
  107. #define SMMU_PMCG_PA_SHIFT 12
  108. #define SMMU_PMCG_EVCNTR_RDONLY BIT(0)
  109. #define SMMU_PMCG_HARDEN_DISABLE BIT(1)
  110. static int cpuhp_state_num;
  111. struct smmu_pmu {
  112. struct hlist_node node;
  113. struct perf_event *events[SMMU_PMCG_MAX_COUNTERS];
  114. DECLARE_BITMAP(used_counters, SMMU_PMCG_MAX_COUNTERS);
  115. DECLARE_BITMAP(supported_events, SMMU_PMCG_ARCH_MAX_EVENTS);
  116. unsigned int irq;
  117. unsigned int on_cpu;
  118. struct pmu pmu;
  119. unsigned int num_counters;
  120. struct device *dev;
  121. void __iomem *reg_base;
  122. void __iomem *reloc_base;
  123. u64 counter_mask;
  124. u32 options;
  125. u32 iidr;
  126. bool global_filter;
  127. };
  128. #define to_smmu_pmu(p) (container_of(p, struct smmu_pmu, pmu))
  129. #define SMMU_PMU_EVENT_ATTR_EXTRACTOR(_name, _config, _start, _end) \
  130. static inline u32 get_##_name(struct perf_event *event) \
  131. { \
  132. return FIELD_GET(GENMASK_ULL(_end, _start), \
  133. event->attr._config); \
  134. } \
  135. SMMU_PMU_EVENT_ATTR_EXTRACTOR(event, config, 0, 15);
  136. SMMU_PMU_EVENT_ATTR_EXTRACTOR(filter_stream_id, config1, 0, 31);
  137. SMMU_PMU_EVENT_ATTR_EXTRACTOR(filter_span, config1, 32, 32);
  138. SMMU_PMU_EVENT_ATTR_EXTRACTOR(filter_enable, config1, 33, 33);
  139. static inline void smmu_pmu_enable(struct pmu *pmu)
  140. {
  141. struct smmu_pmu *smmu_pmu = to_smmu_pmu(pmu);
  142. writel(SMMU_PMCG_IRQ_CTRL_IRQEN,
  143. smmu_pmu->reg_base + SMMU_PMCG_IRQ_CTRL);
  144. writel(SMMU_PMCG_CR_ENABLE, smmu_pmu->reg_base + SMMU_PMCG_CR);
  145. }
  146. static int smmu_pmu_apply_event_filter(struct smmu_pmu *smmu_pmu,
  147. struct perf_event *event, int idx);
  148. static inline void smmu_pmu_enable_quirk_hip08_09(struct pmu *pmu)
  149. {
  150. struct smmu_pmu *smmu_pmu = to_smmu_pmu(pmu);
  151. unsigned int idx;
  152. for_each_set_bit(idx, smmu_pmu->used_counters, smmu_pmu->num_counters)
  153. smmu_pmu_apply_event_filter(smmu_pmu, smmu_pmu->events[idx], idx);
  154. smmu_pmu_enable(pmu);
  155. }
  156. static inline void smmu_pmu_disable(struct pmu *pmu)
  157. {
  158. struct smmu_pmu *smmu_pmu = to_smmu_pmu(pmu);
  159. writel(0, smmu_pmu->reg_base + SMMU_PMCG_CR);
  160. writel(0, smmu_pmu->reg_base + SMMU_PMCG_IRQ_CTRL);
  161. }
  162. static inline void smmu_pmu_disable_quirk_hip08_09(struct pmu *pmu)
  163. {
  164. struct smmu_pmu *smmu_pmu = to_smmu_pmu(pmu);
  165. unsigned int idx;
  166. /*
  167. * The global disable of PMU sometimes fail to stop the counting.
  168. * Harden this by writing an invalid event type to each used counter
  169. * to forcibly stop counting.
  170. */
  171. for_each_set_bit(idx, smmu_pmu->used_counters, smmu_pmu->num_counters)
  172. writel(0xffff, smmu_pmu->reg_base + SMMU_PMCG_EVTYPER(idx));
  173. smmu_pmu_disable(pmu);
  174. }
  175. static inline void smmu_pmu_counter_set_value(struct smmu_pmu *smmu_pmu,
  176. u32 idx, u64 value)
  177. {
  178. if (smmu_pmu->counter_mask & BIT(32))
  179. writeq(value, smmu_pmu->reloc_base + SMMU_PMCG_EVCNTR(idx, 8));
  180. else
  181. writel(value, smmu_pmu->reloc_base + SMMU_PMCG_EVCNTR(idx, 4));
  182. }
  183. static inline u64 smmu_pmu_counter_get_value(struct smmu_pmu *smmu_pmu, u32 idx)
  184. {
  185. u64 value;
  186. if (smmu_pmu->counter_mask & BIT(32))
  187. value = readq(smmu_pmu->reloc_base + SMMU_PMCG_EVCNTR(idx, 8));
  188. else
  189. value = readl(smmu_pmu->reloc_base + SMMU_PMCG_EVCNTR(idx, 4));
  190. return value;
  191. }
  192. static inline void smmu_pmu_counter_enable(struct smmu_pmu *smmu_pmu, u32 idx)
  193. {
  194. writeq(BIT(idx), smmu_pmu->reg_base + SMMU_PMCG_CNTENSET0);
  195. }
  196. static inline void smmu_pmu_counter_disable(struct smmu_pmu *smmu_pmu, u32 idx)
  197. {
  198. writeq(BIT(idx), smmu_pmu->reg_base + SMMU_PMCG_CNTENCLR0);
  199. }
  200. static inline void smmu_pmu_interrupt_enable(struct smmu_pmu *smmu_pmu, u32 idx)
  201. {
  202. writeq(BIT(idx), smmu_pmu->reg_base + SMMU_PMCG_INTENSET0);
  203. }
  204. static inline void smmu_pmu_interrupt_disable(struct smmu_pmu *smmu_pmu,
  205. u32 idx)
  206. {
  207. writeq(BIT(idx), smmu_pmu->reg_base + SMMU_PMCG_INTENCLR0);
  208. }
  209. static inline void smmu_pmu_set_evtyper(struct smmu_pmu *smmu_pmu, u32 idx,
  210. u32 val)
  211. {
  212. writel(val, smmu_pmu->reg_base + SMMU_PMCG_EVTYPER(idx));
  213. }
  214. static inline void smmu_pmu_set_smr(struct smmu_pmu *smmu_pmu, u32 idx, u32 val)
  215. {
  216. writel(val, smmu_pmu->reg_base + SMMU_PMCG_SMR(idx));
  217. }
  218. static void smmu_pmu_event_update(struct perf_event *event)
  219. {
  220. struct hw_perf_event *hwc = &event->hw;
  221. struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
  222. u64 delta, prev, now;
  223. u32 idx = hwc->idx;
  224. do {
  225. prev = local64_read(&hwc->prev_count);
  226. now = smmu_pmu_counter_get_value(smmu_pmu, idx);
  227. } while (local64_cmpxchg(&hwc->prev_count, prev, now) != prev);
  228. /* handle overflow. */
  229. delta = now - prev;
  230. delta &= smmu_pmu->counter_mask;
  231. local64_add(delta, &event->count);
  232. }
  233. static void smmu_pmu_set_period(struct smmu_pmu *smmu_pmu,
  234. struct hw_perf_event *hwc)
  235. {
  236. u32 idx = hwc->idx;
  237. u64 new;
  238. if (smmu_pmu->options & SMMU_PMCG_EVCNTR_RDONLY) {
  239. /*
  240. * On platforms that require this quirk, if the counter starts
  241. * at < half_counter value and wraps, the current logic of
  242. * handling the overflow may not work. It is expected that,
  243. * those platforms will have full 64 counter bits implemented
  244. * so that such a possibility is remote(eg: HiSilicon HIP08).
  245. */
  246. new = smmu_pmu_counter_get_value(smmu_pmu, idx);
  247. } else {
  248. /*
  249. * We limit the max period to half the max counter value
  250. * of the counter size, so that even in the case of extreme
  251. * interrupt latency the counter will (hopefully) not wrap
  252. * past its initial value.
  253. */
  254. new = smmu_pmu->counter_mask >> 1;
  255. smmu_pmu_counter_set_value(smmu_pmu, idx, new);
  256. }
  257. local64_set(&hwc->prev_count, new);
  258. }
  259. static void smmu_pmu_set_event_filter(struct perf_event *event,
  260. int idx, u32 span, u32 sid)
  261. {
  262. struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
  263. u32 evtyper;
  264. evtyper = get_event(event) | span << SMMU_PMCG_SID_SPAN_SHIFT;
  265. smmu_pmu_set_evtyper(smmu_pmu, idx, evtyper);
  266. smmu_pmu_set_smr(smmu_pmu, idx, sid);
  267. }
  268. static bool smmu_pmu_check_global_filter(struct perf_event *curr,
  269. struct perf_event *new)
  270. {
  271. if (get_filter_enable(new) != get_filter_enable(curr))
  272. return false;
  273. if (!get_filter_enable(new))
  274. return true;
  275. return get_filter_span(new) == get_filter_span(curr) &&
  276. get_filter_stream_id(new) == get_filter_stream_id(curr);
  277. }
  278. static int smmu_pmu_apply_event_filter(struct smmu_pmu *smmu_pmu,
  279. struct perf_event *event, int idx)
  280. {
  281. u32 span, sid;
  282. unsigned int cur_idx, num_ctrs = smmu_pmu->num_counters;
  283. bool filter_en = !!get_filter_enable(event);
  284. span = filter_en ? get_filter_span(event) :
  285. SMMU_PMCG_DEFAULT_FILTER_SPAN;
  286. sid = filter_en ? get_filter_stream_id(event) :
  287. SMMU_PMCG_DEFAULT_FILTER_SID;
  288. cur_idx = find_first_bit(smmu_pmu->used_counters, num_ctrs);
  289. /*
  290. * Per-counter filtering, or scheduling the first globally-filtered
  291. * event into an empty PMU so idx == 0 and it works out equivalent.
  292. */
  293. if (!smmu_pmu->global_filter || cur_idx == num_ctrs) {
  294. smmu_pmu_set_event_filter(event, idx, span, sid);
  295. return 0;
  296. }
  297. /* Otherwise, must match whatever's currently scheduled */
  298. if (smmu_pmu_check_global_filter(smmu_pmu->events[cur_idx], event)) {
  299. smmu_pmu_set_evtyper(smmu_pmu, idx, get_event(event));
  300. return 0;
  301. }
  302. return -EAGAIN;
  303. }
  304. static int smmu_pmu_get_event_idx(struct smmu_pmu *smmu_pmu,
  305. struct perf_event *event)
  306. {
  307. int idx, err;
  308. unsigned int num_ctrs = smmu_pmu->num_counters;
  309. idx = find_first_zero_bit(smmu_pmu->used_counters, num_ctrs);
  310. if (idx == num_ctrs)
  311. /* The counters are all in use. */
  312. return -EAGAIN;
  313. err = smmu_pmu_apply_event_filter(smmu_pmu, event, idx);
  314. if (err)
  315. return err;
  316. set_bit(idx, smmu_pmu->used_counters);
  317. return idx;
  318. }
  319. static bool smmu_pmu_events_compatible(struct perf_event *curr,
  320. struct perf_event *new)
  321. {
  322. if (new->pmu != curr->pmu)
  323. return false;
  324. if (to_smmu_pmu(new->pmu)->global_filter &&
  325. !smmu_pmu_check_global_filter(curr, new))
  326. return false;
  327. return true;
  328. }
  329. /*
  330. * Implementation of abstract pmu functionality required by
  331. * the core perf events code.
  332. */
  333. static int smmu_pmu_event_init(struct perf_event *event)
  334. {
  335. struct hw_perf_event *hwc = &event->hw;
  336. struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
  337. struct device *dev = smmu_pmu->dev;
  338. struct perf_event *sibling;
  339. int group_num_events = 1;
  340. u16 event_id;
  341. if (event->attr.type != event->pmu->type)
  342. return -ENOENT;
  343. if (hwc->sample_period) {
  344. dev_dbg(dev, "Sampling not supported\n");
  345. return -EOPNOTSUPP;
  346. }
  347. if (event->cpu < 0) {
  348. dev_dbg(dev, "Per-task mode not supported\n");
  349. return -EOPNOTSUPP;
  350. }
  351. /* Verify specified event is supported on this PMU */
  352. event_id = get_event(event);
  353. if (event_id < SMMU_PMCG_ARCH_MAX_EVENTS &&
  354. (!test_bit(event_id, smmu_pmu->supported_events))) {
  355. dev_dbg(dev, "Invalid event %d for this PMU\n", event_id);
  356. return -EINVAL;
  357. }
  358. /* Don't allow groups with mixed PMUs, except for s/w events */
  359. if (!is_software_event(event->group_leader)) {
  360. if (!smmu_pmu_events_compatible(event->group_leader, event))
  361. return -EINVAL;
  362. if (++group_num_events > smmu_pmu->num_counters)
  363. return -EINVAL;
  364. }
  365. /*
  366. * Ensure all events are on the same cpu so all events are in the
  367. * same cpu context, to avoid races on pmu_enable etc.
  368. */
  369. event->cpu = smmu_pmu->on_cpu;
  370. hwc->idx = -1;
  371. if (event->group_leader == event)
  372. return 0;
  373. for_each_sibling_event(sibling, event->group_leader) {
  374. if (is_software_event(sibling))
  375. continue;
  376. if (!smmu_pmu_events_compatible(sibling, event))
  377. return -EINVAL;
  378. if (++group_num_events > smmu_pmu->num_counters)
  379. return -EINVAL;
  380. }
  381. return 0;
  382. }
  383. static void smmu_pmu_event_start(struct perf_event *event, int flags)
  384. {
  385. struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
  386. struct hw_perf_event *hwc = &event->hw;
  387. int idx = hwc->idx;
  388. hwc->state = 0;
  389. smmu_pmu_set_period(smmu_pmu, hwc);
  390. smmu_pmu_counter_enable(smmu_pmu, idx);
  391. }
  392. static void smmu_pmu_event_stop(struct perf_event *event, int flags)
  393. {
  394. struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
  395. struct hw_perf_event *hwc = &event->hw;
  396. int idx = hwc->idx;
  397. if (hwc->state & PERF_HES_STOPPED)
  398. return;
  399. smmu_pmu_counter_disable(smmu_pmu, idx);
  400. /* As the counter gets updated on _start, ignore PERF_EF_UPDATE */
  401. smmu_pmu_event_update(event);
  402. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  403. }
  404. static int smmu_pmu_event_add(struct perf_event *event, int flags)
  405. {
  406. struct hw_perf_event *hwc = &event->hw;
  407. int idx;
  408. struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
  409. idx = smmu_pmu_get_event_idx(smmu_pmu, event);
  410. if (idx < 0)
  411. return idx;
  412. hwc->idx = idx;
  413. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  414. smmu_pmu->events[idx] = event;
  415. local64_set(&hwc->prev_count, 0);
  416. smmu_pmu_interrupt_enable(smmu_pmu, idx);
  417. if (flags & PERF_EF_START)
  418. smmu_pmu_event_start(event, flags);
  419. /* Propagate changes to the userspace mapping. */
  420. perf_event_update_userpage(event);
  421. return 0;
  422. }
  423. static void smmu_pmu_event_del(struct perf_event *event, int flags)
  424. {
  425. struct hw_perf_event *hwc = &event->hw;
  426. struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
  427. int idx = hwc->idx;
  428. smmu_pmu_event_stop(event, flags | PERF_EF_UPDATE);
  429. smmu_pmu_interrupt_disable(smmu_pmu, idx);
  430. smmu_pmu->events[idx] = NULL;
  431. clear_bit(idx, smmu_pmu->used_counters);
  432. perf_event_update_userpage(event);
  433. }
  434. static void smmu_pmu_event_read(struct perf_event *event)
  435. {
  436. smmu_pmu_event_update(event);
  437. }
  438. /* cpumask */
  439. static ssize_t smmu_pmu_cpumask_show(struct device *dev,
  440. struct device_attribute *attr,
  441. char *buf)
  442. {
  443. struct smmu_pmu *smmu_pmu = to_smmu_pmu(dev_get_drvdata(dev));
  444. return cpumap_print_to_pagebuf(true, buf, cpumask_of(smmu_pmu->on_cpu));
  445. }
  446. static struct device_attribute smmu_pmu_cpumask_attr =
  447. __ATTR(cpumask, 0444, smmu_pmu_cpumask_show, NULL);
  448. static struct attribute *smmu_pmu_cpumask_attrs[] = {
  449. &smmu_pmu_cpumask_attr.attr,
  450. NULL
  451. };
  452. static const struct attribute_group smmu_pmu_cpumask_group = {
  453. .attrs = smmu_pmu_cpumask_attrs,
  454. };
  455. /* Events */
  456. static ssize_t smmu_pmu_event_show(struct device *dev,
  457. struct device_attribute *attr, char *page)
  458. {
  459. struct perf_pmu_events_attr *pmu_attr;
  460. pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
  461. return sysfs_emit(page, "event=0x%02llx\n", pmu_attr->id);
  462. }
  463. #define SMMU_EVENT_ATTR(name, config) \
  464. PMU_EVENT_ATTR_ID(name, smmu_pmu_event_show, config)
  465. static struct attribute *smmu_pmu_events[] = {
  466. SMMU_EVENT_ATTR(cycles, 0),
  467. SMMU_EVENT_ATTR(transaction, 1),
  468. SMMU_EVENT_ATTR(tlb_miss, 2),
  469. SMMU_EVENT_ATTR(config_cache_miss, 3),
  470. SMMU_EVENT_ATTR(trans_table_walk_access, 4),
  471. SMMU_EVENT_ATTR(config_struct_access, 5),
  472. SMMU_EVENT_ATTR(pcie_ats_trans_rq, 6),
  473. SMMU_EVENT_ATTR(pcie_ats_trans_passed, 7),
  474. NULL
  475. };
  476. static umode_t smmu_pmu_event_is_visible(struct kobject *kobj,
  477. struct attribute *attr, int unused)
  478. {
  479. struct device *dev = kobj_to_dev(kobj);
  480. struct smmu_pmu *smmu_pmu = to_smmu_pmu(dev_get_drvdata(dev));
  481. struct perf_pmu_events_attr *pmu_attr;
  482. pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr.attr);
  483. if (test_bit(pmu_attr->id, smmu_pmu->supported_events))
  484. return attr->mode;
  485. return 0;
  486. }
  487. static const struct attribute_group smmu_pmu_events_group = {
  488. .name = "events",
  489. .attrs = smmu_pmu_events,
  490. .is_visible = smmu_pmu_event_is_visible,
  491. };
  492. static ssize_t smmu_pmu_identifier_attr_show(struct device *dev,
  493. struct device_attribute *attr,
  494. char *page)
  495. {
  496. struct smmu_pmu *smmu_pmu = to_smmu_pmu(dev_get_drvdata(dev));
  497. return sysfs_emit(page, "0x%08x\n", smmu_pmu->iidr);
  498. }
  499. static umode_t smmu_pmu_identifier_attr_visible(struct kobject *kobj,
  500. struct attribute *attr,
  501. int n)
  502. {
  503. struct device *dev = kobj_to_dev(kobj);
  504. struct smmu_pmu *smmu_pmu = to_smmu_pmu(dev_get_drvdata(dev));
  505. if (!smmu_pmu->iidr)
  506. return 0;
  507. return attr->mode;
  508. }
  509. static struct device_attribute smmu_pmu_identifier_attr =
  510. __ATTR(identifier, 0444, smmu_pmu_identifier_attr_show, NULL);
  511. static struct attribute *smmu_pmu_identifier_attrs[] = {
  512. &smmu_pmu_identifier_attr.attr,
  513. NULL
  514. };
  515. static const struct attribute_group smmu_pmu_identifier_group = {
  516. .attrs = smmu_pmu_identifier_attrs,
  517. .is_visible = smmu_pmu_identifier_attr_visible,
  518. };
  519. /* Formats */
  520. PMU_FORMAT_ATTR(event, "config:0-15");
  521. PMU_FORMAT_ATTR(filter_stream_id, "config1:0-31");
  522. PMU_FORMAT_ATTR(filter_span, "config1:32");
  523. PMU_FORMAT_ATTR(filter_enable, "config1:33");
  524. static struct attribute *smmu_pmu_formats[] = {
  525. &format_attr_event.attr,
  526. &format_attr_filter_stream_id.attr,
  527. &format_attr_filter_span.attr,
  528. &format_attr_filter_enable.attr,
  529. NULL
  530. };
  531. static const struct attribute_group smmu_pmu_format_group = {
  532. .name = "format",
  533. .attrs = smmu_pmu_formats,
  534. };
  535. static const struct attribute_group *smmu_pmu_attr_grps[] = {
  536. &smmu_pmu_cpumask_group,
  537. &smmu_pmu_events_group,
  538. &smmu_pmu_format_group,
  539. &smmu_pmu_identifier_group,
  540. NULL
  541. };
  542. /*
  543. * Generic device handlers
  544. */
  545. static int smmu_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
  546. {
  547. struct smmu_pmu *smmu_pmu;
  548. unsigned int target;
  549. smmu_pmu = hlist_entry_safe(node, struct smmu_pmu, node);
  550. if (cpu != smmu_pmu->on_cpu)
  551. return 0;
  552. target = cpumask_any_but(cpu_online_mask, cpu);
  553. if (target >= nr_cpu_ids)
  554. return 0;
  555. perf_pmu_migrate_context(&smmu_pmu->pmu, cpu, target);
  556. smmu_pmu->on_cpu = target;
  557. WARN_ON(irq_set_affinity(smmu_pmu->irq, cpumask_of(target)));
  558. return 0;
  559. }
  560. static irqreturn_t smmu_pmu_handle_irq(int irq_num, void *data)
  561. {
  562. struct smmu_pmu *smmu_pmu = data;
  563. DECLARE_BITMAP(ovs, BITS_PER_TYPE(u64));
  564. u64 ovsr;
  565. unsigned int idx;
  566. ovsr = readq(smmu_pmu->reloc_base + SMMU_PMCG_OVSSET0);
  567. if (!ovsr)
  568. return IRQ_NONE;
  569. writeq(ovsr, smmu_pmu->reloc_base + SMMU_PMCG_OVSCLR0);
  570. bitmap_from_u64(ovs, ovsr);
  571. for_each_set_bit(idx, ovs, smmu_pmu->num_counters) {
  572. struct perf_event *event = smmu_pmu->events[idx];
  573. struct hw_perf_event *hwc;
  574. if (WARN_ON_ONCE(!event))
  575. continue;
  576. smmu_pmu_event_update(event);
  577. hwc = &event->hw;
  578. smmu_pmu_set_period(smmu_pmu, hwc);
  579. }
  580. return IRQ_HANDLED;
  581. }
  582. static void smmu_pmu_free_msis(void *data)
  583. {
  584. struct device *dev = data;
  585. platform_device_msi_free_irqs_all(dev);
  586. }
  587. static void smmu_pmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
  588. {
  589. phys_addr_t doorbell;
  590. struct device *dev = msi_desc_to_dev(desc);
  591. struct smmu_pmu *pmu = dev_get_drvdata(dev);
  592. doorbell = (((u64)msg->address_hi) << 32) | msg->address_lo;
  593. doorbell &= MSI_CFG0_ADDR_MASK;
  594. writeq_relaxed(doorbell, pmu->reg_base + SMMU_PMCG_IRQ_CFG0);
  595. writel_relaxed(msg->data, pmu->reg_base + SMMU_PMCG_IRQ_CFG1);
  596. writel_relaxed(MSI_CFG2_MEMATTR_DEVICE_nGnRE,
  597. pmu->reg_base + SMMU_PMCG_IRQ_CFG2);
  598. }
  599. static void smmu_pmu_setup_msi(struct smmu_pmu *pmu)
  600. {
  601. struct device *dev = pmu->dev;
  602. int ret;
  603. /* Clear MSI address reg */
  604. writeq_relaxed(0, pmu->reg_base + SMMU_PMCG_IRQ_CFG0);
  605. /* MSI supported or not */
  606. if (!(readl(pmu->reg_base + SMMU_PMCG_CFGR) & SMMU_PMCG_CFGR_MSI))
  607. return;
  608. ret = platform_device_msi_init_and_alloc_irqs(dev, 1, smmu_pmu_write_msi_msg);
  609. if (ret) {
  610. dev_warn(dev, "failed to allocate MSIs\n");
  611. return;
  612. }
  613. pmu->irq = msi_get_virq(dev, 0);
  614. /* Add callback to free MSIs on teardown */
  615. devm_add_action(dev, smmu_pmu_free_msis, dev);
  616. }
  617. static int smmu_pmu_setup_irq(struct smmu_pmu *pmu)
  618. {
  619. unsigned long flags = IRQF_NOBALANCING | IRQF_SHARED | IRQF_NO_THREAD;
  620. int irq, ret = -ENXIO;
  621. smmu_pmu_setup_msi(pmu);
  622. irq = pmu->irq;
  623. if (irq)
  624. ret = devm_request_irq(pmu->dev, irq, smmu_pmu_handle_irq,
  625. flags, "smmuv3-pmu", pmu);
  626. return ret;
  627. }
  628. static void smmu_pmu_reset(struct smmu_pmu *smmu_pmu)
  629. {
  630. u64 counter_present_mask = GENMASK_ULL(smmu_pmu->num_counters - 1, 0);
  631. smmu_pmu_disable(&smmu_pmu->pmu);
  632. /* Disable counter and interrupt */
  633. writeq_relaxed(counter_present_mask,
  634. smmu_pmu->reg_base + SMMU_PMCG_CNTENCLR0);
  635. writeq_relaxed(counter_present_mask,
  636. smmu_pmu->reg_base + SMMU_PMCG_INTENCLR0);
  637. writeq_relaxed(counter_present_mask,
  638. smmu_pmu->reloc_base + SMMU_PMCG_OVSCLR0);
  639. }
  640. static void smmu_pmu_get_acpi_options(struct smmu_pmu *smmu_pmu)
  641. {
  642. u32 model;
  643. model = *(u32 *)dev_get_platdata(smmu_pmu->dev);
  644. switch (model) {
  645. case IORT_SMMU_V3_PMCG_HISI_HIP08:
  646. /* HiSilicon Erratum 162001800 */
  647. smmu_pmu->options |= SMMU_PMCG_EVCNTR_RDONLY | SMMU_PMCG_HARDEN_DISABLE;
  648. break;
  649. case IORT_SMMU_V3_PMCG_HISI_HIP09:
  650. smmu_pmu->options |= SMMU_PMCG_HARDEN_DISABLE;
  651. break;
  652. }
  653. dev_notice(smmu_pmu->dev, "option mask 0x%x\n", smmu_pmu->options);
  654. }
  655. static bool smmu_pmu_coresight_id_regs(struct smmu_pmu *smmu_pmu)
  656. {
  657. return of_device_is_compatible(smmu_pmu->dev->of_node,
  658. "arm,mmu-600-pmcg");
  659. }
  660. static void smmu_pmu_get_iidr(struct smmu_pmu *smmu_pmu)
  661. {
  662. u32 iidr = readl_relaxed(smmu_pmu->reg_base + SMMU_PMCG_IIDR);
  663. if (!iidr && smmu_pmu_coresight_id_regs(smmu_pmu)) {
  664. u32 pidr0 = readl(smmu_pmu->reg_base + SMMU_PMCG_PIDR0);
  665. u32 pidr1 = readl(smmu_pmu->reg_base + SMMU_PMCG_PIDR1);
  666. u32 pidr2 = readl(smmu_pmu->reg_base + SMMU_PMCG_PIDR2);
  667. u32 pidr3 = readl(smmu_pmu->reg_base + SMMU_PMCG_PIDR3);
  668. u32 pidr4 = readl(smmu_pmu->reg_base + SMMU_PMCG_PIDR4);
  669. u32 productid = FIELD_GET(SMMU_PMCG_PIDR0_PART_0, pidr0) |
  670. (FIELD_GET(SMMU_PMCG_PIDR1_PART_1, pidr1) << 8);
  671. u32 variant = FIELD_GET(SMMU_PMCG_PIDR2_REVISION, pidr2);
  672. u32 revision = FIELD_GET(SMMU_PMCG_PIDR3_REVAND, pidr3);
  673. u32 implementer =
  674. FIELD_GET(SMMU_PMCG_PIDR1_DES_0, pidr1) |
  675. (FIELD_GET(SMMU_PMCG_PIDR2_DES_1, pidr2) << 4) |
  676. (FIELD_GET(SMMU_PMCG_PIDR4_DES_2, pidr4) << 8);
  677. iidr = FIELD_PREP(SMMU_PMCG_IIDR_PRODUCTID, productid) |
  678. FIELD_PREP(SMMU_PMCG_IIDR_VARIANT, variant) |
  679. FIELD_PREP(SMMU_PMCG_IIDR_REVISION, revision) |
  680. FIELD_PREP(SMMU_PMCG_IIDR_IMPLEMENTER, implementer);
  681. }
  682. smmu_pmu->iidr = iidr;
  683. }
  684. static int smmu_pmu_probe(struct platform_device *pdev)
  685. {
  686. struct smmu_pmu *smmu_pmu;
  687. struct resource *res_0;
  688. u32 cfgr, reg_size;
  689. u64 ceid_64[2];
  690. int irq, err;
  691. char *name;
  692. struct device *dev = &pdev->dev;
  693. smmu_pmu = devm_kzalloc(dev, sizeof(*smmu_pmu), GFP_KERNEL);
  694. if (!smmu_pmu)
  695. return -ENOMEM;
  696. smmu_pmu->dev = dev;
  697. platform_set_drvdata(pdev, smmu_pmu);
  698. smmu_pmu->pmu = (struct pmu) {
  699. .module = THIS_MODULE,
  700. .parent = &pdev->dev,
  701. .task_ctx_nr = perf_invalid_context,
  702. .pmu_enable = smmu_pmu_enable,
  703. .pmu_disable = smmu_pmu_disable,
  704. .event_init = smmu_pmu_event_init,
  705. .add = smmu_pmu_event_add,
  706. .del = smmu_pmu_event_del,
  707. .start = smmu_pmu_event_start,
  708. .stop = smmu_pmu_event_stop,
  709. .read = smmu_pmu_event_read,
  710. .attr_groups = smmu_pmu_attr_grps,
  711. .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
  712. };
  713. smmu_pmu->reg_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res_0);
  714. if (IS_ERR(smmu_pmu->reg_base))
  715. return PTR_ERR(smmu_pmu->reg_base);
  716. cfgr = readl_relaxed(smmu_pmu->reg_base + SMMU_PMCG_CFGR);
  717. /* Determine if page 1 is present */
  718. if (cfgr & SMMU_PMCG_CFGR_RELOC_CTRS) {
  719. smmu_pmu->reloc_base = devm_platform_ioremap_resource(pdev, 1);
  720. if (IS_ERR(smmu_pmu->reloc_base))
  721. return PTR_ERR(smmu_pmu->reloc_base);
  722. } else {
  723. smmu_pmu->reloc_base = smmu_pmu->reg_base;
  724. }
  725. irq = platform_get_irq_optional(pdev, 0);
  726. if (irq > 0)
  727. smmu_pmu->irq = irq;
  728. ceid_64[0] = readq_relaxed(smmu_pmu->reg_base + SMMU_PMCG_CEID0);
  729. ceid_64[1] = readq_relaxed(smmu_pmu->reg_base + SMMU_PMCG_CEID1);
  730. bitmap_from_arr32(smmu_pmu->supported_events, (u32 *)ceid_64,
  731. SMMU_PMCG_ARCH_MAX_EVENTS);
  732. smmu_pmu->num_counters = FIELD_GET(SMMU_PMCG_CFGR_NCTR, cfgr) + 1;
  733. smmu_pmu->global_filter = !!(cfgr & SMMU_PMCG_CFGR_SID_FILTER_TYPE);
  734. reg_size = FIELD_GET(SMMU_PMCG_CFGR_SIZE, cfgr);
  735. smmu_pmu->counter_mask = GENMASK_ULL(reg_size, 0);
  736. smmu_pmu_reset(smmu_pmu);
  737. err = smmu_pmu_setup_irq(smmu_pmu);
  738. if (err) {
  739. dev_err(dev, "Setup irq failed, PMU @%pa\n", &res_0->start);
  740. return err;
  741. }
  742. smmu_pmu_get_iidr(smmu_pmu);
  743. name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "smmuv3_pmcg_%llx",
  744. (res_0->start) >> SMMU_PMCG_PA_SHIFT);
  745. if (!name) {
  746. dev_err(dev, "Create name failed, PMU @%pa\n", &res_0->start);
  747. return -EINVAL;
  748. }
  749. if (!dev->of_node)
  750. smmu_pmu_get_acpi_options(smmu_pmu);
  751. /*
  752. * For platforms suffer this quirk, the PMU disable sometimes fails to
  753. * stop the counters. This will leads to inaccurate or error counting.
  754. * Forcibly disable the counters with these quirk handler.
  755. */
  756. if (smmu_pmu->options & SMMU_PMCG_HARDEN_DISABLE) {
  757. smmu_pmu->pmu.pmu_enable = smmu_pmu_enable_quirk_hip08_09;
  758. smmu_pmu->pmu.pmu_disable = smmu_pmu_disable_quirk_hip08_09;
  759. }
  760. /* Pick one CPU to be the preferred one to use */
  761. smmu_pmu->on_cpu = raw_smp_processor_id();
  762. WARN_ON(irq_set_affinity(smmu_pmu->irq, cpumask_of(smmu_pmu->on_cpu)));
  763. err = cpuhp_state_add_instance_nocalls(cpuhp_state_num,
  764. &smmu_pmu->node);
  765. if (err) {
  766. dev_err(dev, "Error %d registering hotplug, PMU @%pa\n",
  767. err, &res_0->start);
  768. return err;
  769. }
  770. err = perf_pmu_register(&smmu_pmu->pmu, name, -1);
  771. if (err) {
  772. dev_err(dev, "Error %d registering PMU @%pa\n",
  773. err, &res_0->start);
  774. goto out_unregister;
  775. }
  776. dev_info(dev, "Registered PMU @ %pa using %d counters with %s filter settings\n",
  777. &res_0->start, smmu_pmu->num_counters,
  778. smmu_pmu->global_filter ? "Global(Counter0)" :
  779. "Individual");
  780. return 0;
  781. out_unregister:
  782. cpuhp_state_remove_instance_nocalls(cpuhp_state_num, &smmu_pmu->node);
  783. return err;
  784. }
  785. static void smmu_pmu_remove(struct platform_device *pdev)
  786. {
  787. struct smmu_pmu *smmu_pmu = platform_get_drvdata(pdev);
  788. perf_pmu_unregister(&smmu_pmu->pmu);
  789. cpuhp_state_remove_instance_nocalls(cpuhp_state_num, &smmu_pmu->node);
  790. }
  791. static void smmu_pmu_shutdown(struct platform_device *pdev)
  792. {
  793. struct smmu_pmu *smmu_pmu = platform_get_drvdata(pdev);
  794. smmu_pmu_disable(&smmu_pmu->pmu);
  795. }
  796. #ifdef CONFIG_OF
  797. static const struct of_device_id smmu_pmu_of_match[] = {
  798. { .compatible = "arm,smmu-v3-pmcg" },
  799. {}
  800. };
  801. MODULE_DEVICE_TABLE(of, smmu_pmu_of_match);
  802. #endif
  803. static struct platform_driver smmu_pmu_driver = {
  804. .driver = {
  805. .name = "arm-smmu-v3-pmcg",
  806. .of_match_table = of_match_ptr(smmu_pmu_of_match),
  807. .suppress_bind_attrs = true,
  808. },
  809. .probe = smmu_pmu_probe,
  810. .remove_new = smmu_pmu_remove,
  811. .shutdown = smmu_pmu_shutdown,
  812. };
  813. static int __init arm_smmu_pmu_init(void)
  814. {
  815. int ret;
  816. cpuhp_state_num = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
  817. "perf/arm/pmcg:online",
  818. NULL,
  819. smmu_pmu_offline_cpu);
  820. if (cpuhp_state_num < 0)
  821. return cpuhp_state_num;
  822. ret = platform_driver_register(&smmu_pmu_driver);
  823. if (ret)
  824. cpuhp_remove_multi_state(cpuhp_state_num);
  825. return ret;
  826. }
  827. module_init(arm_smmu_pmu_init);
  828. static void __exit arm_smmu_pmu_exit(void)
  829. {
  830. platform_driver_unregister(&smmu_pmu_driver);
  831. cpuhp_remove_multi_state(cpuhp_state_num);
  832. }
  833. module_exit(arm_smmu_pmu_exit);
  834. MODULE_ALIAS("platform:arm-smmu-v3-pmcg");
  835. MODULE_DESCRIPTION("PMU driver for ARM SMMUv3 Performance Monitors Extension");
  836. MODULE_AUTHOR("Neil Leeder <nleeder@codeaurora.org>");
  837. MODULE_AUTHOR("Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>");
  838. MODULE_LICENSE("GPL v2");