perf_cpum_cf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. ctrs_state = cpumf_state_ctl[hwc->config_base];
  144. if (!(ctrs_state & cpuhw->info.auth_ctl))
  145. err = -EPERM;
  146. put_cpu_var(cpu_hw_events);
  147. return err;
  148. }
  149. /*
  150. * Change the CPUMF state to active.
  151. * Enable and activate the CPU-counter sets according
  152. * to the per-cpu control state.
  153. */
  154. static void cpumf_pmu_enable(struct pmu *pmu)
  155. {
  156. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  157. int err;
  158. if (cpuhw->flags & PMU_F_ENABLED)
  159. return;
  160. err = lcctl(cpuhw->state);
  161. if (err) {
  162. pr_err("Enabling the performance measuring unit "
  163. "failed with rc=%x\n", err);
  164. return;
  165. }
  166. cpuhw->flags |= PMU_F_ENABLED;
  167. }
  168. /*
  169. * Change the CPUMF state to inactive.
  170. * Disable and enable (inactive) the CPU-counter sets according
  171. * to the per-cpu control state.
  172. */
  173. static void cpumf_pmu_disable(struct pmu *pmu)
  174. {
  175. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  176. int err;
  177. u64 inactive;
  178. if (!(cpuhw->flags & PMU_F_ENABLED))
  179. return;
  180. inactive = cpuhw->state & ~((1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1);
  181. err = lcctl(inactive);
  182. if (err) {
  183. pr_err("Disabling the performance measuring unit "
  184. "failed with rc=%x\n", err);
  185. return;
  186. }
  187. cpuhw->flags &= ~PMU_F_ENABLED;
  188. }
  189. /* Number of perf events counting hardware events */
  190. static atomic_t num_events = ATOMIC_INIT(0);
  191. /* Used to avoid races in calling reserve/release_cpumf_hardware */
  192. static DEFINE_MUTEX(pmc_reserve_mutex);
  193. /* CPU-measurement alerts for the counter facility */
  194. static void cpumf_measurement_alert(struct ext_code ext_code,
  195. unsigned int alert, unsigned long unused)
  196. {
  197. struct cpu_hw_events *cpuhw;
  198. if (!(alert & CPU_MF_INT_CF_MASK))
  199. return;
  200. inc_irq_stat(IRQEXT_CMC);
  201. cpuhw = &__get_cpu_var(cpu_hw_events);
  202. /* Measurement alerts are shared and might happen when the PMU
  203. * is not reserved. Ignore these alerts in this case. */
  204. if (!(cpuhw->flags & PMU_F_RESERVED))
  205. return;
  206. /* counter authorization change alert */
  207. if (alert & CPU_MF_INT_CF_CACA)
  208. qctri(&cpuhw->info);
  209. /* loss of counter data alert */
  210. if (alert & CPU_MF_INT_CF_LCDA)
  211. pr_err("CPU[%i] Counter data was lost\n", smp_processor_id());
  212. }
  213. #define PMC_INIT 0
  214. #define PMC_RELEASE 1
  215. static void setup_pmc_cpu(void *flags)
  216. {
  217. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  218. switch (*((int *) flags)) {
  219. case PMC_INIT:
  220. memset(&cpuhw->info, 0, sizeof(cpuhw->info));
  221. qctri(&cpuhw->info);
  222. cpuhw->flags |= PMU_F_RESERVED;
  223. break;
  224. case PMC_RELEASE:
  225. cpuhw->flags &= ~PMU_F_RESERVED;
  226. break;
  227. }
  228. /* Disable CPU counter sets */
  229. lcctl(0);
  230. }
  231. /* Initialize the CPU-measurement facility */
  232. static int reserve_pmc_hardware(void)
  233. {
  234. int flags = PMC_INIT;
  235. on_each_cpu(setup_pmc_cpu, &flags, 1);
  236. measurement_alert_subclass_register();
  237. return 0;
  238. }
  239. /* Release the CPU-measurement facility */
  240. static void release_pmc_hardware(void)
  241. {
  242. int flags = PMC_RELEASE;
  243. on_each_cpu(setup_pmc_cpu, &flags, 1);
  244. measurement_alert_subclass_unregister();
  245. }
  246. /* Release the PMU if event is the last perf event */
  247. static void hw_perf_event_destroy(struct perf_event *event)
  248. {
  249. if (!atomic_add_unless(&num_events, -1, 1)) {
  250. mutex_lock(&pmc_reserve_mutex);
  251. if (atomic_dec_return(&num_events) == 0)
  252. release_pmc_hardware();
  253. mutex_unlock(&pmc_reserve_mutex);
  254. }
  255. }
  256. /* CPUMF <-> perf event mappings for kernel+userspace (basic set) */
  257. static const int cpumf_generic_events_basic[] = {
  258. [PERF_COUNT_HW_CPU_CYCLES] = 0,
  259. [PERF_COUNT_HW_INSTRUCTIONS] = 1,
  260. [PERF_COUNT_HW_CACHE_REFERENCES] = -1,
  261. [PERF_COUNT_HW_CACHE_MISSES] = -1,
  262. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1,
  263. [PERF_COUNT_HW_BRANCH_MISSES] = -1,
  264. [PERF_COUNT_HW_BUS_CYCLES] = -1,
  265. };
  266. /* CPUMF <-> perf event mappings for userspace (problem-state set) */
  267. static const int cpumf_generic_events_user[] = {
  268. [PERF_COUNT_HW_CPU_CYCLES] = 32,
  269. [PERF_COUNT_HW_INSTRUCTIONS] = 33,
  270. [PERF_COUNT_HW_CACHE_REFERENCES] = -1,
  271. [PERF_COUNT_HW_CACHE_MISSES] = -1,
  272. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1,
  273. [PERF_COUNT_HW_BRANCH_MISSES] = -1,
  274. [PERF_COUNT_HW_BUS_CYCLES] = -1,
  275. };
  276. static int __hw_perf_event_init(struct perf_event *event)
  277. {
  278. struct perf_event_attr *attr = &event->attr;
  279. struct hw_perf_event *hwc = &event->hw;
  280. int err;
  281. u64 ev;
  282. switch (attr->type) {
  283. case PERF_TYPE_RAW:
  284. /* Raw events are used to access counters directly,
  285. * hence do not permit excludes */
  286. if (attr->exclude_kernel || attr->exclude_user ||
  287. attr->exclude_hv)
  288. return -EOPNOTSUPP;
  289. ev = attr->config;
  290. break;
  291. case PERF_TYPE_HARDWARE:
  292. ev = attr->config;
  293. /* Count user space (problem-state) only */
  294. if (!attr->exclude_user && attr->exclude_kernel) {
  295. if (ev >= ARRAY_SIZE(cpumf_generic_events_user))
  296. return -EOPNOTSUPP;
  297. ev = cpumf_generic_events_user[ev];
  298. /* No support for kernel space counters only */
  299. } else if (!attr->exclude_kernel && attr->exclude_user) {
  300. return -EOPNOTSUPP;
  301. /* Count user and kernel space */
  302. } else {
  303. if (ev >= ARRAY_SIZE(cpumf_generic_events_basic))
  304. return -EOPNOTSUPP;
  305. ev = cpumf_generic_events_basic[ev];
  306. }
  307. break;
  308. default:
  309. return -ENOENT;
  310. }
  311. if (ev == -1)
  312. return -ENOENT;
  313. if (ev >= PERF_CPUM_CF_MAX_CTR)
  314. return -EINVAL;
  315. /* Use the hardware perf event structure to store the counter number
  316. * in 'config' member and the counter set to which the counter belongs
  317. * in the 'config_base'. The counter set (config_base) is then used
  318. * to enable/disable the counters.
  319. */
  320. hwc->config = ev;
  321. hwc->config_base = get_counter_set(ev);
  322. /* Validate the counter that is assigned to this event.
  323. * Because the counter facility can use numerous counters at the
  324. * same time without constraints, it is not necessary to explicity
  325. * validate event groups (event->group_leader != event).
  326. */
  327. err = validate_event(hwc);
  328. if (err)
  329. return err;
  330. /* Initialize for using the CPU-measurement counter facility */
  331. if (!atomic_inc_not_zero(&num_events)) {
  332. mutex_lock(&pmc_reserve_mutex);
  333. if (atomic_read(&num_events) == 0 && reserve_pmc_hardware())
  334. err = -EBUSY;
  335. else
  336. atomic_inc(&num_events);
  337. mutex_unlock(&pmc_reserve_mutex);
  338. }
  339. event->destroy = hw_perf_event_destroy;
  340. /* Finally, validate version and authorization of the counter set */
  341. err = validate_ctr_auth(hwc);
  342. if (!err)
  343. err = validate_ctr_version(hwc);
  344. return err;
  345. }
  346. static int cpumf_pmu_event_init(struct perf_event *event)
  347. {
  348. int err;
  349. switch (event->attr.type) {
  350. case PERF_TYPE_HARDWARE:
  351. case PERF_TYPE_HW_CACHE:
  352. case PERF_TYPE_RAW:
  353. /* The CPU measurement counter facility does not have overflow
  354. * interrupts to do sampling. Sampling must be provided by
  355. * external means, for example, by timers.
  356. */
  357. if (is_sampling_event(event))
  358. return -ENOENT;
  359. err = __hw_perf_event_init(event);
  360. break;
  361. default:
  362. return -ENOENT;
  363. }
  364. if (unlikely(err) && event->destroy)
  365. event->destroy(event);
  366. return err;
  367. }
  368. static int hw_perf_event_reset(struct perf_event *event)
  369. {
  370. u64 prev, new;
  371. int err;
  372. do {
  373. prev = local64_read(&event->hw.prev_count);
  374. err = ecctr(event->hw.config, &new);
  375. if (err) {
  376. if (err != 3)
  377. break;
  378. /* The counter is not (yet) available. This
  379. * might happen if the counter set to which
  380. * this counter belongs is in the disabled
  381. * state.
  382. */
  383. new = 0;
  384. }
  385. } while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
  386. return err;
  387. }
  388. static int hw_perf_event_update(struct perf_event *event)
  389. {
  390. u64 prev, new, delta;
  391. int err;
  392. do {
  393. prev = local64_read(&event->hw.prev_count);
  394. err = ecctr(event->hw.config, &new);
  395. if (err)
  396. goto out;
  397. } while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
  398. delta = (prev <= new) ? new - prev
  399. : (-1ULL - prev) + new + 1; /* overflow */
  400. local64_add(delta, &event->count);
  401. out:
  402. return err;
  403. }
  404. static void cpumf_pmu_read(struct perf_event *event)
  405. {
  406. if (event->hw.state & PERF_HES_STOPPED)
  407. return;
  408. hw_perf_event_update(event);
  409. }
  410. static void cpumf_pmu_start(struct perf_event *event, int flags)
  411. {
  412. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  413. struct hw_perf_event *hwc = &event->hw;
  414. if (WARN_ON_ONCE(!(hwc->state & PERF_HES_STOPPED)))
  415. return;
  416. if (WARN_ON_ONCE(hwc->config == -1))
  417. return;
  418. if (flags & PERF_EF_RELOAD)
  419. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  420. hwc->state = 0;
  421. /* (Re-)enable and activate the counter set */
  422. ctr_set_enable(&cpuhw->state, hwc->config_base);
  423. ctr_set_start(&cpuhw->state, hwc->config_base);
  424. /* The counter set to which this counter belongs can be already active.
  425. * Because all counters in a set are active, the event->hw.prev_count
  426. * needs to be synchronized. At this point, the counter set can be in
  427. * the inactive or disabled state.
  428. */
  429. hw_perf_event_reset(event);
  430. /* increment refcount for this counter set */
  431. atomic_inc(&cpuhw->ctr_set[hwc->config_base]);
  432. }
  433. static void cpumf_pmu_stop(struct perf_event *event, int flags)
  434. {
  435. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  436. struct hw_perf_event *hwc = &event->hw;
  437. if (!(hwc->state & PERF_HES_STOPPED)) {
  438. /* Decrement reference count for this counter set and if this
  439. * is the last used counter in the set, clear activation
  440. * control and set the counter set state to inactive.
  441. */
  442. if (!atomic_dec_return(&cpuhw->ctr_set[hwc->config_base]))
  443. ctr_set_stop(&cpuhw->state, hwc->config_base);
  444. event->hw.state |= PERF_HES_STOPPED;
  445. }
  446. if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  447. hw_perf_event_update(event);
  448. event->hw.state |= PERF_HES_UPTODATE;
  449. }
  450. }
  451. static int cpumf_pmu_add(struct perf_event *event, int flags)
  452. {
  453. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  454. /* Check authorization for the counter set to which this
  455. * counter belongs.
  456. * For group events transaction, the authorization check is
  457. * done in cpumf_pmu_commit_txn().
  458. */
  459. if (!(cpuhw->flags & PERF_EVENT_TXN))
  460. if (validate_ctr_auth(&event->hw))
  461. return -EPERM;
  462. ctr_set_enable(&cpuhw->state, event->hw.config_base);
  463. event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  464. if (flags & PERF_EF_START)
  465. cpumf_pmu_start(event, PERF_EF_RELOAD);
  466. perf_event_update_userpage(event);
  467. return 0;
  468. }
  469. static void cpumf_pmu_del(struct perf_event *event, int flags)
  470. {
  471. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  472. cpumf_pmu_stop(event, PERF_EF_UPDATE);
  473. /* Check if any counter in the counter set is still used. If not used,
  474. * change the counter set to the disabled state. This also clears the
  475. * content of all counters in the set.
  476. *
  477. * When a new perf event has been added but not yet started, this can
  478. * clear enable control and resets all counters in a set. Therefore,
  479. * cpumf_pmu_start() always has to reenable a counter set.
  480. */
  481. if (!atomic_read(&cpuhw->ctr_set[event->hw.config_base]))
  482. ctr_set_disable(&cpuhw->state, event->hw.config_base);
  483. perf_event_update_userpage(event);
  484. }
  485. /*
  486. * Start group events scheduling transaction.
  487. * Set flags to perform a single test at commit time.
  488. */
  489. static void cpumf_pmu_start_txn(struct pmu *pmu)
  490. {
  491. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  492. perf_pmu_disable(pmu);
  493. cpuhw->flags |= PERF_EVENT_TXN;
  494. cpuhw->tx_state = cpuhw->state;
  495. }
  496. /*
  497. * Stop and cancel a group events scheduling tranctions.
  498. * Assumes cpumf_pmu_del() is called for each successful added
  499. * cpumf_pmu_add() during the transaction.
  500. */
  501. static void cpumf_pmu_cancel_txn(struct pmu *pmu)
  502. {
  503. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  504. WARN_ON(cpuhw->tx_state != cpuhw->state);
  505. cpuhw->flags &= ~PERF_EVENT_TXN;
  506. perf_pmu_enable(pmu);
  507. }
  508. /*
  509. * Commit the group events scheduling transaction. On success, the
  510. * transaction is closed. On error, the transaction is kept open
  511. * until cpumf_pmu_cancel_txn() is called.
  512. */
  513. static int cpumf_pmu_commit_txn(struct pmu *pmu)
  514. {
  515. struct cpu_hw_events *cpuhw = &__get_cpu_var(cpu_hw_events);
  516. u64 state;
  517. /* check if the updated state can be scheduled */
  518. state = cpuhw->state & ~((1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1);
  519. state >>= CPUMF_LCCTL_ENABLE_SHIFT;
  520. if ((state & cpuhw->info.auth_ctl) != state)
  521. return -EPERM;
  522. cpuhw->flags &= ~PERF_EVENT_TXN;
  523. perf_pmu_enable(pmu);
  524. return 0;
  525. }
  526. /* Performance monitoring unit for s390x */
  527. static struct pmu cpumf_pmu = {
  528. .pmu_enable = cpumf_pmu_enable,
  529. .pmu_disable = cpumf_pmu_disable,
  530. .event_init = cpumf_pmu_event_init,
  531. .add = cpumf_pmu_add,
  532. .del = cpumf_pmu_del,
  533. .start = cpumf_pmu_start,
  534. .stop = cpumf_pmu_stop,
  535. .read = cpumf_pmu_read,
  536. .start_txn = cpumf_pmu_start_txn,
  537. .commit_txn = cpumf_pmu_commit_txn,
  538. .cancel_txn = cpumf_pmu_cancel_txn,
  539. };
  540. static int __cpuinit cpumf_pmu_notifier(struct notifier_block *self,
  541. unsigned long action, void *hcpu)
  542. {
  543. unsigned int cpu = (long) hcpu;
  544. int flags;
  545. switch (action & ~CPU_TASKS_FROZEN) {
  546. case CPU_ONLINE:
  547. flags = PMC_INIT;
  548. smp_call_function_single(cpu, setup_pmc_cpu, &flags, 1);
  549. break;
  550. case CPU_DOWN_PREPARE:
  551. flags = PMC_RELEASE;
  552. smp_call_function_single(cpu, setup_pmc_cpu, &flags, 1);
  553. break;
  554. default:
  555. break;
  556. }
  557. return NOTIFY_OK;
  558. }
  559. static int __init cpumf_pmu_init(void)
  560. {
  561. int rc;
  562. if (!cpum_cf_avail())
  563. return -ENODEV;
  564. /* clear bit 15 of cr0 to unauthorize problem-state to
  565. * extract measurement counters */
  566. ctl_clear_bit(0, 48);
  567. /* register handler for measurement-alert interruptions */
  568. rc = register_external_interrupt(0x1407, cpumf_measurement_alert);
  569. if (rc) {
  570. pr_err("Registering for CPU-measurement alerts "
  571. "failed with rc=%i\n", rc);
  572. goto out;
  573. }
  574. rc = perf_pmu_register(&cpumf_pmu, "cpum_cf", PERF_TYPE_RAW);
  575. if (rc) {
  576. pr_err("Registering the cpum_cf PMU failed with rc=%i\n", rc);
  577. unregister_external_interrupt(0x1407, cpumf_measurement_alert);
  578. goto out;
  579. }
  580. perf_cpu_notifier(cpumf_pmu_notifier);
  581. out:
  582. return rc;
  583. }
  584. early_initcall(cpumf_pmu_init);