thunderx2_pmu.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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_setup(&tx2_pmu->hrtimer, tx2_pmu->hrtimer_callback, CLOCK_MONOTONIC,
  621. HRTIMER_MODE_REL);
  622. }
  623. ret = tx2_uncore_pmu_register(tx2_pmu);
  624. if (ret) {
  625. dev_err(tx2_pmu->dev, "%s PMU: Failed to init driver\n",
  626. tx2_pmu->name);
  627. return -ENODEV;
  628. }
  629. /* register hotplug callback for the pmu */
  630. ret = cpuhp_state_add_instance(
  631. CPUHP_AP_PERF_ARM_CAVIUM_TX2_UNCORE_ONLINE,
  632. &tx2_pmu->hpnode);
  633. if (ret) {
  634. dev_err(tx2_pmu->dev, "Error %d registering hotplug", ret);
  635. return ret;
  636. }
  637. /* Add to list */
  638. list_add(&tx2_pmu->entry, &tx2_pmus);
  639. dev_dbg(tx2_pmu->dev, "%s PMU UNCORE registered\n",
  640. tx2_pmu->pmu.name);
  641. return ret;
  642. }
  643. static struct tx2_uncore_pmu *tx2_uncore_pmu_init_dev(struct device *dev,
  644. acpi_handle handle, struct acpi_device *adev, u32 type)
  645. {
  646. struct tx2_uncore_pmu *tx2_pmu;
  647. void __iomem *base;
  648. struct resource res;
  649. struct resource_entry *rentry;
  650. struct list_head list;
  651. int ret;
  652. INIT_LIST_HEAD(&list);
  653. ret = acpi_dev_get_resources(adev, &list, NULL, NULL);
  654. if (ret <= 0) {
  655. dev_err(dev, "failed to parse _CRS method, error %d\n", ret);
  656. return NULL;
  657. }
  658. list_for_each_entry(rentry, &list, node) {
  659. if (resource_type(rentry->res) == IORESOURCE_MEM) {
  660. res = *rentry->res;
  661. rentry = NULL;
  662. break;
  663. }
  664. }
  665. acpi_dev_free_resource_list(&list);
  666. if (rentry) {
  667. dev_err(dev, "PMU type %d: Fail to find resource\n", type);
  668. return NULL;
  669. }
  670. base = devm_ioremap_resource(dev, &res);
  671. if (IS_ERR(base))
  672. return NULL;
  673. tx2_pmu = devm_kzalloc(dev, sizeof(*tx2_pmu), GFP_KERNEL);
  674. if (!tx2_pmu)
  675. return NULL;
  676. tx2_pmu->dev = dev;
  677. tx2_pmu->type = type;
  678. tx2_pmu->base = base;
  679. tx2_pmu->node = dev_to_node(dev);
  680. INIT_LIST_HEAD(&tx2_pmu->entry);
  681. switch (tx2_pmu->type) {
  682. case PMU_TYPE_L3C:
  683. tx2_pmu->max_counters = TX2_PMU_DMC_L3C_MAX_COUNTERS;
  684. tx2_pmu->counters_mask = 0x3;
  685. tx2_pmu->prorate_factor = TX2_PMU_L3_TILES;
  686. tx2_pmu->max_events = L3_EVENT_MAX;
  687. tx2_pmu->events_mask = 0x1f;
  688. tx2_pmu->hrtimer_interval = TX2_PMU_HRTIMER_INTERVAL;
  689. tx2_pmu->hrtimer_callback = tx2_hrtimer_callback;
  690. tx2_pmu->attr_groups = l3c_pmu_attr_groups;
  691. tx2_pmu->name = devm_kasprintf(dev, GFP_KERNEL,
  692. "uncore_l3c_%d", tx2_pmu->node);
  693. tx2_pmu->init_cntr_base = init_cntr_base_l3c;
  694. tx2_pmu->start_event = uncore_start_event_l3c;
  695. tx2_pmu->stop_event = uncore_stop_event_l3c;
  696. break;
  697. case PMU_TYPE_DMC:
  698. tx2_pmu->max_counters = TX2_PMU_DMC_L3C_MAX_COUNTERS;
  699. tx2_pmu->counters_mask = 0x3;
  700. tx2_pmu->prorate_factor = TX2_PMU_DMC_CHANNELS;
  701. tx2_pmu->max_events = DMC_EVENT_MAX;
  702. tx2_pmu->events_mask = 0x1f;
  703. tx2_pmu->hrtimer_interval = TX2_PMU_HRTIMER_INTERVAL;
  704. tx2_pmu->hrtimer_callback = tx2_hrtimer_callback;
  705. tx2_pmu->attr_groups = dmc_pmu_attr_groups;
  706. tx2_pmu->name = devm_kasprintf(dev, GFP_KERNEL,
  707. "uncore_dmc_%d", tx2_pmu->node);
  708. tx2_pmu->init_cntr_base = init_cntr_base_dmc;
  709. tx2_pmu->start_event = uncore_start_event_dmc;
  710. tx2_pmu->stop_event = uncore_stop_event_dmc;
  711. break;
  712. case PMU_TYPE_CCPI2:
  713. /* CCPI2 has 8 counters */
  714. tx2_pmu->max_counters = TX2_PMU_CCPI2_MAX_COUNTERS;
  715. tx2_pmu->counters_mask = 0x7;
  716. tx2_pmu->prorate_factor = 1;
  717. tx2_pmu->max_events = CCPI2_EVENT_MAX;
  718. tx2_pmu->events_mask = 0x1ff;
  719. tx2_pmu->attr_groups = ccpi2_pmu_attr_groups;
  720. tx2_pmu->name = devm_kasprintf(dev, GFP_KERNEL,
  721. "uncore_ccpi2_%d", tx2_pmu->node);
  722. tx2_pmu->init_cntr_base = init_cntr_base_ccpi2;
  723. tx2_pmu->start_event = uncore_start_event_ccpi2;
  724. tx2_pmu->stop_event = uncore_stop_event_ccpi2;
  725. tx2_pmu->hrtimer_callback = NULL;
  726. break;
  727. case PMU_TYPE_INVALID:
  728. devm_kfree(dev, tx2_pmu);
  729. return NULL;
  730. }
  731. return tx2_pmu;
  732. }
  733. static acpi_status tx2_uncore_pmu_add(acpi_handle handle, u32 level,
  734. void *data, void **return_value)
  735. {
  736. struct acpi_device *adev = acpi_fetch_acpi_dev(handle);
  737. struct tx2_uncore_pmu *tx2_pmu;
  738. enum tx2_uncore_type type;
  739. if (!adev || acpi_bus_get_status(adev) || !adev->status.present)
  740. return AE_OK;
  741. type = get_tx2_pmu_type(adev);
  742. if (type == PMU_TYPE_INVALID)
  743. return AE_OK;
  744. tx2_pmu = tx2_uncore_pmu_init_dev((struct device *)data,
  745. handle, adev, type);
  746. if (!tx2_pmu)
  747. return AE_ERROR;
  748. if (tx2_uncore_pmu_add_dev(tx2_pmu)) {
  749. /* Can't add the PMU device, abort */
  750. return AE_ERROR;
  751. }
  752. return AE_OK;
  753. }
  754. static int tx2_uncore_pmu_online_cpu(unsigned int cpu,
  755. struct hlist_node *hpnode)
  756. {
  757. struct tx2_uncore_pmu *tx2_pmu;
  758. tx2_pmu = hlist_entry_safe(hpnode,
  759. struct tx2_uncore_pmu, hpnode);
  760. /* Pick this CPU, If there is no CPU/PMU association and both are
  761. * from same node.
  762. */
  763. if ((tx2_pmu->cpu >= nr_cpu_ids) &&
  764. (tx2_pmu->node == cpu_to_node(cpu)))
  765. tx2_pmu->cpu = cpu;
  766. return 0;
  767. }
  768. static int tx2_uncore_pmu_offline_cpu(unsigned int cpu,
  769. struct hlist_node *hpnode)
  770. {
  771. struct tx2_uncore_pmu *tx2_pmu;
  772. unsigned int new_cpu;
  773. tx2_pmu = hlist_entry_safe(hpnode,
  774. struct tx2_uncore_pmu, hpnode);
  775. if (cpu != tx2_pmu->cpu)
  776. return 0;
  777. if (tx2_pmu->hrtimer_callback)
  778. hrtimer_cancel(&tx2_pmu->hrtimer);
  779. new_cpu = cpumask_any_and_but(cpumask_of_node(tx2_pmu->node),
  780. cpu_online_mask, cpu);
  781. tx2_pmu->cpu = new_cpu;
  782. if (new_cpu >= nr_cpu_ids)
  783. return 0;
  784. perf_pmu_migrate_context(&tx2_pmu->pmu, cpu, new_cpu);
  785. return 0;
  786. }
  787. static const struct acpi_device_id tx2_uncore_acpi_match[] = {
  788. {"CAV901C", 0},
  789. {},
  790. };
  791. MODULE_DEVICE_TABLE(acpi, tx2_uncore_acpi_match);
  792. static int tx2_uncore_probe(struct platform_device *pdev)
  793. {
  794. struct device *dev = &pdev->dev;
  795. acpi_handle handle;
  796. acpi_status status;
  797. set_dev_node(dev, acpi_get_node(ACPI_HANDLE(dev)));
  798. if (!has_acpi_companion(dev))
  799. return -ENODEV;
  800. handle = ACPI_HANDLE(dev);
  801. if (!handle)
  802. return -EINVAL;
  803. /* Walk through the tree for all PMU UNCORE devices */
  804. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  805. tx2_uncore_pmu_add,
  806. NULL, dev, NULL);
  807. if (ACPI_FAILURE(status)) {
  808. dev_err(dev, "failed to probe PMU devices\n");
  809. return_ACPI_STATUS(status);
  810. }
  811. dev_info(dev, "node%d: pmu uncore registered\n", dev_to_node(dev));
  812. return 0;
  813. }
  814. static void tx2_uncore_remove(struct platform_device *pdev)
  815. {
  816. struct tx2_uncore_pmu *tx2_pmu, *temp;
  817. struct device *dev = &pdev->dev;
  818. if (!list_empty(&tx2_pmus)) {
  819. list_for_each_entry_safe(tx2_pmu, temp, &tx2_pmus, entry) {
  820. if (tx2_pmu->node == dev_to_node(dev)) {
  821. cpuhp_state_remove_instance_nocalls(
  822. CPUHP_AP_PERF_ARM_CAVIUM_TX2_UNCORE_ONLINE,
  823. &tx2_pmu->hpnode);
  824. perf_pmu_unregister(&tx2_pmu->pmu);
  825. list_del(&tx2_pmu->entry);
  826. }
  827. }
  828. }
  829. }
  830. static struct platform_driver tx2_uncore_driver = {
  831. .driver = {
  832. .name = "tx2-uncore-pmu",
  833. .acpi_match_table = ACPI_PTR(tx2_uncore_acpi_match),
  834. .suppress_bind_attrs = true,
  835. },
  836. .probe = tx2_uncore_probe,
  837. .remove = tx2_uncore_remove,
  838. };
  839. static int __init tx2_uncore_driver_init(void)
  840. {
  841. int ret;
  842. ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_CAVIUM_TX2_UNCORE_ONLINE,
  843. "perf/tx2/uncore:online",
  844. tx2_uncore_pmu_online_cpu,
  845. tx2_uncore_pmu_offline_cpu);
  846. if (ret) {
  847. pr_err("TX2 PMU: setup hotplug failed(%d)\n", ret);
  848. return ret;
  849. }
  850. ret = platform_driver_register(&tx2_uncore_driver);
  851. if (ret)
  852. cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CAVIUM_TX2_UNCORE_ONLINE);
  853. return ret;
  854. }
  855. module_init(tx2_uncore_driver_init);
  856. static void __exit tx2_uncore_driver_exit(void)
  857. {
  858. platform_driver_unregister(&tx2_uncore_driver);
  859. cpuhp_remove_multi_state(CPUHP_AP_PERF_ARM_CAVIUM_TX2_UNCORE_ONLINE);
  860. }
  861. module_exit(tx2_uncore_driver_exit);
  862. MODULE_DESCRIPTION("ThunderX2 UNCORE PMU driver");
  863. MODULE_LICENSE("GPL v2");
  864. MODULE_AUTHOR("Ganapatrao Kulkarni <gkulkarni@cavium.com>");