perf_cpum_cf.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Performance event support for s390x - CPU-measurement Counter Facility
  4. *
  5. * Copyright IBM Corp. 2012, 2017
  6. * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
  7. */
  8. #define KMSG_COMPONENT "cpum_cf"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/kernel_stat.h>
  12. #include <linux/perf_event.h>
  13. #include <linux/percpu.h>
  14. #include <linux/notifier.h>
  15. #include <linux/init.h>
  16. #include <linux/export.h>
  17. #include <asm/ctl_reg.h>
  18. #include <asm/irq.h>
  19. #include <asm/cpu_mf.h>
  20. enum cpumf_ctr_set {
  21. CPUMF_CTR_SET_BASIC = 0, /* Basic Counter Set */
  22. CPUMF_CTR_SET_USER = 1, /* Problem-State Counter Set */
  23. CPUMF_CTR_SET_CRYPTO = 2, /* Crypto-Activity Counter Set */
  24. CPUMF_CTR_SET_EXT = 3, /* Extended Counter Set */
  25. CPUMF_CTR_SET_MT_DIAG = 4, /* MT-diagnostic Counter Set */
  26. /* Maximum number of counter sets */
  27. CPUMF_CTR_SET_MAX,
  28. };
  29. #define CPUMF_LCCTL_ENABLE_SHIFT 16
  30. #define CPUMF_LCCTL_ACTCTL_SHIFT 0
  31. static const u64 cpumf_state_ctl[CPUMF_CTR_SET_MAX] = {
  32. [CPUMF_CTR_SET_BASIC] = 0x02,
  33. [CPUMF_CTR_SET_USER] = 0x04,
  34. [CPUMF_CTR_SET_CRYPTO] = 0x08,
  35. [CPUMF_CTR_SET_EXT] = 0x01,
  36. [CPUMF_CTR_SET_MT_DIAG] = 0x20,
  37. };
  38. static void ctr_set_enable(u64 *state, int ctr_set)
  39. {
  40. *state |= cpumf_state_ctl[ctr_set] << CPUMF_LCCTL_ENABLE_SHIFT;
  41. }
  42. static void ctr_set_disable(u64 *state, int ctr_set)
  43. {
  44. *state &= ~(cpumf_state_ctl[ctr_set] << CPUMF_LCCTL_ENABLE_SHIFT);
  45. }
  46. static void ctr_set_start(u64 *state, int ctr_set)
  47. {
  48. *state |= cpumf_state_ctl[ctr_set] << CPUMF_LCCTL_ACTCTL_SHIFT;
  49. }
  50. static void ctr_set_stop(u64 *state, int ctr_set)
  51. {
  52. *state &= ~(cpumf_state_ctl[ctr_set] << CPUMF_LCCTL_ACTCTL_SHIFT);
  53. }
  54. /* Local CPUMF event structure */
  55. struct cpu_hw_events {
  56. struct cpumf_ctr_info info;
  57. atomic_t ctr_set[CPUMF_CTR_SET_MAX];
  58. u64 state, tx_state;
  59. unsigned int flags;
  60. unsigned int txn_flags;
  61. };
  62. static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
  63. .ctr_set = {
  64. [CPUMF_CTR_SET_BASIC] = ATOMIC_INIT(0),
  65. [CPUMF_CTR_SET_USER] = ATOMIC_INIT(0),
  66. [CPUMF_CTR_SET_CRYPTO] = ATOMIC_INIT(0),
  67. [CPUMF_CTR_SET_EXT] = ATOMIC_INIT(0),
  68. [CPUMF_CTR_SET_MT_DIAG] = ATOMIC_INIT(0),
  69. },
  70. .state = 0,
  71. .flags = 0,
  72. .txn_flags = 0,
  73. };
  74. static enum cpumf_ctr_set get_counter_set(u64 event)
  75. {
  76. int set = CPUMF_CTR_SET_MAX;
  77. if (event < 32)
  78. set = CPUMF_CTR_SET_BASIC;
  79. else if (event < 64)
  80. set = CPUMF_CTR_SET_USER;
  81. else if (event < 128)
  82. set = CPUMF_CTR_SET_CRYPTO;
  83. else if (event < 256)
  84. set = CPUMF_CTR_SET_EXT;
  85. else if (event >= 448 && event < 496)
  86. set = CPUMF_CTR_SET_MT_DIAG;
  87. return set;
  88. }
  89. static int validate_ctr_version(const struct hw_perf_event *hwc)
  90. {
  91. struct cpu_hw_events *cpuhw;
  92. int err = 0;
  93. u16 mtdiag_ctl;
  94. cpuhw = &get_cpu_var(cpu_hw_events);
  95. /* check required version for counter sets */
  96. switch (hwc->config_base) {
  97. case CPUMF_CTR_SET_BASIC:
  98. case CPUMF_CTR_SET_USER:
  99. if (cpuhw->info.cfvn < 1)
  100. err = -EOPNOTSUPP;
  101. break;
  102. case CPUMF_CTR_SET_CRYPTO:
  103. case CPUMF_CTR_SET_EXT:
  104. if (cpuhw->info.csvn < 1)
  105. err = -EOPNOTSUPP;
  106. if ((cpuhw->info.csvn == 1 && hwc->config > 159) ||
  107. (cpuhw->info.csvn == 2 && hwc->config > 175) ||
  108. (cpuhw->info.csvn > 2 && hwc->config > 255))
  109. err = -EOPNOTSUPP;
  110. break;
  111. case CPUMF_CTR_SET_MT_DIAG:
  112. if (cpuhw->info.csvn <= 3)
  113. err = -EOPNOTSUPP;
  114. /*
  115. * MT-diagnostic counters are read-only. The counter set
  116. * is automatically enabled and activated on all CPUs with
  117. * multithreading (SMT). Deactivation of multithreading
  118. * also disables the counter set. State changes are ignored
  119. * by lcctl(). Because Linux controls SMT enablement through
  120. * a kernel parameter only, the counter set is either disabled
  121. * or enabled and active.
  122. *
  123. * Thus, the counters can only be used if SMT is on and the
  124. * counter set is enabled and active.
  125. */
  126. mtdiag_ctl = cpumf_state_ctl[CPUMF_CTR_SET_MT_DIAG];
  127. if (!((cpuhw->info.auth_ctl & mtdiag_ctl) &&
  128. (cpuhw->info.enable_ctl & mtdiag_ctl) &&
  129. (cpuhw->info.act_ctl & mtdiag_ctl)))
  130. err = -EOPNOTSUPP;
  131. break;
  132. }
  133. put_cpu_var(cpu_hw_events);
  134. return err;
  135. }
  136. static int validate_ctr_auth(const struct hw_perf_event *hwc)
  137. {
  138. struct cpu_hw_events *cpuhw;
  139. u64 ctrs_state;
  140. int err = 0;
  141. cpuhw = &get_cpu_var(cpu_hw_events);
  142. /* Check authorization for cpu counter sets.
  143. * If the particular CPU counter set is not authorized,
  144. * return with -ENOENT in order to fall back to other
  145. * PMUs that might suffice the event request.
  146. */
  147. ctrs_state = cpumf_state_ctl[hwc->config_base];
  148. if (!(ctrs_state & cpuhw->info.auth_ctl))
  149. err = -ENOENT;
  150. put_cpu_var(cpu_hw_events);
  151. return err;
  152. }
  153. /*
  154. * Change the CPUMF state to active.
  155. * Enable and activate the CPU-counter sets according
  156. * to the per-cpu control state.
  157. */
  158. static void cpumf_pmu_enable(struct pmu *pmu)
  159. {
  160. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  161. int err;
  162. if (cpuhw->flags & PMU_F_ENABLED)
  163. return;
  164. err = lcctl(cpuhw->state);
  165. if (err) {
  166. pr_err("Enabling the performance measuring unit "
  167. "failed with rc=%x\n", err);
  168. return;
  169. }
  170. cpuhw->flags |= PMU_F_ENABLED;
  171. }
  172. /*
  173. * Change the CPUMF state to inactive.
  174. * Disable and enable (inactive) the CPU-counter sets according
  175. * to the per-cpu control state.
  176. */
  177. static void cpumf_pmu_disable(struct pmu *pmu)
  178. {
  179. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  180. int err;
  181. u64 inactive;
  182. if (!(cpuhw->flags & PMU_F_ENABLED))
  183. return;
  184. inactive = cpuhw->state & ~((1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1);
  185. err = lcctl(inactive);
  186. if (err) {
  187. pr_err("Disabling the performance measuring unit "
  188. "failed with rc=%x\n", err);
  189. return;
  190. }
  191. cpuhw->flags &= ~PMU_F_ENABLED;
  192. }
  193. /* Number of perf events counting hardware events */
  194. static atomic_t num_events = ATOMIC_INIT(0);
  195. /* Used to avoid races in calling reserve/release_cpumf_hardware */
  196. static DEFINE_MUTEX(pmc_reserve_mutex);
  197. /* CPU-measurement alerts for the counter facility */
  198. static void cpumf_measurement_alert(struct ext_code ext_code,
  199. unsigned int alert, unsigned long unused)
  200. {
  201. struct cpu_hw_events *cpuhw;
  202. if (!(alert & CPU_MF_INT_CF_MASK))
  203. return;
  204. inc_irq_stat(IRQEXT_CMC);
  205. cpuhw = this_cpu_ptr(&cpu_hw_events);
  206. /* Measurement alerts are shared and might happen when the PMU
  207. * is not reserved. Ignore these alerts in this case. */
  208. if (!(cpuhw->flags & PMU_F_RESERVED))
  209. return;
  210. /* counter authorization change alert */
  211. if (alert & CPU_MF_INT_CF_CACA)
  212. qctri(&cpuhw->info);
  213. /* loss of counter data alert */
  214. if (alert & CPU_MF_INT_CF_LCDA)
  215. pr_err("CPU[%i] Counter data was lost\n", smp_processor_id());
  216. /* loss of MT counter data alert */
  217. if (alert & CPU_MF_INT_CF_MTDA)
  218. pr_warn("CPU[%i] MT counter data was lost\n",
  219. smp_processor_id());
  220. }
  221. #define PMC_INIT 0
  222. #define PMC_RELEASE 1
  223. static void setup_pmc_cpu(void *flags)
  224. {
  225. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  226. switch (*((int *) flags)) {
  227. case PMC_INIT:
  228. memset(&cpuhw->info, 0, sizeof(cpuhw->info));
  229. qctri(&cpuhw->info);
  230. cpuhw->flags |= PMU_F_RESERVED;
  231. break;
  232. case PMC_RELEASE:
  233. cpuhw->flags &= ~PMU_F_RESERVED;
  234. break;
  235. }
  236. /* Disable CPU counter sets */
  237. lcctl(0);
  238. }
  239. /* Initialize the CPU-measurement facility */
  240. static int reserve_pmc_hardware(void)
  241. {
  242. int flags = PMC_INIT;
  243. on_each_cpu(setup_pmc_cpu, &flags, 1);
  244. irq_subclass_register(IRQ_SUBCLASS_MEASUREMENT_ALERT);
  245. return 0;
  246. }
  247. /* Release the CPU-measurement facility */
  248. static void release_pmc_hardware(void)
  249. {
  250. int flags = PMC_RELEASE;
  251. on_each_cpu(setup_pmc_cpu, &flags, 1);
  252. irq_subclass_unregister(IRQ_SUBCLASS_MEASUREMENT_ALERT);
  253. }
  254. /* Release the PMU if event is the last perf event */
  255. static void hw_perf_event_destroy(struct perf_event *event)
  256. {
  257. if (!atomic_add_unless(&num_events, -1, 1)) {
  258. mutex_lock(&pmc_reserve_mutex);
  259. if (atomic_dec_return(&num_events) == 0)
  260. release_pmc_hardware();
  261. mutex_unlock(&pmc_reserve_mutex);
  262. }
  263. }
  264. /* CPUMF <-> perf event mappings for kernel+userspace (basic set) */
  265. static const int cpumf_generic_events_basic[] = {
  266. [PERF_COUNT_HW_CPU_CYCLES] = 0,
  267. [PERF_COUNT_HW_INSTRUCTIONS] = 1,
  268. [PERF_COUNT_HW_CACHE_REFERENCES] = -1,
  269. [PERF_COUNT_HW_CACHE_MISSES] = -1,
  270. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1,
  271. [PERF_COUNT_HW_BRANCH_MISSES] = -1,
  272. [PERF_COUNT_HW_BUS_CYCLES] = -1,
  273. };
  274. /* CPUMF <-> perf event mappings for userspace (problem-state set) */
  275. static const int cpumf_generic_events_user[] = {
  276. [PERF_COUNT_HW_CPU_CYCLES] = 32,
  277. [PERF_COUNT_HW_INSTRUCTIONS] = 33,
  278. [PERF_COUNT_HW_CACHE_REFERENCES] = -1,
  279. [PERF_COUNT_HW_CACHE_MISSES] = -1,
  280. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1,
  281. [PERF_COUNT_HW_BRANCH_MISSES] = -1,
  282. [PERF_COUNT_HW_BUS_CYCLES] = -1,
  283. };
  284. static int __hw_perf_event_init(struct perf_event *event)
  285. {
  286. struct perf_event_attr *attr = &event->attr;
  287. struct hw_perf_event *hwc = &event->hw;
  288. enum cpumf_ctr_set set;
  289. int err;
  290. u64 ev;
  291. switch (attr->type) {
  292. case PERF_TYPE_RAW:
  293. /* Raw events are used to access counters directly,
  294. * hence do not permit excludes */
  295. if (attr->exclude_kernel || attr->exclude_user ||
  296. attr->exclude_hv)
  297. return -EOPNOTSUPP;
  298. ev = attr->config;
  299. break;
  300. case PERF_TYPE_HARDWARE:
  301. if (is_sampling_event(event)) /* No sampling support */
  302. return -ENOENT;
  303. ev = attr->config;
  304. /* Count user space (problem-state) only */
  305. if (!attr->exclude_user && attr->exclude_kernel) {
  306. if (ev >= ARRAY_SIZE(cpumf_generic_events_user))
  307. return -EOPNOTSUPP;
  308. ev = cpumf_generic_events_user[ev];
  309. /* No support for kernel space counters only */
  310. } else if (!attr->exclude_kernel && attr->exclude_user) {
  311. return -EOPNOTSUPP;
  312. /* Count user and kernel space */
  313. } else {
  314. if (ev >= ARRAY_SIZE(cpumf_generic_events_basic))
  315. return -EOPNOTSUPP;
  316. ev = cpumf_generic_events_basic[ev];
  317. }
  318. break;
  319. default:
  320. return -ENOENT;
  321. }
  322. if (ev == -1)
  323. return -ENOENT;
  324. if (ev > PERF_CPUM_CF_MAX_CTR)
  325. return -ENOENT;
  326. /* Obtain the counter set to which the specified counter belongs */
  327. set = get_counter_set(ev);
  328. switch (set) {
  329. case CPUMF_CTR_SET_BASIC:
  330. case CPUMF_CTR_SET_USER:
  331. case CPUMF_CTR_SET_CRYPTO:
  332. case CPUMF_CTR_SET_EXT:
  333. case CPUMF_CTR_SET_MT_DIAG:
  334. /*
  335. * Use the hardware perf event structure to store the
  336. * counter number in the 'config' member and the counter
  337. * set number in the 'config_base'. The counter set number
  338. * is then later used to enable/disable the counter(s).
  339. */
  340. hwc->config = ev;
  341. hwc->config_base = set;
  342. break;
  343. case CPUMF_CTR_SET_MAX:
  344. /* The counter could not be associated to a counter set */
  345. return -EINVAL;
  346. };
  347. /* Initialize for using the CPU-measurement counter facility */
  348. if (!atomic_inc_not_zero(&num_events)) {
  349. mutex_lock(&pmc_reserve_mutex);
  350. if (atomic_read(&num_events) == 0 && reserve_pmc_hardware())
  351. err = -EBUSY;
  352. else
  353. atomic_inc(&num_events);
  354. mutex_unlock(&pmc_reserve_mutex);
  355. }
  356. event->destroy = hw_perf_event_destroy;
  357. /* Finally, validate version and authorization of the counter set */
  358. err = validate_ctr_auth(hwc);
  359. if (!err)
  360. err = validate_ctr_version(hwc);
  361. return err;
  362. }
  363. static int cpumf_pmu_event_init(struct perf_event *event)
  364. {
  365. int err;
  366. switch (event->attr.type) {
  367. case PERF_TYPE_HARDWARE:
  368. case PERF_TYPE_HW_CACHE:
  369. case PERF_TYPE_RAW:
  370. err = __hw_perf_event_init(event);
  371. break;
  372. default:
  373. return -ENOENT;
  374. }
  375. if (unlikely(err) && event->destroy)
  376. event->destroy(event);
  377. return err;
  378. }
  379. static int hw_perf_event_reset(struct perf_event *event)
  380. {
  381. u64 prev, new;
  382. int err;
  383. do {
  384. prev = local64_read(&event->hw.prev_count);
  385. err = ecctr(event->hw.config, &new);
  386. if (err) {
  387. if (err != 3)
  388. break;
  389. /* The counter is not (yet) available. This
  390. * might happen if the counter set to which
  391. * this counter belongs is in the disabled
  392. * state.
  393. */
  394. new = 0;
  395. }
  396. } while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
  397. return err;
  398. }
  399. static void hw_perf_event_update(struct perf_event *event)
  400. {
  401. u64 prev, new, delta;
  402. int err;
  403. do {
  404. prev = local64_read(&event->hw.prev_count);
  405. err = ecctr(event->hw.config, &new);
  406. if (err)
  407. return;
  408. } while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
  409. delta = (prev <= new) ? new - prev
  410. : (-1ULL - prev) + new + 1; /* overflow */
  411. local64_add(delta, &event->count);
  412. }
  413. static void cpumf_pmu_read(struct perf_event *event)
  414. {
  415. if (event->hw.state & PERF_HES_STOPPED)
  416. return;
  417. hw_perf_event_update(event);
  418. }
  419. static void cpumf_pmu_start(struct perf_event *event, int flags)
  420. {
  421. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  422. struct hw_perf_event *hwc = &event->hw;
  423. if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
  424. return;
  425. if (WARN_ON_ONCE(hwc->config == -1))
  426. return;
  427. if (flags & PERF_EF_RELOAD)
  428. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  429. hwc->state = 0;
  430. /* (Re-)enable and activate the counter set */
  431. ctr_set_enable(&cpuhw->state, hwc->config_base);
  432. ctr_set_start(&cpuhw->state, hwc->config_base);
  433. /* The counter set to which this counter belongs can be already active.
  434. * Because all counters in a set are active, the event->hw.prev_count
  435. * needs to be synchronized. At this point, the counter set can be in
  436. * the inactive or disabled state.
  437. */
  438. hw_perf_event_reset(event);
  439. /* increment refcount for this counter set */
  440. atomic_inc(&cpuhw->ctr_set[hwc->config_base]);
  441. }
  442. static void cpumf_pmu_stop(struct perf_event *event, int flags)
  443. {
  444. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  445. struct hw_perf_event *hwc = &event->hw;
  446. if (!(hwc->state & PERF_HES_STOPPED)) {
  447. /* Decrement reference count for this counter set and if this
  448. * is the last used counter in the set, clear activation
  449. * control and set the counter set state to inactive.
  450. */
  451. if (!atomic_dec_return(&cpuhw->ctr_set[hwc->config_base]))
  452. ctr_set_stop(&cpuhw->state, hwc->config_base);
  453. event->hw.state |= PERF_HES_STOPPED;
  454. }
  455. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  456. hw_perf_event_update(event);
  457. event->hw.state |= PERF_HES_UPTODATE;
  458. }
  459. }
  460. static int cpumf_pmu_add(struct perf_event *event, int flags)
  461. {
  462. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  463. /* Check authorization for the counter set to which this
  464. * counter belongs.
  465. * For group events transaction, the authorization check is
  466. * done in cpumf_pmu_commit_txn().
  467. */
  468. if (!(cpuhw->txn_flags & PERF_PMU_TXN_ADD))
  469. if (validate_ctr_auth(&event->hw))
  470. return -ENOENT;
  471. ctr_set_enable(&cpuhw->state, event->hw.config_base);
  472. event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  473. if (flags & PERF_EF_START)
  474. cpumf_pmu_start(event, PERF_EF_RELOAD);
  475. perf_event_update_userpage(event);
  476. return 0;
  477. }
  478. static void cpumf_pmu_del(struct perf_event *event, int flags)
  479. {
  480. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  481. cpumf_pmu_stop(event, PERF_EF_UPDATE);
  482. /* Check if any counter in the counter set is still used. If not used,
  483. * change the counter set to the disabled state. This also clears the
  484. * content of all counters in the set.
  485. *
  486. * When a new perf event has been added but not yet started, this can
  487. * clear enable control and resets all counters in a set. Therefore,
  488. * cpumf_pmu_start() always has to reenable a counter set.
  489. */
  490. if (!atomic_read(&cpuhw->ctr_set[event->hw.config_base]))
  491. ctr_set_disable(&cpuhw->state, event->hw.config_base);
  492. perf_event_update_userpage(event);
  493. }
  494. /*
  495. * Start group events scheduling transaction.
  496. * Set flags to perform a single test at commit time.
  497. *
  498. * We only support PERF_PMU_TXN_ADD transactions. Save the
  499. * transaction flags but otherwise ignore non-PERF_PMU_TXN_ADD
  500. * transactions.
  501. */
  502. static void cpumf_pmu_start_txn(struct pmu *pmu, unsigned int txn_flags)
  503. {
  504. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  505. WARN_ON_ONCE(cpuhw->txn_flags); /* txn already in flight */
  506. cpuhw->txn_flags = txn_flags;
  507. if (txn_flags & ~PERF_PMU_TXN_ADD)
  508. return;
  509. perf_pmu_disable(pmu);
  510. cpuhw->tx_state = cpuhw->state;
  511. }
  512. /*
  513. * Stop and cancel a group events scheduling tranctions.
  514. * Assumes cpumf_pmu_del() is called for each successful added
  515. * cpumf_pmu_add() during the transaction.
  516. */
  517. static void cpumf_pmu_cancel_txn(struct pmu *pmu)
  518. {
  519. unsigned int txn_flags;
  520. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  521. WARN_ON_ONCE(!cpuhw->txn_flags); /* no txn in flight */
  522. txn_flags = cpuhw->txn_flags;
  523. cpuhw->txn_flags = 0;
  524. if (txn_flags & ~PERF_PMU_TXN_ADD)
  525. return;
  526. WARN_ON(cpuhw->tx_state != cpuhw->state);
  527. perf_pmu_enable(pmu);
  528. }
  529. /*
  530. * Commit the group events scheduling transaction. On success, the
  531. * transaction is closed. On error, the transaction is kept open
  532. * until cpumf_pmu_cancel_txn() is called.
  533. */
  534. static int cpumf_pmu_commit_txn(struct pmu *pmu)
  535. {
  536. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  537. u64 state;
  538. WARN_ON_ONCE(!cpuhw->txn_flags); /* no txn in flight */
  539. if (cpuhw->txn_flags & ~PERF_PMU_TXN_ADD) {
  540. cpuhw->txn_flags = 0;
  541. return 0;
  542. }
  543. /* check if the updated state can be scheduled */
  544. state = cpuhw->state & ~((1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1);
  545. state >>= CPUMF_LCCTL_ENABLE_SHIFT;
  546. if ((state & cpuhw->info.auth_ctl) != state)
  547. return -ENOENT;
  548. cpuhw->txn_flags = 0;
  549. perf_pmu_enable(pmu);
  550. return 0;
  551. }
  552. /* Performance monitoring unit for s390x */
  553. static struct pmu cpumf_pmu = {
  554. .task_ctx_nr = perf_sw_context,
  555. .capabilities = PERF_PMU_CAP_NO_INTERRUPT,
  556. .pmu_enable = cpumf_pmu_enable,
  557. .pmu_disable = cpumf_pmu_disable,
  558. .event_init = cpumf_pmu_event_init,
  559. .add = cpumf_pmu_add,
  560. .del = cpumf_pmu_del,
  561. .start = cpumf_pmu_start,
  562. .stop = cpumf_pmu_stop,
  563. .read = cpumf_pmu_read,
  564. .start_txn = cpumf_pmu_start_txn,
  565. .commit_txn = cpumf_pmu_commit_txn,
  566. .cancel_txn = cpumf_pmu_cancel_txn,
  567. };
  568. static int cpumf_pmf_setup(unsigned int cpu, int flags)
  569. {
  570. local_irq_disable();
  571. setup_pmc_cpu(&flags);
  572. local_irq_enable();
  573. return 0;
  574. }
  575. static int s390_pmu_online_cpu(unsigned int cpu)
  576. {
  577. return cpumf_pmf_setup(cpu, PMC_INIT);
  578. }
  579. static int s390_pmu_offline_cpu(unsigned int cpu)
  580. {
  581. return cpumf_pmf_setup(cpu, PMC_RELEASE);
  582. }
  583. static int __init cpumf_pmu_init(void)
  584. {
  585. int rc;
  586. if (!cpum_cf_avail())
  587. return -ENODEV;
  588. /* clear bit 15 of cr0 to unauthorize problem-state to
  589. * extract measurement counters */
  590. ctl_clear_bit(0, 48);
  591. /* register handler for measurement-alert interruptions */
  592. rc = register_external_irq(EXT_IRQ_MEASURE_ALERT,
  593. cpumf_measurement_alert);
  594. if (rc) {
  595. pr_err("Registering for CPU-measurement alerts "
  596. "failed with rc=%i\n", rc);
  597. return rc;
  598. }
  599. cpumf_pmu.attr_groups = cpumf_cf_event_group();
  600. rc = perf_pmu_register(&cpumf_pmu, "cpum_cf", PERF_TYPE_RAW);
  601. if (rc) {
  602. pr_err("Registering the cpum_cf PMU failed with rc=%i\n", rc);
  603. unregister_external_irq(EXT_IRQ_MEASURE_ALERT,
  604. cpumf_measurement_alert);
  605. return rc;
  606. }
  607. return cpuhp_setup_state(CPUHP_AP_PERF_S390_CF_ONLINE,
  608. "perf/s390/cf:online",
  609. s390_pmu_online_cpu, s390_pmu_offline_cpu);
  610. }
  611. early_initcall(cpumf_pmu_init);