perf_cpum_cf.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. * Performance event support for s390x - CPU-measurement Counter Facility
  3. *
  4. * Copyright IBM Corp. 2012
  5. * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License (version 2 only)
  9. * as published by the Free Software Foundation.
  10. */
  11. #define KMSG_COMPONENT "cpum_cf"
  12. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  13. #include <linux/kernel.h>
  14. #include <linux/kernel_stat.h>
  15. #include <linux/perf_event.h>
  16. #include <linux/percpu.h>
  17. #include <linux/notifier.h>
  18. #include <linux/init.h>
  19. #include <linux/export.h>
  20. #include <asm/ctl_reg.h>
  21. #include <asm/irq.h>
  22. #include <asm/cpu_mf.h>
  23. /* CPU-measurement counter facility supports these CPU counter sets:
  24. * For CPU counter sets:
  25. * Basic counter set: 0-31
  26. * Problem-state counter set: 32-63
  27. * Crypto-activity counter set: 64-127
  28. * Extented counter set: 128-159
  29. */
  30. enum cpumf_ctr_set {
  31. /* CPU counter sets */
  32. CPUMF_CTR_SET_BASIC = 0,
  33. CPUMF_CTR_SET_USER = 1,
  34. CPUMF_CTR_SET_CRYPTO = 2,
  35. CPUMF_CTR_SET_EXT = 3,
  36. /* Maximum number of counter sets */
  37. CPUMF_CTR_SET_MAX,
  38. };
  39. #define CPUMF_LCCTL_ENABLE_SHIFT 16
  40. #define CPUMF_LCCTL_ACTCTL_SHIFT 0
  41. static const u64 cpumf_state_ctl[CPUMF_CTR_SET_MAX] = {
  42. [CPUMF_CTR_SET_BASIC] = 0x02,
  43. [CPUMF_CTR_SET_USER] = 0x04,
  44. [CPUMF_CTR_SET_CRYPTO] = 0x08,
  45. [CPUMF_CTR_SET_EXT] = 0x01,
  46. };
  47. static void ctr_set_enable(u64 *state, int ctr_set)
  48. {
  49. *state |= cpumf_state_ctl[ctr_set] << CPUMF_LCCTL_ENABLE_SHIFT;
  50. }
  51. static void ctr_set_disable(u64 *state, int ctr_set)
  52. {
  53. *state &= ~(cpumf_state_ctl[ctr_set] << CPUMF_LCCTL_ENABLE_SHIFT);
  54. }
  55. static void ctr_set_start(u64 *state, int ctr_set)
  56. {
  57. *state |= cpumf_state_ctl[ctr_set] << CPUMF_LCCTL_ACTCTL_SHIFT;
  58. }
  59. static void ctr_set_stop(u64 *state, int ctr_set)
  60. {
  61. *state &= ~(cpumf_state_ctl[ctr_set] << CPUMF_LCCTL_ACTCTL_SHIFT);
  62. }
  63. /* Local CPUMF event structure */
  64. struct cpu_hw_events {
  65. struct cpumf_ctr_info info;
  66. atomic_t ctr_set[CPUMF_CTR_SET_MAX];
  67. u64 state, tx_state;
  68. unsigned int flags;
  69. };
  70. static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
  71. .ctr_set = {
  72. [CPUMF_CTR_SET_BASIC] = ATOMIC_INIT(0),
  73. [CPUMF_CTR_SET_USER] = ATOMIC_INIT(0),
  74. [CPUMF_CTR_SET_CRYPTO] = ATOMIC_INIT(0),
  75. [CPUMF_CTR_SET_EXT] = ATOMIC_INIT(0),
  76. },
  77. .state = 0,
  78. .flags = 0,
  79. };
  80. static int get_counter_set(u64 event)
  81. {
  82. int set = -1;
  83. if (event < 32)
  84. set = CPUMF_CTR_SET_BASIC;
  85. else if (event < 64)
  86. set = CPUMF_CTR_SET_USER;
  87. else if (event < 128)
  88. set = CPUMF_CTR_SET_CRYPTO;
  89. else if (event < 256)
  90. set = CPUMF_CTR_SET_EXT;
  91. return set;
  92. }
  93. static int validate_event(const struct hw_perf_event *hwc)
  94. {
  95. switch (hwc->config_base) {
  96. case CPUMF_CTR_SET_BASIC:
  97. case CPUMF_CTR_SET_USER:
  98. case CPUMF_CTR_SET_CRYPTO:
  99. case CPUMF_CTR_SET_EXT:
  100. /* check for reserved counters */
  101. if ((hwc->config >= 6 && hwc->config <= 31) ||
  102. (hwc->config >= 38 && hwc->config <= 63) ||
  103. (hwc->config >= 80 && hwc->config <= 127))
  104. return -EOPNOTSUPP;
  105. break;
  106. default:
  107. return -EINVAL;
  108. }
  109. return 0;
  110. }
  111. static int validate_ctr_version(const struct hw_perf_event *hwc)
  112. {
  113. struct cpu_hw_events *cpuhw;
  114. int err = 0;
  115. cpuhw = &get_cpu_var(cpu_hw_events);
  116. /* check required version for counter sets */
  117. switch (hwc->config_base) {
  118. case CPUMF_CTR_SET_BASIC:
  119. case CPUMF_CTR_SET_USER:
  120. if (cpuhw->info.cfvn < 1)
  121. err = -EOPNOTSUPP;
  122. break;
  123. case CPUMF_CTR_SET_CRYPTO:
  124. case CPUMF_CTR_SET_EXT:
  125. if (cpuhw->info.csvn < 1)
  126. err = -EOPNOTSUPP;
  127. if ((cpuhw->info.csvn == 1 && hwc->config > 159) ||
  128. (cpuhw->info.csvn == 2 && hwc->config > 175) ||
  129. (cpuhw->info.csvn > 2 && hwc->config > 255))
  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. }
  217. #define PMC_INIT 0
  218. #define PMC_RELEASE 1
  219. static void setup_pmc_cpu(void *flags)
  220. {
  221. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  222. switch (*((int *) flags)) {
  223. case PMC_INIT:
  224. memset(&cpuhw->info, 0, sizeof(cpuhw->info));
  225. qctri(&cpuhw->info);
  226. cpuhw->flags |= PMU_F_RESERVED;
  227. break;
  228. case PMC_RELEASE:
  229. cpuhw->flags &= ~PMU_F_RESERVED;
  230. break;
  231. }
  232. /* Disable CPU counter sets */
  233. lcctl(0);
  234. }
  235. /* Initialize the CPU-measurement facility */
  236. static int reserve_pmc_hardware(void)
  237. {
  238. int flags = PMC_INIT;
  239. on_each_cpu(setup_pmc_cpu, &flags, 1);
  240. irq_subclass_register(IRQ_SUBCLASS_MEASUREMENT_ALERT);
  241. return 0;
  242. }
  243. /* Release the CPU-measurement facility */
  244. static void release_pmc_hardware(void)
  245. {
  246. int flags = PMC_RELEASE;
  247. on_each_cpu(setup_pmc_cpu, &flags, 1);
  248. irq_subclass_unregister(IRQ_SUBCLASS_MEASUREMENT_ALERT);
  249. }
  250. /* Release the PMU if event is the last perf event */
  251. static void hw_perf_event_destroy(struct perf_event *event)
  252. {
  253. if (!atomic_add_unless(&num_events, -1, 1)) {
  254. mutex_lock(&pmc_reserve_mutex);
  255. if (atomic_dec_return(&num_events) == 0)
  256. release_pmc_hardware();
  257. mutex_unlock(&pmc_reserve_mutex);
  258. }
  259. }
  260. /* CPUMF <-> perf event mappings for kernel+userspace (basic set) */
  261. static const int cpumf_generic_events_basic[] = {
  262. [PERF_COUNT_HW_CPU_CYCLES] = 0,
  263. [PERF_COUNT_HW_INSTRUCTIONS] = 1,
  264. [PERF_COUNT_HW_CACHE_REFERENCES] = -1,
  265. [PERF_COUNT_HW_CACHE_MISSES] = -1,
  266. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1,
  267. [PERF_COUNT_HW_BRANCH_MISSES] = -1,
  268. [PERF_COUNT_HW_BUS_CYCLES] = -1,
  269. };
  270. /* CPUMF <-> perf event mappings for userspace (problem-state set) */
  271. static const int cpumf_generic_events_user[] = {
  272. [PERF_COUNT_HW_CPU_CYCLES] = 32,
  273. [PERF_COUNT_HW_INSTRUCTIONS] = 33,
  274. [PERF_COUNT_HW_CACHE_REFERENCES] = -1,
  275. [PERF_COUNT_HW_CACHE_MISSES] = -1,
  276. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1,
  277. [PERF_COUNT_HW_BRANCH_MISSES] = -1,
  278. [PERF_COUNT_HW_BUS_CYCLES] = -1,
  279. };
  280. static int __hw_perf_event_init(struct perf_event *event)
  281. {
  282. struct perf_event_attr *attr = &event->attr;
  283. struct hw_perf_event *hwc = &event->hw;
  284. int err;
  285. u64 ev;
  286. switch (attr->type) {
  287. case PERF_TYPE_RAW:
  288. /* Raw events are used to access counters directly,
  289. * hence do not permit excludes */
  290. if (attr->exclude_kernel || attr->exclude_user ||
  291. attr->exclude_hv)
  292. return -EOPNOTSUPP;
  293. ev = attr->config;
  294. break;
  295. case PERF_TYPE_HARDWARE:
  296. ev = attr->config;
  297. /* Count user space (problem-state) only */
  298. if (!attr->exclude_user && attr->exclude_kernel) {
  299. if (ev >= ARRAY_SIZE(cpumf_generic_events_user))
  300. return -EOPNOTSUPP;
  301. ev = cpumf_generic_events_user[ev];
  302. /* No support for kernel space counters only */
  303. } else if (!attr->exclude_kernel && attr->exclude_user) {
  304. return -EOPNOTSUPP;
  305. /* Count user and kernel space */
  306. } else {
  307. if (ev >= ARRAY_SIZE(cpumf_generic_events_basic))
  308. return -EOPNOTSUPP;
  309. ev = cpumf_generic_events_basic[ev];
  310. }
  311. break;
  312. default:
  313. return -ENOENT;
  314. }
  315. if (ev == -1)
  316. return -ENOENT;
  317. if (ev >= PERF_CPUM_CF_MAX_CTR)
  318. return -EINVAL;
  319. /* Use the hardware perf event structure to store the counter number
  320. * in 'config' member and the counter set to which the counter belongs
  321. * in the 'config_base'. The counter set (config_base) is then used
  322. * to enable/disable the counters.
  323. */
  324. hwc->config = ev;
  325. hwc->config_base = get_counter_set(ev);
  326. /* Validate the counter that is assigned to this event.
  327. * Because the counter facility can use numerous counters at the
  328. * same time without constraints, it is not necessary to explicity
  329. * validate event groups (event->group_leader != event).
  330. */
  331. err = validate_event(hwc);
  332. if (err)
  333. return err;
  334. /* Initialize for using the CPU-measurement counter facility */
  335. if (!atomic_inc_not_zero(&num_events)) {
  336. mutex_lock(&pmc_reserve_mutex);
  337. if (atomic_read(&num_events) == 0 && reserve_pmc_hardware())
  338. err = -EBUSY;
  339. else
  340. atomic_inc(&num_events);
  341. mutex_unlock(&pmc_reserve_mutex);
  342. }
  343. event->destroy = hw_perf_event_destroy;
  344. /* Finally, validate version and authorization of the counter set */
  345. err = validate_ctr_auth(hwc);
  346. if (!err)
  347. err = validate_ctr_version(hwc);
  348. return err;
  349. }
  350. static int cpumf_pmu_event_init(struct perf_event *event)
  351. {
  352. int err;
  353. switch (event->attr.type) {
  354. case PERF_TYPE_HARDWARE:
  355. case PERF_TYPE_HW_CACHE:
  356. case PERF_TYPE_RAW:
  357. err = __hw_perf_event_init(event);
  358. break;
  359. default:
  360. return -ENOENT;
  361. }
  362. if (unlikely(err) && event->destroy)
  363. event->destroy(event);
  364. return err;
  365. }
  366. static int hw_perf_event_reset(struct perf_event *event)
  367. {
  368. u64 prev, new;
  369. int err;
  370. do {
  371. prev = local64_read(&event->hw.prev_count);
  372. err = ecctr(event->hw.config, &new);
  373. if (err) {
  374. if (err != 3)
  375. break;
  376. /* The counter is not (yet) available. This
  377. * might happen if the counter set to which
  378. * this counter belongs is in the disabled
  379. * state.
  380. */
  381. new = 0;
  382. }
  383. } while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
  384. return err;
  385. }
  386. static int hw_perf_event_update(struct perf_event *event)
  387. {
  388. u64 prev, new, delta;
  389. int err;
  390. do {
  391. prev = local64_read(&event->hw.prev_count);
  392. err = ecctr(event->hw.config, &new);
  393. if (err)
  394. goto out;
  395. } while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
  396. delta = (prev <= new) ? new - prev
  397. : (-1ULL - prev) + new + 1; /* overflow */
  398. local64_add(delta, &event->count);
  399. out:
  400. return err;
  401. }
  402. static void cpumf_pmu_read(struct perf_event *event)
  403. {
  404. if (event->hw.state & PERF_HES_STOPPED)
  405. return;
  406. hw_perf_event_update(event);
  407. }
  408. static void cpumf_pmu_start(struct perf_event *event, int flags)
  409. {
  410. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  411. struct hw_perf_event *hwc = &event->hw;
  412. if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
  413. return;
  414. if (WARN_ON_ONCE(hwc->config == -1))
  415. return;
  416. if (flags & PERF_EF_RELOAD)
  417. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  418. hwc->state = 0;
  419. /* (Re-)enable and activate the counter set */
  420. ctr_set_enable(&cpuhw->state, hwc->config_base);
  421. ctr_set_start(&cpuhw->state, hwc->config_base);
  422. /* The counter set to which this counter belongs can be already active.
  423. * Because all counters in a set are active, the event->hw.prev_count
  424. * needs to be synchronized. At this point, the counter set can be in
  425. * the inactive or disabled state.
  426. */
  427. hw_perf_event_reset(event);
  428. /* increment refcount for this counter set */
  429. atomic_inc(&cpuhw->ctr_set[hwc->config_base]);
  430. }
  431. static void cpumf_pmu_stop(struct perf_event *event, int flags)
  432. {
  433. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  434. struct hw_perf_event *hwc = &event->hw;
  435. if (!(hwc->state & PERF_HES_STOPPED)) {
  436. /* Decrement reference count for this counter set and if this
  437. * is the last used counter in the set, clear activation
  438. * control and set the counter set state to inactive.
  439. */
  440. if (!atomic_dec_return(&cpuhw->ctr_set[hwc->config_base]))
  441. ctr_set_stop(&cpuhw->state, hwc->config_base);
  442. event->hw.state |= PERF_HES_STOPPED;
  443. }
  444. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  445. hw_perf_event_update(event);
  446. event->hw.state |= PERF_HES_UPTODATE;
  447. }
  448. }
  449. static int cpumf_pmu_add(struct perf_event *event, int flags)
  450. {
  451. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  452. /* Check authorization for the counter set to which this
  453. * counter belongs.
  454. * For group events transaction, the authorization check is
  455. * done in cpumf_pmu_commit_txn().
  456. */
  457. if (!(cpuhw->flags & PERF_EVENT_TXN))
  458. if (validate_ctr_auth(&event->hw))
  459. return -ENOENT;
  460. ctr_set_enable(&cpuhw->state, event->hw.config_base);
  461. event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  462. if (flags & PERF_EF_START)
  463. cpumf_pmu_start(event, PERF_EF_RELOAD);
  464. perf_event_update_userpage(event);
  465. return 0;
  466. }
  467. static void cpumf_pmu_del(struct perf_event *event, int flags)
  468. {
  469. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  470. cpumf_pmu_stop(event, PERF_EF_UPDATE);
  471. /* Check if any counter in the counter set is still used. If not used,
  472. * change the counter set to the disabled state. This also clears the
  473. * content of all counters in the set.
  474. *
  475. * When a new perf event has been added but not yet started, this can
  476. * clear enable control and resets all counters in a set. Therefore,
  477. * cpumf_pmu_start() always has to reenable a counter set.
  478. */
  479. if (!atomic_read(&cpuhw->ctr_set[event->hw.config_base]))
  480. ctr_set_disable(&cpuhw->state, event->hw.config_base);
  481. perf_event_update_userpage(event);
  482. }
  483. /*
  484. * Start group events scheduling transaction.
  485. * Set flags to perform a single test at commit time.
  486. */
  487. static void cpumf_pmu_start_txn(struct pmu *pmu)
  488. {
  489. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  490. perf_pmu_disable(pmu);
  491. cpuhw->flags |= PERF_EVENT_TXN;
  492. cpuhw->tx_state = cpuhw->state;
  493. }
  494. /*
  495. * Stop and cancel a group events scheduling tranctions.
  496. * Assumes cpumf_pmu_del() is called for each successful added
  497. * cpumf_pmu_add() during the transaction.
  498. */
  499. static void cpumf_pmu_cancel_txn(struct pmu *pmu)
  500. {
  501. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  502. WARN_ON(cpuhw->tx_state != cpuhw->state);
  503. cpuhw->flags &= ~PERF_EVENT_TXN;
  504. perf_pmu_enable(pmu);
  505. }
  506. /*
  507. * Commit the group events scheduling transaction. On success, the
  508. * transaction is closed. On error, the transaction is kept open
  509. * until cpumf_pmu_cancel_txn() is called.
  510. */
  511. static int cpumf_pmu_commit_txn(struct pmu *pmu)
  512. {
  513. struct cpu_hw_events *cpuhw = this_cpu_ptr(&cpu_hw_events);
  514. u64 state;
  515. /* check if the updated state can be scheduled */
  516. state = cpuhw->state & ~((1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1);
  517. state >>= CPUMF_LCCTL_ENABLE_SHIFT;
  518. if ((state & cpuhw->info.auth_ctl) != state)
  519. return -ENOENT;
  520. cpuhw->flags &= ~PERF_EVENT_TXN;
  521. perf_pmu_enable(pmu);
  522. return 0;
  523. }
  524. /* Performance monitoring unit for s390x */
  525. static struct pmu cpumf_pmu = {
  526. .pmu_enable = cpumf_pmu_enable,
  527. .pmu_disable = cpumf_pmu_disable,
  528. .event_init = cpumf_pmu_event_init,
  529. .add = cpumf_pmu_add,
  530. .del = cpumf_pmu_del,
  531. .start = cpumf_pmu_start,
  532. .stop = cpumf_pmu_stop,
  533. .read = cpumf_pmu_read,
  534. .start_txn = cpumf_pmu_start_txn,
  535. .commit_txn = cpumf_pmu_commit_txn,
  536. .cancel_txn = cpumf_pmu_cancel_txn,
  537. };
  538. static int cpumf_pmu_notifier(struct notifier_block *self, unsigned long action,
  539. void *hcpu)
  540. {
  541. unsigned int cpu = (long) hcpu;
  542. int flags;
  543. switch (action & ~CPU_TASKS_FROZEN) {
  544. case CPU_ONLINE:
  545. flags = PMC_INIT;
  546. smp_call_function_single(cpu, setup_pmc_cpu, &flags, 1);
  547. break;
  548. case CPU_DOWN_PREPARE:
  549. flags = PMC_RELEASE;
  550. smp_call_function_single(cpu, setup_pmc_cpu, &flags, 1);
  551. break;
  552. default:
  553. break;
  554. }
  555. return NOTIFY_OK;
  556. }
  557. static int __init cpumf_pmu_init(void)
  558. {
  559. int rc;
  560. if (!cpum_cf_avail())
  561. return -ENODEV;
  562. /* clear bit 15 of cr0 to unauthorize problem-state to
  563. * extract measurement counters */
  564. ctl_clear_bit(0, 48);
  565. /* register handler for measurement-alert interruptions */
  566. rc = register_external_irq(EXT_IRQ_MEASURE_ALERT,
  567. cpumf_measurement_alert);
  568. if (rc) {
  569. pr_err("Registering for CPU-measurement alerts "
  570. "failed with rc=%i\n", rc);
  571. goto out;
  572. }
  573. /* The CPU measurement counter facility does not have overflow
  574. * interrupts to do sampling. Sampling must be provided by
  575. * external means, for example, by timers.
  576. */
  577. cpumf_pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  578. cpumf_pmu.attr_groups = cpumf_cf_event_group();
  579. rc = perf_pmu_register(&cpumf_pmu, "cpum_cf", PERF_TYPE_RAW);
  580. if (rc) {
  581. pr_err("Registering the cpum_cf PMU failed with rc=%i\n", rc);
  582. unregister_external_irq(EXT_IRQ_MEASURE_ALERT,
  583. cpumf_measurement_alert);
  584. goto out;
  585. }
  586. perf_cpu_notifier(cpumf_pmu_notifier);
  587. out:
  588. return rc;
  589. }
  590. early_initcall(cpumf_pmu_init);