thunderx2_pmu.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * CAVIUM THUNDERX2 SoC PMU UNCORE
  4. * Copyright (C) 2018 Cavium Inc.
  5. * Author: Ganapatrao Kulkarni <gkulkarni@cavium.com>
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/cpuhotplug.h>
  9. #include <linux/perf_event.h>
  10. #include <linux/platform_device.h>
  11. /* Each ThunderX2(TX2) Socket has a L3C and DMC UNCORE PMU device.
  12. * Each UNCORE PMU device consists of 4 independent programmable counters.
  13. * Counters are 32 bit and do not support overflow interrupt,
  14. * they need to be sampled before overflow(i.e, at every 2 seconds).
  15. */
  16. #define TX2_PMU_DMC_L3C_MAX_COUNTERS 4
  17. #define TX2_PMU_CCPI2_MAX_COUNTERS 8
  18. #define TX2_PMU_MAX_COUNTERS TX2_PMU_CCPI2_MAX_COUNTERS
  19. #define TX2_PMU_DMC_CHANNELS 8
  20. #define TX2_PMU_L3_TILES 16
  21. #define TX2_PMU_HRTIMER_INTERVAL (2 * NSEC_PER_SEC)
  22. #define GET_EVENTID(ev, mask) ((ev->hw.config) & mask)
  23. #define GET_COUNTERID(ev, mask) ((ev->hw.idx) & mask)
  24. /* 1 byte per counter(4 counters).
  25. * Event id is encoded in bits [5:1] of a byte,
  26. */
  27. #define DMC_EVENT_CFG(idx, val) ((val) << (((idx) * 8) + 1))
  28. /* bits[3:0] to select counters, are indexed from 8 to 15. */
  29. #define CCPI2_COUNTER_OFFSET 8
  30. #define L3C_COUNTER_CTL 0xA8
  31. #define L3C_COUNTER_DATA 0xAC
  32. #define DMC_COUNTER_CTL 0x234
  33. #define DMC_COUNTER_DATA 0x240
  34. #define CCPI2_PERF_CTL 0x108
  35. #define CCPI2_COUNTER_CTL 0x10C
  36. #define CCPI2_COUNTER_SEL 0x12c
  37. #define CCPI2_COUNTER_DATA_L 0x130
  38. #define CCPI2_COUNTER_DATA_H 0x134
  39. /* L3C event IDs */
  40. #define L3_EVENT_READ_REQ 0xD
  41. #define L3_EVENT_WRITEBACK_REQ 0xE
  42. #define L3_EVENT_INV_N_WRITE_REQ 0xF
  43. #define L3_EVENT_INV_REQ 0x10
  44. #define L3_EVENT_EVICT_REQ 0x13
  45. #define L3_EVENT_INV_N_WRITE_HIT 0x14
  46. #define L3_EVENT_INV_HIT 0x15
  47. #define L3_EVENT_READ_HIT 0x17
  48. #define L3_EVENT_MAX 0x18
  49. /* DMC event IDs */
  50. #define DMC_EVENT_COUNT_CYCLES 0x1
  51. #define DMC_EVENT_WRITE_TXNS 0xB
  52. #define DMC_EVENT_DATA_TRANSFERS 0xD
  53. #define DMC_EVENT_READ_TXNS 0xF
  54. #define DMC_EVENT_MAX 0x10
  55. #define CCPI2_EVENT_REQ_PKT_SENT 0x3D
  56. #define CCPI2_EVENT_SNOOP_PKT_SENT 0x65
  57. #define CCPI2_EVENT_DATA_PKT_SENT 0x105
  58. #define CCPI2_EVENT_GIC_PKT_SENT 0x12D
  59. #define CCPI2_EVENT_MAX 0x200
  60. #define CCPI2_PERF_CTL_ENABLE BIT(0)
  61. #define CCPI2_PERF_CTL_START BIT(1)
  62. #define CCPI2_PERF_CTL_RESET BIT(4)
  63. #define CCPI2_EVENT_LEVEL_RISING_EDGE BIT(10)
  64. #define CCPI2_EVENT_TYPE_EDGE_SENSITIVE BIT(11)
  65. enum tx2_uncore_type {
  66. PMU_TYPE_L3C,
  67. PMU_TYPE_DMC,
  68. PMU_TYPE_CCPI2,
  69. PMU_TYPE_INVALID,
  70. };
  71. /*
  72. * Each socket has 3 uncore devices associated with a PMU. The DMC and
  73. * L3C have 4 32-bit counters and the CCPI2 has 8 64-bit counters.
  74. */
  75. struct tx2_uncore_pmu {
  76. struct hlist_node hpnode;
  77. struct list_head entry;
  78. struct pmu pmu;
  79. char *name;
  80. int node;
  81. int cpu;
  82. u32 max_counters;
  83. u32 counters_mask;
  84. u32 prorate_factor;
  85. u32 max_events;
  86. u32 events_mask;
  87. u64 hrtimer_interval;
  88. void __iomem *base;
  89. DECLARE_BITMAP(active_counters, TX2_PMU_MAX_COUNTERS);
  90. struct perf_event *events[TX2_PMU_MAX_COUNTERS];
  91. struct device *dev;
  92. struct hrtimer hrtimer;
  93. const struct attribute_group **attr_groups;
  94. enum tx2_uncore_type type;
  95. enum hrtimer_restart (*hrtimer_callback)(struct hrtimer *cb);
  96. void (*init_cntr_base)(struct perf_event *event,
  97. struct tx2_uncore_pmu *tx2_pmu);
  98. void (*stop_event)(struct perf_event *event);
  99. void (*start_event)(struct perf_event *event, int flags);
  100. };
  101. static LIST_HEAD(tx2_pmus);
  102. static inline struct tx2_uncore_pmu *pmu_to_tx2_pmu(struct pmu *pmu)
  103. {
  104. return container_of(pmu, struct tx2_uncore_pmu, pmu);
  105. }
  106. #define TX2_PMU_FORMAT_ATTR(_var, _name, _format) \
  107. static ssize_t \
  108. __tx2_pmu_##_var##_show(struct device *dev, \
  109. struct device_attribute *attr, \
  110. char *page) \
  111. { \
  112. BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
  113. return sysfs_emit(page, _format "\n"); \
  114. } \
  115. \
  116. static struct device_attribute format_attr_##_var = \
  117. __ATTR(_name, 0444, __tx2_pmu_##_var##_show, NULL)
  118. TX2_PMU_FORMAT_ATTR(event, event, "config:0-4");
  119. TX2_PMU_FORMAT_ATTR(event_ccpi2, event, "config:0-9");
  120. static struct attribute *l3c_pmu_format_attrs[] = {
  121. &format_attr_event.attr,
  122. NULL,
  123. };
  124. static struct attribute *dmc_pmu_format_attrs[] = {
  125. &format_attr_event.attr,
  126. NULL,
  127. };
  128. static struct attribute *ccpi2_pmu_format_attrs[] = {
  129. &format_attr_event_ccpi2.attr,
  130. NULL,
  131. };
  132. static const struct attribute_group l3c_pmu_format_attr_group = {
  133. .name = "format",
  134. .attrs = l3c_pmu_format_attrs,
  135. };
  136. static const struct attribute_group dmc_pmu_format_attr_group = {
  137. .name = "format",
  138. .attrs = dmc_pmu_format_attrs,
  139. };
  140. static const struct attribute_group ccpi2_pmu_format_attr_group = {
  141. .name = "format",
  142. .attrs = ccpi2_pmu_format_attrs,
  143. };
  144. /*
  145. * sysfs event attributes
  146. */
  147. static ssize_t tx2_pmu_event_show(struct device *dev,
  148. struct device_attribute *attr, char *buf)
  149. {
  150. struct dev_ext_attribute *eattr;
  151. eattr = container_of(attr, struct dev_ext_attribute, attr);
  152. return sysfs_emit(buf, "event=0x%lx\n", (unsigned long) eattr->var);
  153. }
  154. #define TX2_EVENT_ATTR(name, config) \
  155. PMU_EVENT_ATTR(name, tx2_pmu_event_attr_##name, \
  156. config, tx2_pmu_event_show)
  157. TX2_EVENT_ATTR(read_request, L3_EVENT_READ_REQ);
  158. TX2_EVENT_ATTR(writeback_request, L3_EVENT_WRITEBACK_REQ);
  159. TX2_EVENT_ATTR(inv_nwrite_request, L3_EVENT_INV_N_WRITE_REQ);
  160. TX2_EVENT_ATTR(inv_request, L3_EVENT_INV_REQ);
  161. TX2_EVENT_ATTR(evict_request, L3_EVENT_EVICT_REQ);
  162. TX2_EVENT_ATTR(inv_nwrite_hit, L3_EVENT_INV_N_WRITE_HIT);
  163. TX2_EVENT_ATTR(inv_hit, L3_EVENT_INV_HIT);
  164. TX2_EVENT_ATTR(read_hit, L3_EVENT_READ_HIT);
  165. static struct attribute *l3c_pmu_events_attrs[] = {
  166. &tx2_pmu_event_attr_read_request.attr.attr,
  167. &tx2_pmu_event_attr_writeback_request.attr.attr,
  168. &tx2_pmu_event_attr_inv_nwrite_request.attr.attr,
  169. &tx2_pmu_event_attr_inv_request.attr.attr,
  170. &tx2_pmu_event_attr_evict_request.attr.attr,
  171. &tx2_pmu_event_attr_inv_nwrite_hit.attr.attr,
  172. &tx2_pmu_event_attr_inv_hit.attr.attr,
  173. &tx2_pmu_event_attr_read_hit.attr.attr,
  174. NULL,
  175. };
  176. TX2_EVENT_ATTR(cnt_cycles, DMC_EVENT_COUNT_CYCLES);
  177. TX2_EVENT_ATTR(write_txns, DMC_EVENT_WRITE_TXNS);
  178. TX2_EVENT_ATTR(data_transfers, DMC_EVENT_DATA_TRANSFERS);
  179. TX2_EVENT_ATTR(read_txns, DMC_EVENT_READ_TXNS);
  180. static struct attribute *dmc_pmu_events_attrs[] = {
  181. &tx2_pmu_event_attr_cnt_cycles.attr.attr,
  182. &tx2_pmu_event_attr_write_txns.attr.attr,
  183. &tx2_pmu_event_attr_data_transfers.attr.attr,
  184. &tx2_pmu_event_attr_read_txns.attr.attr,
  185. NULL,
  186. };
  187. TX2_EVENT_ATTR(req_pktsent, CCPI2_EVENT_REQ_PKT_SENT);
  188. TX2_EVENT_ATTR(snoop_pktsent, CCPI2_EVENT_SNOOP_PKT_SENT);
  189. TX2_EVENT_ATTR(data_pktsent, CCPI2_EVENT_DATA_PKT_SENT);
  190. TX2_EVENT_ATTR(gic_pktsent, CCPI2_EVENT_GIC_PKT_SENT);
  191. static struct attribute *ccpi2_pmu_events_attrs[] = {
  192. &tx2_pmu_event_attr_req_pktsent.attr.attr,
  193. &tx2_pmu_event_attr_snoop_pktsent.attr.attr,
  194. &tx2_pmu_event_attr_data_pktsent.attr.attr,
  195. &tx2_pmu_event_attr_gic_pktsent.attr.attr,
  196. NULL,
  197. };
  198. static const struct attribute_group l3c_pmu_events_attr_group = {
  199. .name = "events",
  200. .attrs = l3c_pmu_events_attrs,
  201. };
  202. static const struct attribute_group dmc_pmu_events_attr_group = {
  203. .name = "events",
  204. .attrs = dmc_pmu_events_attrs,
  205. };
  206. static const struct attribute_group ccpi2_pmu_events_attr_group = {
  207. .name = "events",
  208. .attrs = ccpi2_pmu_events_attrs,
  209. };
  210. /*
  211. * sysfs cpumask attributes
  212. */
  213. static ssize_t cpumask_show(struct device *dev, struct device_attribute *attr,
  214. char *buf)
  215. {
  216. struct tx2_uncore_pmu *tx2_pmu;
  217. tx2_pmu = pmu_to_tx2_pmu(dev_get_drvdata(dev));
  218. return cpumap_print_to_pagebuf(true, buf, cpumask_of(tx2_pmu->cpu));
  219. }
  220. static DEVICE_ATTR_RO(cpumask);
  221. static struct attribute *tx2_pmu_cpumask_attrs[] = {
  222. &dev_attr_cpumask.attr,
  223. NULL,
  224. };
  225. static const struct attribute_group pmu_cpumask_attr_group = {
  226. .attrs = tx2_pmu_cpumask_attrs,
  227. };
  228. /*
  229. * Per PMU device attribute groups
  230. */
  231. static const struct attribute_group *l3c_pmu_attr_groups[] = {
  232. &l3c_pmu_format_attr_group,
  233. &pmu_cpumask_attr_group,
  234. &l3c_pmu_events_attr_group,
  235. NULL
  236. };
  237. static const struct attribute_group *dmc_pmu_attr_groups[] = {
  238. &dmc_pmu_format_attr_group,
  239. &pmu_cpumask_attr_group,
  240. &dmc_pmu_events_attr_group,
  241. NULL
  242. };
  243. static const struct attribute_group *ccpi2_pmu_attr_groups[] = {
  244. &ccpi2_pmu_format_attr_group,
  245. &pmu_cpumask_attr_group,
  246. &ccpi2_pmu_events_attr_group,
  247. NULL
  248. };
  249. static inline u32 reg_readl(unsigned long addr)
  250. {
  251. return readl((void __iomem *)addr);
  252. }
  253. static inline void reg_writel(u32 val, unsigned long addr)
  254. {
  255. writel(val, (void __iomem *)addr);
  256. }
  257. static int alloc_counter(struct tx2_uncore_pmu *tx2_pmu)
  258. {
  259. int counter;
  260. counter = find_first_zero_bit(tx2_pmu->active_counters,
  261. tx2_pmu->max_counters);
  262. if (counter == tx2_pmu->max_counters)
  263. return -ENOSPC;
  264. set_bit(counter, tx2_pmu->active_counters);
  265. return counter;
  266. }
  267. static inline void free_counter(struct tx2_uncore_pmu *tx2_pmu, int counter)
  268. {
  269. clear_bit(counter, tx2_pmu->active_counters);
  270. }
  271. static void init_cntr_base_l3c(struct perf_event *event,
  272. struct tx2_uncore_pmu *tx2_pmu)
  273. {
  274. struct hw_perf_event *hwc = &event->hw;
  275. u32 cmask;
  276. tx2_pmu = pmu_to_tx2_pmu(event->pmu);
  277. cmask = tx2_pmu->counters_mask;
  278. /* counter ctrl/data reg offset at 8 */
  279. hwc->config_base = (unsigned long)tx2_pmu->base
  280. + L3C_COUNTER_CTL + (8 * GET_COUNTERID(event, cmask));
  281. hwc->event_base = (unsigned long)tx2_pmu->base
  282. + L3C_COUNTER_DATA + (8 * GET_COUNTERID(event, cmask));
  283. }
  284. static void init_cntr_base_dmc(struct perf_event *event,
  285. struct tx2_uncore_pmu *tx2_pmu)
  286. {
  287. struct hw_perf_event *hwc = &event->hw;
  288. u32 cmask;
  289. tx2_pmu = pmu_to_tx2_pmu(event->pmu);
  290. cmask = tx2_pmu->counters_mask;
  291. hwc->config_base = (unsigned long)tx2_pmu->base
  292. + DMC_COUNTER_CTL;
  293. /* counter data reg offset at 0xc */
  294. hwc->event_base = (unsigned long)tx2_pmu->base
  295. + DMC_COUNTER_DATA + (0xc * GET_COUNTERID(event, cmask));
  296. }
  297. static void init_cntr_base_ccpi2(struct perf_event *event,
  298. struct tx2_uncore_pmu *tx2_pmu)
  299. {
  300. struct hw_perf_event *hwc = &event->hw;
  301. u32 cmask;
  302. cmask = tx2_pmu->counters_mask;
  303. hwc->config_base = (unsigned long)tx2_pmu->base
  304. + CCPI2_COUNTER_CTL + (4 * GET_COUNTERID(event, cmask));
  305. hwc->event_base = (unsigned long)tx2_pmu->base;
  306. }
  307. static void uncore_start_event_l3c(struct perf_event *event, int flags)
  308. {
  309. u32 val, emask;
  310. struct hw_perf_event *hwc = &event->hw;
  311. struct tx2_uncore_pmu *tx2_pmu;
  312. tx2_pmu = pmu_to_tx2_pmu(event->pmu);
  313. emask = tx2_pmu->events_mask;
  314. /* event id encoded in bits [07:03] */
  315. val = GET_EVENTID(event, emask) << 3;
  316. reg_writel(val, hwc->config_base);
  317. local64_set(&hwc->prev_count, 0);
  318. reg_writel(0, hwc->event_base);
  319. }
  320. static inline void uncore_stop_event_l3c(struct perf_event *event)
  321. {
  322. reg_writel(0, event->hw.config_base);
  323. }
  324. static void uncore_start_event_dmc(struct perf_event *event, int flags)
  325. {
  326. u32 val, cmask, emask;
  327. struct hw_perf_event *hwc = &event->hw;
  328. struct tx2_uncore_pmu *tx2_pmu;
  329. int idx, event_id;
  330. tx2_pmu = pmu_to_tx2_pmu(event->pmu);
  331. cmask = tx2_pmu->counters_mask;
  332. emask = tx2_pmu->events_mask;
  333. idx = GET_COUNTERID(event, cmask);
  334. event_id = GET_EVENTID(event, emask);
  335. /* enable and start counters.
  336. * 8 bits for each counter, bits[05:01] of a counter to set event type.
  337. */
  338. val = reg_readl(hwc->config_base);
  339. val &= ~DMC_EVENT_CFG(idx, 0x1f);
  340. val |= DMC_EVENT_CFG(idx, event_id);
  341. reg_writel(val, hwc->config_base);
  342. local64_set(&hwc->prev_count, 0);
  343. reg_writel(0, hwc->event_base);
  344. }
  345. static void uncore_stop_event_dmc(struct perf_event *event)
  346. {
  347. u32 val, cmask;
  348. struct hw_perf_event *hwc = &event->hw;
  349. struct tx2_uncore_pmu *tx2_pmu;
  350. int idx;
  351. tx2_pmu = pmu_to_tx2_pmu(event->pmu);
  352. cmask = tx2_pmu->counters_mask;
  353. idx = GET_COUNTERID(event, cmask);
  354. /* clear event type(bits[05:01]) to stop counter */
  355. val = reg_readl(hwc->config_base);
  356. val &= ~DMC_EVENT_CFG(idx, 0x1f);
  357. reg_writel(val, hwc->config_base);
  358. }
  359. static void uncore_start_event_ccpi2(struct perf_event *event, int flags)
  360. {
  361. u32 emask;
  362. struct hw_perf_event *hwc = &event->hw;
  363. struct tx2_uncore_pmu *tx2_pmu;
  364. tx2_pmu = pmu_to_tx2_pmu(event->pmu);
  365. emask = tx2_pmu->events_mask;
  366. /* Bit [09:00] to set event id.
  367. * Bits [10], set level to rising edge.
  368. * Bits [11], set type to edge sensitive.
  369. */
  370. reg_writel((CCPI2_EVENT_TYPE_EDGE_SENSITIVE |
  371. CCPI2_EVENT_LEVEL_RISING_EDGE |
  372. GET_EVENTID(event, emask)), hwc->config_base);
  373. /* reset[4], enable[0] and start[1] counters */
  374. reg_writel(CCPI2_PERF_CTL_RESET |
  375. CCPI2_PERF_CTL_START |
  376. CCPI2_PERF_CTL_ENABLE,
  377. hwc->event_base + CCPI2_PERF_CTL);
  378. local64_set(&event->hw.prev_count, 0ULL);
  379. }
  380. static void uncore_stop_event_ccpi2(struct perf_event *event)
  381. {
  382. struct hw_perf_event *hwc = &event->hw;
  383. /* disable and stop counter */
  384. reg_writel(0, hwc->event_base + CCPI2_PERF_CTL);
  385. }
  386. static void tx2_uncore_event_update(struct perf_event *event)
  387. {
  388. u64 prev, delta, new = 0;
  389. struct hw_perf_event *hwc = &event->hw;
  390. struct tx2_uncore_pmu *tx2_pmu;
  391. enum tx2_uncore_type type;
  392. u32 prorate_factor;
  393. u32 cmask, emask;
  394. tx2_pmu = pmu_to_tx2_pmu(event->pmu);
  395. type = tx2_pmu->type;
  396. cmask = tx2_pmu->counters_mask;
  397. emask = tx2_pmu->events_mask;
  398. prorate_factor = tx2_pmu->prorate_factor;
  399. if (type == PMU_TYPE_CCPI2) {
  400. reg_writel(CCPI2_COUNTER_OFFSET +
  401. GET_COUNTERID(event, cmask),
  402. hwc->event_base + CCPI2_COUNTER_SEL);
  403. new = reg_readl(hwc->event_base + CCPI2_COUNTER_DATA_H);
  404. new = (new << 32) +
  405. reg_readl(hwc->event_base + CCPI2_COUNTER_DATA_L);
  406. prev = local64_xchg(&hwc->prev_count, new);
  407. delta = new - prev;
  408. } else {
  409. new = reg_readl(hwc->event_base);
  410. prev = local64_xchg(&hwc->prev_count, new);
  411. /* handles rollover of 32 bit counter */
  412. delta = (u32)(((1ULL << 32) - prev) + new);
  413. }
  414. /* DMC event data_transfers granularity is 16 Bytes, convert it to 64 */
  415. if (type == PMU_TYPE_DMC &&
  416. GET_EVENTID(event, emask) == DMC_EVENT_DATA_TRANSFERS)
  417. delta = delta/4;
  418. /* L3C and DMC has 16 and 8 interleave channels respectively.
  419. * The sampled value is for channel 0 and multiplied with
  420. * prorate_factor to get the count for a device.
  421. */
  422. local64_add(delta * prorate_factor, &event->count);
  423. }
  424. static enum tx2_uncore_type get_tx2_pmu_type(struct acpi_device *adev)
  425. {
  426. struct acpi_device_id devices[] = {
  427. {"CAV901D", PMU_TYPE_L3C},
  428. {"CAV901F", PMU_TYPE_DMC},
  429. {"CAV901E", PMU_TYPE_CCPI2},
  430. {}
  431. };
  432. const struct acpi_device_id *id;
  433. id = acpi_match_acpi_device(devices, adev);
  434. if (!id)
  435. return PMU_TYPE_INVALID;
  436. return (enum tx2_uncore_type)id->driver_data;
  437. }
  438. static bool tx2_uncore_validate_event(struct pmu *pmu,
  439. struct perf_event *event, int *counters)
  440. {
  441. if (is_software_event(event))
  442. return true;
  443. /* Reject groups spanning multiple HW PMUs. */
  444. if (event->pmu != pmu)
  445. return false;
  446. *counters = *counters + 1;
  447. return true;
  448. }
  449. /*
  450. * Make sure the group of events can be scheduled at once
  451. * on the PMU.
  452. */
  453. static bool tx2_uncore_validate_event_group(struct perf_event *event,
  454. int max_counters)
  455. {
  456. struct perf_event *sibling, *leader = event->group_leader;
  457. int counters = 0;
  458. if (event->group_leader == event)
  459. return true;
  460. if (!tx2_uncore_validate_event(event->pmu, leader, &counters))
  461. return false;
  462. for_each_sibling_event(sibling, leader) {
  463. if (!tx2_uncore_validate_event(event->pmu, sibling, &counters))
  464. return false;
  465. }
  466. if (!tx2_uncore_validate_event(event->pmu, event, &counters))
  467. return false;
  468. /*
  469. * If the group requires more counters than the HW has,
  470. * it cannot ever be scheduled.
  471. */
  472. return counters <= max_counters;
  473. }
  474. static int tx2_uncore_event_init(struct perf_event *event)
  475. {
  476. struct hw_perf_event *hwc = &event->hw;
  477. struct tx2_uncore_pmu *tx2_pmu;
  478. /* Test the event attr type check for PMU enumeration */
  479. if (event->attr.type != event->pmu->type)
  480. return -ENOENT;
  481. /*
  482. * SOC PMU counters are shared across all cores.
  483. * Therefore, it does not support per-process mode.
  484. * Also, it does not support event sampling mode.
  485. */
  486. if (is_sampling_event(event) || event->attach_state & PERF_ATTACH_TASK)
  487. return -EINVAL;
  488. if (event->cpu < 0)
  489. return -EINVAL;
  490. tx2_pmu = pmu_to_tx2_pmu(event->pmu);
  491. if (tx2_pmu->cpu >= nr_cpu_ids)
  492. return -EINVAL;
  493. event->cpu = tx2_pmu->cpu;
  494. if (event->attr.config >= tx2_pmu->max_events)
  495. return -EINVAL;
  496. /* store event id */
  497. hwc->config = event->attr.config;
  498. /* Validate the group */
  499. if (!tx2_uncore_validate_event_group(event, tx2_pmu->max_counters))
  500. return -EINVAL;
  501. return 0;
  502. }
  503. static void tx2_uncore_event_start(struct perf_event *event, int flags)
  504. {
  505. struct hw_perf_event *hwc = &event->hw;
  506. struct tx2_uncore_pmu *tx2_pmu;
  507. hwc->state = 0;
  508. tx2_pmu = pmu_to_tx2_pmu(event->pmu);
  509. tx2_pmu->start_event(event, flags);
  510. perf_event_update_userpage(event);
  511. /* No hrtimer needed for CCPI2, 64-bit counters */
  512. if (!tx2_pmu->hrtimer_callback)
  513. return;
  514. /* Start timer for first event */
  515. if (bitmap_weight(tx2_pmu->active_counters,
  516. tx2_pmu->max_counters) == 1) {
  517. hrtimer_start(&tx2_pmu->hrtimer,
  518. ns_to_ktime(tx2_pmu->hrtimer_interval),
  519. HRTIMER_MODE_REL_PINNED);
  520. }
  521. }
  522. static void tx2_uncore_event_stop(struct perf_event *event, int flags)
  523. {
  524. struct hw_perf_event *hwc = &event->hw;
  525. struct tx2_uncore_pmu *tx2_pmu;
  526. if (hwc->state & PERF_HES_UPTODATE)
  527. return;
  528. tx2_pmu = pmu_to_tx2_pmu(event->pmu);
  529. tx2_pmu->stop_event(event);
  530. WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
  531. hwc->state |= PERF_HES_STOPPED;
  532. if (flags & PERF_EF_UPDATE) {
  533. tx2_uncore_event_update(event);
  534. hwc->state |= PERF_HES_UPTODATE;
  535. }
  536. }
  537. static int tx2_uncore_event_add(struct perf_event *event, int flags)
  538. {
  539. struct hw_perf_event *hwc = &event->hw;
  540. struct tx2_uncore_pmu *tx2_pmu;
  541. tx2_pmu = pmu_to_tx2_pmu(event->pmu);
  542. /* Allocate a free counter */
  543. hwc->idx = alloc_counter(tx2_pmu);
  544. if (hwc->idx < 0)
  545. return -EAGAIN;
  546. tx2_pmu->events[hwc->idx] = event;
  547. /* set counter control and data registers base address */
  548. tx2_pmu->init_cntr_base(event, tx2_pmu);
  549. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  550. if (flags & PERF_EF_START)
  551. tx2_uncore_event_start(event, flags);
  552. return 0;
  553. }
  554. static void tx2_uncore_event_del(struct perf_event *event, int flags)
  555. {
  556. struct tx2_uncore_pmu *tx2_pmu = pmu_to_tx2_pmu(event->pmu);
  557. struct hw_perf_event *hwc = &event->hw;
  558. u32 cmask;
  559. cmask = tx2_pmu->counters_mask;
  560. tx2_uncore_event_stop(event, PERF_EF_UPDATE);
  561. /* clear the assigned counter */
  562. free_counter(tx2_pmu, GET_COUNTERID(event, cmask));
  563. perf_event_update_userpage(event);
  564. tx2_pmu->events[hwc->idx] = NULL;
  565. hwc->idx = -1;
  566. if (!tx2_pmu->hrtimer_callback)
  567. return;
  568. if (bitmap_empty(tx2_pmu->active_counters, tx2_pmu->max_counters))
  569. hrtimer_cancel(&tx2_pmu->hrtimer);
  570. }
  571. static void tx2_uncore_event_read(struct perf_event *event)
  572. {
  573. tx2_uncore_event_update(event);
  574. }
  575. static enum hrtimer_restart tx2_hrtimer_callback(struct hrtimer *timer)
  576. {
  577. struct tx2_uncore_pmu *tx2_pmu;
  578. int max_counters, idx;
  579. tx2_pmu = container_of(timer, struct tx2_uncore_pmu, hrtimer);
  580. max_counters = tx2_pmu->max_counters;
  581. if (bitmap_empty(tx2_pmu->active_counters, max_counters))
  582. return HRTIMER_NORESTART;
  583. for_each_set_bit(idx, tx2_pmu->active_counters, max_counters) {
  584. struct perf_event *event = tx2_pmu->events[idx];
  585. tx2_uncore_event_update(event);
  586. }
  587. hrtimer_forward_now(timer, ns_to_ktime(tx2_pmu->hrtimer_interval));
  588. return HRTIMER_RESTART;
  589. }
  590. static int tx2_uncore_pmu_register(
  591. struct tx2_uncore_pmu *tx2_pmu)
  592. {
  593. struct device *dev = tx2_pmu->dev;
  594. char *name = tx2_pmu->name;
  595. /* Perf event registration */
  596. tx2_pmu->pmu = (struct pmu) {
  597. .module = THIS_MODULE,
  598. .parent = tx2_pmu->dev,
  599. .attr_groups = tx2_pmu->attr_groups,
  600. .task_ctx_nr = perf_invalid_context,
  601. .event_init = tx2_uncore_event_init,
  602. .add = tx2_uncore_event_add,
  603. .del = tx2_uncore_event_del,
  604. .start = tx2_uncore_event_start,
  605. .stop = tx2_uncore_event_stop,
  606. .read = tx2_uncore_event_read,
  607. .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
  608. };
  609. tx2_pmu->pmu.name = devm_kasprintf(dev, GFP_KERNEL,
  610. "%s", name);
  611. return perf_pmu_register(&tx2_pmu->pmu, tx2_pmu->pmu.name, -1);
  612. }
  613. static int tx2_uncore_pmu_add_dev(struct tx2_uncore_pmu *tx2_pmu)
  614. {
  615. int ret, cpu;
  616. cpu = cpumask_any_and(cpumask_of_node(tx2_pmu->node),
  617. cpu_online_mask);
  618. tx2_pmu->cpu = cpu;
  619. if (tx2_pmu->hrtimer_callback) {
  620. hrtimer_init(&tx2_pmu->hrtimer,
  621. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  622. tx2_pmu->hrtimer.function = tx2_pmu->hrtimer_callback;
  623. }
  624. ret = tx2_uncore_pmu_register(tx2_pmu);
  625. if (ret) {
  626. dev_err(tx2_pmu->dev, "%s PMU: Failed to init driver\n",
  627. tx2_pmu->name);
  628. return -ENODEV;
  629. }
  630. /* register hotplug callback for the pmu */
  631. ret = cpuhp_state_add_instance(
  632. CPUHP_AP_PERF_ARM_CAVIUM_TX2_UNCORE_ONLINE,
  633. &tx2_pmu->hpnode);
  634. if (ret) {
  635. dev_err(tx2_pmu->dev, "Error %d registering hotplug", ret);
  636. return ret;
  637. }
  638. /* Add to list */
  639. list_add(&tx2_pmu->entry, &tx2_pmus);
  640. dev_dbg(tx2_pmu->dev, "%s PMU UNCORE registered\n",
  641. tx2_pmu->pmu.name);
  642. return ret;
  643. }
  644. static struct tx2_uncore_pmu *tx2_uncore_pmu_init_dev(struct device *dev,
  645. acpi_handle handle, struct acpi_device *adev, u32 type)
  646. {
  647. struct tx2_uncore_pmu *tx2_pmu;
  648. void __iomem *base;
  649. struct resource res;
  650. struct resource_entry *rentry;
  651. struct list_head list;
  652. int ret;
  653. INIT_LIST_HEAD(&list);
  654. ret = acpi_dev_get_resources(adev, &list, NULL, NULL);
  655. if (ret <= 0) {
  656. dev_err(dev, "failed to parse _CRS method, error %d\n", ret);
  657. return NULL;
  658. }
  659. list_for_each_entry(rentry, &list, node) {
  660. if (resource_type(rentry->res) == IORESOURCE_MEM) {
  661. res = *rentry->res;
  662. rentry = NULL;
  663. break;
  664. }
  665. }
  666. acpi_dev_free_resource_list(&list);
  667. if (rentry) {
  668. dev_err(dev, "PMU type %d: Fail to find resource\n", type);
  669. return NULL;
  670. }
  671. base = devm_ioremap_resource(dev, &res);
  672. if (IS_ERR(base))
  673. return NULL;
  674. tx2_pmu = devm_kzalloc(dev, sizeof(*tx2_pmu), GFP_KERNEL);
  675. if (!tx2_pmu)
  676. return NULL;
  677. tx2_pmu->dev = dev;
  678. tx2_pmu->type = type;
  679. tx2_pmu->base = base;
  680. tx2_pmu->node = dev_to_node(dev);
  681. INIT_LIST_HEAD(&tx2_pmu->entry);
  682. switch (tx2_pmu->type) {
  683. case PMU_TYPE_L3C:
  684. tx2_pmu->max_counters = TX2_PMU_DMC_L3C_MAX_COUNTERS;
  685. tx2_pmu->counters_mask = 0x3;
  686. tx2_pmu->prorate_factor = TX2_PMU_L3_TILES;
  687. tx2_pmu->max_events = L3_EVENT_MAX;
  688. tx2_pmu->events_mask = 0x1f;
  689. tx2_pmu->hrtimer_interval = TX2_PMU_HRTIMER_INTERVAL;
  690. tx2_pmu->hrtimer_callback = tx2_hrtimer_callback;
  691. tx2_pmu->attr_groups = l3c_pmu_attr_groups;
  692. tx2_pmu->name = devm_kasprintf(dev, GFP_KERNEL,
  693. "uncore_l3c_%d", tx2_pmu->node);
  694. tx2_pmu->init_cntr_base = init_cntr_base_l3c;
  695. tx2_pmu->start_event = uncore_start_event_l3c;
  696. tx2_pmu->stop_event = uncore_stop_event_l3c;
  697. break;
  698. case PMU_TYPE_DMC:
  699. tx2_pmu->max_counters = TX2_PMU_DMC_L3C_MAX_COUNTERS;
  700. tx2_pmu->counters_mask = 0x3;
  701. tx2_pmu->prorate_factor = TX2_PMU_DMC_CHANNELS;
  702. tx2_pmu->max_events = DMC_EVENT_MAX;
  703. tx2_pmu->events_mask = 0x1f;
  704. tx2_pmu->hrtimer_interval = TX2_PMU_HRTIMER_INTERVAL;
  705. tx2_pmu->hrtimer_callback = tx2_hrtimer_callback;
  706. tx2_pmu->attr_groups = dmc_pmu_attr_groups;
  707. tx2_pmu->name = devm_kasprintf(dev, GFP_KERNEL,
  708. "uncore_dmc_%d", tx2_pmu->node);
  709. tx2_pmu->init_cntr_base = init_cntr_base_dmc;
  710. tx2_pmu->start_event = uncore_start_event_dmc;
  711. tx2_pmu->stop_event = uncore_stop_event_dmc;
  712. break;
  713. case PMU_TYPE_CCPI2:
  714. /* CCPI2 has 8 counters */
  715. tx2_pmu->max_counters = TX2_PMU_CCPI2_MAX_COUNTERS;
  716. tx2_pmu->counters_mask = 0x7;
  717. tx2_pmu->prorate_factor = 1;
  718. tx2_pmu->max_events = CCPI2_EVENT_MAX;
  719. tx2_pmu->events_mask = 0x1ff;
  720. tx2_pmu->attr_groups = ccpi2_pmu_attr_groups;
  721. tx2_pmu->name = devm_kasprintf(dev, GFP_KERNEL,
  722. "uncore_ccpi2_%d", tx2_pmu->node);
  723. tx2_pmu->init_cntr_base = init_cntr_base_ccpi2;
  724. tx2_pmu->start_event = uncore_start_event_ccpi2;
  725. tx2_pmu->stop_event = uncore_stop_event_ccpi2;
  726. tx2_pmu->hrtimer_callback = NULL;
  727. break;
  728. case PMU_TYPE_INVALID:
  729. devm_kfree(dev, tx2_pmu);
  730. return NULL;
  731. }
  732. return tx2_pmu;
  733. }
  734. static acpi_status tx2_uncore_pmu_add(acpi_handle handle, u32 level,
  735. void *data, void **return_value)
  736. {
  737. struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
  738. struct tx2_uncore_pmu *tx2_pmu;
  739. enum tx2_uncore_type type;
  740. if (!adev || acpi_bus_get_status(adev) || !adev->status.present)
  741. return AE_OK;
  742. type = get_tx2_pmu_type(adev);
  743. if (type == PMU_TYPE_INVALID)
  744. return AE_OK;
  745. tx2_pmu = tx2_uncore_pmu_init_dev((struct device *)data,
  746. handle, adev, type);
  747. if (!tx2_pmu)
  748. return AE_ERROR;
  749. if (tx2_uncore_pmu_add_dev(tx2_pmu)) {
  750. /* Can't add the PMU device, abort */
  751. return AE_ERROR;
  752. }
  753. return AE_OK;
  754. }
  755. static int tx2_uncore_pmu_online_cpu(unsigned int cpu,
  756. struct hlist_node *hpnode)
  757. {
  758. struct tx2_uncore_pmu *tx2_pmu;
  759. tx2_pmu = hlist_entry_safe(hpnode,
  760. struct tx2_uncore_pmu, hpnode);
  761. /* Pick this CPU, If there is no CPU/PMU association and both are
  762. * from same node.
  763. */
  764. if ((tx2_pmu->cpu >= nr_cpu_ids) &&
  765. (tx2_pmu->node == cpu_to_node(cpu)))
  766. tx2_pmu->cpu = cpu;
  767. return 0;
  768. }
  769. static int tx2_uncore_pmu_offline_cpu(unsigned int cpu,
  770. struct hlist_node *hpnode)
  771. {
  772. struct tx2_uncore_pmu *tx2_pmu;
  773. unsigned int new_cpu;
  774. tx2_pmu = hlist_entry_safe(hpnode,
  775. struct tx2_uncore_pmu, hpnode);
  776. if (cpu != tx2_pmu->cpu)
  777. return 0;
  778. if (tx2_pmu->hrtimer_callback)
  779. hrtimer_cancel(&tx2_pmu->hrtimer);
  780. new_cpu = cpumask_any_and_but(cpumask_of_node(tx2_pmu->node),
  781. cpu_online_mask, cpu);
  782. tx2_pmu->cpu = new_cpu;
  783. if (new_cpu >= nr_cpu_ids)
  784. return 0;
  785. perf_pmu_migrate_context(&tx2_pmu->pmu, cpu, new_cpu);
  786. return 0;
  787. }
  788. static const struct acpi_device_id tx2_uncore_acpi_match[] = {
  789. {"CAV901C", 0},
  790. {},
  791. };
  792. MODULE_DEVICE_TABLE(acpi, tx2_uncore_acpi_match);
  793. static int tx2_uncore_probe(struct platform_device *pdev)
  794. {
  795. struct device *dev = &pdev->dev;
  796. acpi_handle handle;
  797. acpi_status status;
  798. set_dev_node(dev, acpi_get_node(ACPI_HANDLE(dev)));
  799. if (!has_acpi_companion(dev))
  800. return -ENODEV;
  801. handle = ACPI_HANDLE(dev);
  802. if (!handle)
  803. return -EINVAL;
  804. /* Walk through the tree for all PMU UNCORE devices */
  805. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  806. tx2_uncore_pmu_add,
  807. NULL, dev, NULL);
  808. if (ACPI_FAILURE(status)) {
  809. dev_err(dev, "failed to probe PMU devices\n");
  810. return_ACPI_STATUS(status);
  811. }
  812. dev_info(dev, "node%d: pmu uncore registered\n", dev_to_node(dev));
  813. return 0;
  814. }
  815. static void tx2_uncore_remove(struct platform_device *pdev)
  816. {
  817. struct tx2_uncore_pmu *tx2_pmu, *temp;
  818. struct device *dev = &pdev->dev;
  819. if (!list_empty(&tx2_pmus)) {
  820. list_for_each_entry_safe(tx2_pmu, temp, &tx2_pmus, entry) {
  821. if (tx2_pmu->node == dev_to_node(dev)) {
  822. cpuhp_state_remove_instance_nocalls(
  823. CPUHP_AP_PERF_ARM_CAVIUM_TX2_UNCORE_ONLINE,
  824. &tx2_pmu->hpnode);
  825. perf_pmu_unregister(&tx2_pmu->pmu);
  826. list_del(&tx2_pmu->entry);
  827. }
  828. }
  829. }
  830. }
  831. static struct platform_driver tx2_uncore_driver = {
  832. .driver = {
  833. .name = "tx2-uncore-pmu",
  834. .acpi_match_table = ACPI_PTR(tx2_uncore_acpi_match),
  835. .suppress_bind_attrs = true,
  836. },
  837. .probe = tx2_uncore_probe,
  838. .remove_new = tx2_uncore_remove,
  839. };
  840. static int __init tx2_uncore_driver_init(void)
  841. {
  842. int ret;
  843. ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_CAVIUM_TX2_UNCORE_ONLINE,
  844. "perf/tx2/uncore:online",
  845. tx2_uncore_pmu_online_cpu,
  846. tx2_uncore_pmu_offline_cpu);
  847. if (ret) {
  848. pr_err("TX2 PMU: setup hotplug failed(%d)\n", ret);
  849. return ret;
  850. }
  851. ret = platform_driver_register(&tx2_uncore_driver);
  852. if (ret)
  853. cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CAVIUM_TX2_UNCORE_ONLINE);
  854. return ret;
  855. }
  856. module_init(tx2_uncore_driver_init);
  857. static void __exit tx2_uncore_driver_exit(void)
  858. {
  859. platform_driver_unregister(&tx2_uncore_driver);
  860. cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CAVIUM_TX2_UNCORE_ONLINE);
  861. }
  862. module_exit(tx2_uncore_driver_exit);
  863. MODULE_DESCRIPTION("ThunderX2 UNCORE PMU driver");
  864. MODULE_LICENSE("GPL v2");
  865. MODULE_AUTHOR("Ganapatrao Kulkarni <gkulkarni@cavium.com>");