arm_smmuv3_pmu.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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. for_each_sibling_event(sibling, event->group_leader) {
  366. if (is_software_event(sibling))
  367. continue;
  368. if (!smmu_pmu_events_compatible(sibling, event))
  369. return -EINVAL;
  370. if (++group_num_events > smmu_pmu->num_counters)
  371. return -EINVAL;
  372. }
  373. hwc->idx = -1;
  374. /*
  375. * Ensure all events are on the same cpu so all events are in the
  376. * same cpu context, to avoid races on pmu_enable etc.
  377. */
  378. event->cpu = smmu_pmu->on_cpu;
  379. return 0;
  380. }
  381. static void smmu_pmu_event_start(struct perf_event *event, int flags)
  382. {
  383. struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
  384. struct hw_perf_event *hwc = &event->hw;
  385. int idx = hwc->idx;
  386. hwc->state = 0;
  387. smmu_pmu_set_period(smmu_pmu, hwc);
  388. smmu_pmu_counter_enable(smmu_pmu, idx);
  389. }
  390. static void smmu_pmu_event_stop(struct perf_event *event, int flags)
  391. {
  392. struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
  393. struct hw_perf_event *hwc = &event->hw;
  394. int idx = hwc->idx;
  395. if (hwc->state & PERF_HES_STOPPED)
  396. return;
  397. smmu_pmu_counter_disable(smmu_pmu, idx);
  398. /* As the counter gets updated on _start, ignore PERF_EF_UPDATE */
  399. smmu_pmu_event_update(event);
  400. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  401. }
  402. static int smmu_pmu_event_add(struct perf_event *event, int flags)
  403. {
  404. struct hw_perf_event *hwc = &event->hw;
  405. int idx;
  406. struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
  407. idx = smmu_pmu_get_event_idx(smmu_pmu, event);
  408. if (idx < 0)
  409. return idx;
  410. hwc->idx = idx;
  411. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  412. smmu_pmu->events[idx] = event;
  413. local64_set(&hwc->prev_count, 0);
  414. smmu_pmu_interrupt_enable(smmu_pmu, idx);
  415. if (flags & PERF_EF_START)
  416. smmu_pmu_event_start(event, flags);
  417. /* Propagate changes to the userspace mapping. */
  418. perf_event_update_userpage(event);
  419. return 0;
  420. }
  421. static void smmu_pmu_event_del(struct perf_event *event, int flags)
  422. {
  423. struct hw_perf_event *hwc = &event->hw;
  424. struct smmu_pmu *smmu_pmu = to_smmu_pmu(event->pmu);
  425. int idx = hwc->idx;
  426. smmu_pmu_event_stop(event, flags | PERF_EF_UPDATE);
  427. smmu_pmu_interrupt_disable(smmu_pmu, idx);
  428. smmu_pmu->events[idx] = NULL;
  429. clear_bit(idx, smmu_pmu->used_counters);
  430. perf_event_update_userpage(event);
  431. }
  432. static void smmu_pmu_event_read(struct perf_event *event)
  433. {
  434. smmu_pmu_event_update(event);
  435. }
  436. /* cpumask */
  437. static ssize_t smmu_pmu_cpumask_show(struct device *dev,
  438. struct device_attribute *attr,
  439. char *buf)
  440. {
  441. struct smmu_pmu *smmu_pmu = to_smmu_pmu(dev_get_drvdata(dev));
  442. return cpumap_print_to_pagebuf(true, buf, cpumask_of(smmu_pmu->on_cpu));
  443. }
  444. static struct device_attribute smmu_pmu_cpumask_attr =
  445. __ATTR(cpumask, 0444, smmu_pmu_cpumask_show, NULL);
  446. static struct attribute *smmu_pmu_cpumask_attrs[] = {
  447. &smmu_pmu_cpumask_attr.attr,
  448. NULL
  449. };
  450. static const struct attribute_group smmu_pmu_cpumask_group = {
  451. .attrs = smmu_pmu_cpumask_attrs,
  452. };
  453. /* Events */
  454. static ssize_t smmu_pmu_event_show(struct device *dev,
  455. struct device_attribute *attr, char *page)
  456. {
  457. struct perf_pmu_events_attr *pmu_attr;
  458. pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
  459. return sysfs_emit(page, "event=0x%02llx\n", pmu_attr->id);
  460. }
  461. #define SMMU_EVENT_ATTR(name, config) \
  462. PMU_EVENT_ATTR_ID(name, smmu_pmu_event_show, config)
  463. static struct attribute *smmu_pmu_events[] = {
  464. SMMU_EVENT_ATTR(cycles, 0),
  465. SMMU_EVENT_ATTR(transaction, 1),
  466. SMMU_EVENT_ATTR(tlb_miss, 2),
  467. SMMU_EVENT_ATTR(config_cache_miss, 3),
  468. SMMU_EVENT_ATTR(trans_table_walk_access, 4),
  469. SMMU_EVENT_ATTR(config_struct_access, 5),
  470. SMMU_EVENT_ATTR(pcie_ats_trans_rq, 6),
  471. SMMU_EVENT_ATTR(pcie_ats_trans_passed, 7),
  472. NULL
  473. };
  474. static umode_t smmu_pmu_event_is_visible(struct kobject *kobj,
  475. struct attribute *attr, int unused)
  476. {
  477. struct device *dev = kobj_to_dev(kobj);
  478. struct smmu_pmu *smmu_pmu = to_smmu_pmu(dev_get_drvdata(dev));
  479. struct perf_pmu_events_attr *pmu_attr;
  480. pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr.attr);
  481. if (test_bit(pmu_attr->id, smmu_pmu->supported_events))
  482. return attr->mode;
  483. return 0;
  484. }
  485. static const struct attribute_group smmu_pmu_events_group = {
  486. .name = "events",
  487. .attrs = smmu_pmu_events,
  488. .is_visible = smmu_pmu_event_is_visible,
  489. };
  490. static ssize_t smmu_pmu_identifier_attr_show(struct device *dev,
  491. struct device_attribute *attr,
  492. char *page)
  493. {
  494. struct smmu_pmu *smmu_pmu = to_smmu_pmu(dev_get_drvdata(dev));
  495. return sysfs_emit(page, "0x%08x\n", smmu_pmu->iidr);
  496. }
  497. static umode_t smmu_pmu_identifier_attr_visible(struct kobject *kobj,
  498. struct attribute *attr,
  499. int n)
  500. {
  501. struct device *dev = kobj_to_dev(kobj);
  502. struct smmu_pmu *smmu_pmu = to_smmu_pmu(dev_get_drvdata(dev));
  503. if (!smmu_pmu->iidr)
  504. return 0;
  505. return attr->mode;
  506. }
  507. static struct device_attribute smmu_pmu_identifier_attr =
  508. __ATTR(identifier, 0444, smmu_pmu_identifier_attr_show, NULL);
  509. static struct attribute *smmu_pmu_identifier_attrs[] = {
  510. &smmu_pmu_identifier_attr.attr,
  511. NULL
  512. };
  513. static const struct attribute_group smmu_pmu_identifier_group = {
  514. .attrs = smmu_pmu_identifier_attrs,
  515. .is_visible = smmu_pmu_identifier_attr_visible,
  516. };
  517. /* Formats */
  518. PMU_FORMAT_ATTR(event, "config:0-15");
  519. PMU_FORMAT_ATTR(filter_stream_id, "config1:0-31");
  520. PMU_FORMAT_ATTR(filter_span, "config1:32");
  521. PMU_FORMAT_ATTR(filter_enable, "config1:33");
  522. static struct attribute *smmu_pmu_formats[] = {
  523. &format_attr_event.attr,
  524. &format_attr_filter_stream_id.attr,
  525. &format_attr_filter_span.attr,
  526. &format_attr_filter_enable.attr,
  527. NULL
  528. };
  529. static const struct attribute_group smmu_pmu_format_group = {
  530. .name = "format",
  531. .attrs = smmu_pmu_formats,
  532. };
  533. static const struct attribute_group *smmu_pmu_attr_grps[] = {
  534. &smmu_pmu_cpumask_group,
  535. &smmu_pmu_events_group,
  536. &smmu_pmu_format_group,
  537. &smmu_pmu_identifier_group,
  538. NULL
  539. };
  540. /*
  541. * Generic device handlers
  542. */
  543. static int smmu_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
  544. {
  545. struct smmu_pmu *smmu_pmu;
  546. unsigned int target;
  547. smmu_pmu = hlist_entry_safe(node, struct smmu_pmu, node);
  548. if (cpu != smmu_pmu->on_cpu)
  549. return 0;
  550. target = cpumask_any_but(cpu_online_mask, cpu);
  551. if (target >= nr_cpu_ids)
  552. return 0;
  553. perf_pmu_migrate_context(&smmu_pmu->pmu, cpu, target);
  554. smmu_pmu->on_cpu = target;
  555. WARN_ON(irq_set_affinity(smmu_pmu->irq, cpumask_of(target)));
  556. return 0;
  557. }
  558. static irqreturn_t smmu_pmu_handle_irq(int irq_num, void *data)
  559. {
  560. struct smmu_pmu *smmu_pmu = data;
  561. DECLARE_BITMAP(ovs, BITS_PER_TYPE(u64));
  562. u64 ovsr;
  563. unsigned int idx;
  564. ovsr = readq(smmu_pmu->reloc_base + SMMU_PMCG_OVSSET0);
  565. if (!ovsr)
  566. return IRQ_NONE;
  567. writeq(ovsr, smmu_pmu->reloc_base + SMMU_PMCG_OVSCLR0);
  568. bitmap_from_u64(ovs, ovsr);
  569. for_each_set_bit(idx, ovs, smmu_pmu->num_counters) {
  570. struct perf_event *event = smmu_pmu->events[idx];
  571. struct hw_perf_event *hwc;
  572. if (WARN_ON_ONCE(!event))
  573. continue;
  574. smmu_pmu_event_update(event);
  575. hwc = &event->hw;
  576. smmu_pmu_set_period(smmu_pmu, hwc);
  577. }
  578. return IRQ_HANDLED;
  579. }
  580. static void smmu_pmu_free_msis(void *data)
  581. {
  582. struct device *dev = data;
  583. platform_msi_domain_free_irqs(dev);
  584. }
  585. static void smmu_pmu_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
  586. {
  587. phys_addr_t doorbell;
  588. struct device *dev = msi_desc_to_dev(desc);
  589. struct smmu_pmu *pmu = dev_get_drvdata(dev);
  590. doorbell = (((u64)msg->address_hi) << 32) | msg->address_lo;
  591. doorbell &= MSI_CFG0_ADDR_MASK;
  592. writeq_relaxed(doorbell, pmu->reg_base + SMMU_PMCG_IRQ_CFG0);
  593. writel_relaxed(msg->data, pmu->reg_base + SMMU_PMCG_IRQ_CFG1);
  594. writel_relaxed(MSI_CFG2_MEMATTR_DEVICE_nGnRE,
  595. pmu->reg_base + SMMU_PMCG_IRQ_CFG2);
  596. }
  597. static void smmu_pmu_setup_msi(struct smmu_pmu *pmu)
  598. {
  599. struct device *dev = pmu->dev;
  600. int ret;
  601. /* Clear MSI address reg */
  602. writeq_relaxed(0, pmu->reg_base + SMMU_PMCG_IRQ_CFG0);
  603. /* MSI supported or not */
  604. if (!(readl(pmu->reg_base + SMMU_PMCG_CFGR) & SMMU_PMCG_CFGR_MSI))
  605. return;
  606. ret = platform_msi_domain_alloc_irqs(dev, 1, smmu_pmu_write_msi_msg);
  607. if (ret) {
  608. dev_warn(dev, "failed to allocate MSIs\n");
  609. return;
  610. }
  611. pmu->irq = msi_get_virq(dev, 0);
  612. /* Add callback to free MSIs on teardown */
  613. devm_add_action(dev, smmu_pmu_free_msis, dev);
  614. }
  615. static int smmu_pmu_setup_irq(struct smmu_pmu *pmu)
  616. {
  617. unsigned long flags = IRQF_NOBALANCING | IRQF_SHARED | IRQF_NO_THREAD;
  618. int irq, ret = -ENXIO;
  619. smmu_pmu_setup_msi(pmu);
  620. irq = pmu->irq;
  621. if (irq)
  622. ret = devm_request_irq(pmu->dev, irq, smmu_pmu_handle_irq,
  623. flags, "smmuv3-pmu", pmu);
  624. return ret;
  625. }
  626. static void smmu_pmu_reset(struct smmu_pmu *smmu_pmu)
  627. {
  628. u64 counter_present_mask = GENMASK_ULL(smmu_pmu->num_counters - 1, 0);
  629. smmu_pmu_disable(&smmu_pmu->pmu);
  630. /* Disable counter and interrupt */
  631. writeq_relaxed(counter_present_mask,
  632. smmu_pmu->reg_base + SMMU_PMCG_CNTENCLR0);
  633. writeq_relaxed(counter_present_mask,
  634. smmu_pmu->reg_base + SMMU_PMCG_INTENCLR0);
  635. writeq_relaxed(counter_present_mask,
  636. smmu_pmu->reloc_base + SMMU_PMCG_OVSCLR0);
  637. }
  638. static void smmu_pmu_get_acpi_options(struct smmu_pmu *smmu_pmu)
  639. {
  640. u32 model;
  641. model = *(u32 *)dev_get_platdata(smmu_pmu->dev);
  642. switch (model) {
  643. case IORT_SMMU_V3_PMCG_HISI_HIP08:
  644. /* HiSilicon Erratum 162001800 */
  645. smmu_pmu->options |= SMMU_PMCG_EVCNTR_RDONLY | SMMU_PMCG_HARDEN_DISABLE;
  646. break;
  647. case IORT_SMMU_V3_PMCG_HISI_HIP09:
  648. smmu_pmu->options |= SMMU_PMCG_HARDEN_DISABLE;
  649. break;
  650. }
  651. dev_notice(smmu_pmu->dev, "option mask 0x%x\n", smmu_pmu->options);
  652. }
  653. static bool smmu_pmu_coresight_id_regs(struct smmu_pmu *smmu_pmu)
  654. {
  655. return of_device_is_compatible(smmu_pmu->dev->of_node,
  656. "arm,mmu-600-pmcg");
  657. }
  658. static void smmu_pmu_get_iidr(struct smmu_pmu *smmu_pmu)
  659. {
  660. u32 iidr = readl_relaxed(smmu_pmu->reg_base + SMMU_PMCG_IIDR);
  661. if (!iidr && smmu_pmu_coresight_id_regs(smmu_pmu)) {
  662. u32 pidr0 = readl(smmu_pmu->reg_base + SMMU_PMCG_PIDR0);
  663. u32 pidr1 = readl(smmu_pmu->reg_base + SMMU_PMCG_PIDR1);
  664. u32 pidr2 = readl(smmu_pmu->reg_base + SMMU_PMCG_PIDR2);
  665. u32 pidr3 = readl(smmu_pmu->reg_base + SMMU_PMCG_PIDR3);
  666. u32 pidr4 = readl(smmu_pmu->reg_base + SMMU_PMCG_PIDR4);
  667. u32 productid = FIELD_GET(SMMU_PMCG_PIDR0_PART_0, pidr0) |
  668. (FIELD_GET(SMMU_PMCG_PIDR1_PART_1, pidr1) << 8);
  669. u32 variant = FIELD_GET(SMMU_PMCG_PIDR2_REVISION, pidr2);
  670. u32 revision = FIELD_GET(SMMU_PMCG_PIDR3_REVAND, pidr3);
  671. u32 implementer =
  672. FIELD_GET(SMMU_PMCG_PIDR1_DES_0, pidr1) |
  673. (FIELD_GET(SMMU_PMCG_PIDR2_DES_1, pidr2) << 4) |
  674. (FIELD_GET(SMMU_PMCG_PIDR4_DES_2, pidr4) << 8);
  675. iidr = FIELD_PREP(SMMU_PMCG_IIDR_PRODUCTID, productid) |
  676. FIELD_PREP(SMMU_PMCG_IIDR_VARIANT, variant) |
  677. FIELD_PREP(SMMU_PMCG_IIDR_REVISION, revision) |
  678. FIELD_PREP(SMMU_PMCG_IIDR_IMPLEMENTER, implementer);
  679. }
  680. smmu_pmu->iidr = iidr;
  681. }
  682. static int smmu_pmu_probe(struct platform_device *pdev)
  683. {
  684. struct smmu_pmu *smmu_pmu;
  685. struct resource *res_0;
  686. u32 cfgr, reg_size;
  687. u64 ceid_64[2];
  688. int irq, err;
  689. char *name;
  690. struct device *dev = &pdev->dev;
  691. smmu_pmu = devm_kzalloc(dev, sizeof(*smmu_pmu), GFP_KERNEL);
  692. if (!smmu_pmu)
  693. return -ENOMEM;
  694. smmu_pmu->dev = dev;
  695. platform_set_drvdata(pdev, smmu_pmu);
  696. smmu_pmu->pmu = (struct pmu) {
  697. .module = THIS_MODULE,
  698. .task_ctx_nr = perf_invalid_context,
  699. .pmu_enable = smmu_pmu_enable,
  700. .pmu_disable = smmu_pmu_disable,
  701. .event_init = smmu_pmu_event_init,
  702. .add = smmu_pmu_event_add,
  703. .del = smmu_pmu_event_del,
  704. .start = smmu_pmu_event_start,
  705. .stop = smmu_pmu_event_stop,
  706. .read = smmu_pmu_event_read,
  707. .attr_groups = smmu_pmu_attr_grps,
  708. .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
  709. };
  710. smmu_pmu->reg_base = devm_platform_get_and_ioremap_resource(pdev, 0, &res_0);
  711. if (IS_ERR(smmu_pmu->reg_base))
  712. return PTR_ERR(smmu_pmu->reg_base);
  713. cfgr = readl_relaxed(smmu_pmu->reg_base + SMMU_PMCG_CFGR);
  714. /* Determine if page 1 is present */
  715. if (cfgr & SMMU_PMCG_CFGR_RELOC_CTRS) {
  716. smmu_pmu->reloc_base = devm_platform_ioremap_resource(pdev, 1);
  717. if (IS_ERR(smmu_pmu->reloc_base))
  718. return PTR_ERR(smmu_pmu->reloc_base);
  719. } else {
  720. smmu_pmu->reloc_base = smmu_pmu->reg_base;
  721. }
  722. irq = platform_get_irq_optional(pdev, 0);
  723. if (irq > 0)
  724. smmu_pmu->irq = irq;
  725. ceid_64[0] = readq_relaxed(smmu_pmu->reg_base + SMMU_PMCG_CEID0);
  726. ceid_64[1] = readq_relaxed(smmu_pmu->reg_base + SMMU_PMCG_CEID1);
  727. bitmap_from_arr32(smmu_pmu->supported_events, (u32 *)ceid_64,
  728. SMMU_PMCG_ARCH_MAX_EVENTS);
  729. smmu_pmu->num_counters = FIELD_GET(SMMU_PMCG_CFGR_NCTR, cfgr) + 1;
  730. smmu_pmu->global_filter = !!(cfgr & SMMU_PMCG_CFGR_SID_FILTER_TYPE);
  731. reg_size = FIELD_GET(SMMU_PMCG_CFGR_SIZE, cfgr);
  732. smmu_pmu->counter_mask = GENMASK_ULL(reg_size, 0);
  733. smmu_pmu_reset(smmu_pmu);
  734. err = smmu_pmu_setup_irq(smmu_pmu);
  735. if (err) {
  736. dev_err(dev, "Setup irq failed, PMU @%pa\n", &res_0->start);
  737. return err;
  738. }
  739. smmu_pmu_get_iidr(smmu_pmu);
  740. name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "smmuv3_pmcg_%llx",
  741. (res_0->start) >> SMMU_PMCG_PA_SHIFT);
  742. if (!name) {
  743. dev_err(dev, "Create name failed, PMU @%pa\n", &res_0->start);
  744. return -EINVAL;
  745. }
  746. if (!dev->of_node)
  747. smmu_pmu_get_acpi_options(smmu_pmu);
  748. /*
  749. * For platforms suffer this quirk, the PMU disable sometimes fails to
  750. * stop the counters. This will leads to inaccurate or error counting.
  751. * Forcibly disable the counters with these quirk handler.
  752. */
  753. if (smmu_pmu->options & SMMU_PMCG_HARDEN_DISABLE) {
  754. smmu_pmu->pmu.pmu_enable = smmu_pmu_enable_quirk_hip08_09;
  755. smmu_pmu->pmu.pmu_disable = smmu_pmu_disable_quirk_hip08_09;
  756. }
  757. /* Pick one CPU to be the preferred one to use */
  758. smmu_pmu->on_cpu = raw_smp_processor_id();
  759. WARN_ON(irq_set_affinity(smmu_pmu->irq, cpumask_of(smmu_pmu->on_cpu)));
  760. err = cpuhp_state_add_instance_nocalls(cpuhp_state_num,
  761. &smmu_pmu->node);
  762. if (err) {
  763. dev_err(dev, "Error %d registering hotplug, PMU @%pa\n",
  764. err, &res_0->start);
  765. return err;
  766. }
  767. err = perf_pmu_register(&smmu_pmu->pmu, name, -1);
  768. if (err) {
  769. dev_err(dev, "Error %d registering PMU @%pa\n",
  770. err, &res_0->start);
  771. goto out_unregister;
  772. }
  773. dev_info(dev, "Registered PMU @ %pa using %d counters with %s filter settings\n",
  774. &res_0->start, smmu_pmu->num_counters,
  775. smmu_pmu->global_filter ? "Global(Counter0)" :
  776. "Individual");
  777. return 0;
  778. out_unregister:
  779. cpuhp_state_remove_instance_nocalls(cpuhp_state_num, &smmu_pmu->node);
  780. return err;
  781. }
  782. static int smmu_pmu_remove(struct platform_device *pdev)
  783. {
  784. struct smmu_pmu *smmu_pmu = platform_get_drvdata(pdev);
  785. perf_pmu_unregister(&smmu_pmu->pmu);
  786. cpuhp_state_remove_instance_nocalls(cpuhp_state_num, &smmu_pmu->node);
  787. return 0;
  788. }
  789. static void smmu_pmu_shutdown(struct platform_device *pdev)
  790. {
  791. struct smmu_pmu *smmu_pmu = platform_get_drvdata(pdev);
  792. smmu_pmu_disable(&smmu_pmu->pmu);
  793. }
  794. #ifdef CONFIG_OF
  795. static const struct of_device_id smmu_pmu_of_match[] = {
  796. { .compatible = "arm,smmu-v3-pmcg" },
  797. {}
  798. };
  799. MODULE_DEVICE_TABLE(of, smmu_pmu_of_match);
  800. #endif
  801. static struct platform_driver smmu_pmu_driver = {
  802. .driver = {
  803. .name = "arm-smmu-v3-pmcg",
  804. .of_match_table = of_match_ptr(smmu_pmu_of_match),
  805. .suppress_bind_attrs = true,
  806. },
  807. .probe = smmu_pmu_probe,
  808. .remove = smmu_pmu_remove,
  809. .shutdown = smmu_pmu_shutdown,
  810. };
  811. static int __init arm_smmu_pmu_init(void)
  812. {
  813. int ret;
  814. cpuhp_state_num = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
  815. "perf/arm/pmcg:online",
  816. NULL,
  817. smmu_pmu_offline_cpu);
  818. if (cpuhp_state_num < 0)
  819. return cpuhp_state_num;
  820. ret = platform_driver_register(&smmu_pmu_driver);
  821. if (ret)
  822. cpuhp_remove_multi_state(cpuhp_state_num);
  823. return ret;
  824. }
  825. module_init(arm_smmu_pmu_init);
  826. static void __exit arm_smmu_pmu_exit(void)
  827. {
  828. platform_driver_unregister(&smmu_pmu_driver);
  829. cpuhp_remove_multi_state(cpuhp_state_num);
  830. }
  831. module_exit(arm_smmu_pmu_exit);
  832. MODULE_DESCRIPTION("PMU driver for ARM SMMUv3 Performance Monitors Extension");
  833. MODULE_AUTHOR("Neil Leeder <[email protected]>");
  834. MODULE_AUTHOR("Shameer Kolothum <[email protected]>");
  835. MODULE_LICENSE("GPL v2");