perf_pai_crypto.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Performance event support - Processor Activity Instrumentation Facility
  4. *
  5. * Copyright IBM Corp. 2022
  6. * Author(s): Thomas Richter <tmricht@linux.ibm.com>
  7. */
  8. #define KMSG_COMPONENT "pai_crypto"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/kernel_stat.h>
  12. #include <linux/percpu.h>
  13. #include <linux/notifier.h>
  14. #include <linux/init.h>
  15. #include <linux/export.h>
  16. #include <linux/io.h>
  17. #include <linux/perf_event.h>
  18. #include <asm/ctlreg.h>
  19. #include <asm/pai.h>
  20. #include <asm/debug.h>
  21. static debug_info_t *cfm_dbg;
  22. static unsigned int paicrypt_cnt; /* Size of the mapped counter sets */
  23. /* extracted with QPACI instruction */
  24. DEFINE_STATIC_KEY_FALSE(pai_key);
  25. struct pai_userdata {
  26. u16 num;
  27. u64 value;
  28. } __packed;
  29. struct paicrypt_map {
  30. unsigned long *page; /* Page for CPU to store counters */
  31. struct pai_userdata *save; /* Page to store no-zero counters */
  32. unsigned int active_events; /* # of PAI crypto users */
  33. refcount_t refcnt; /* Reference count mapped buffers */
  34. struct perf_event *event; /* Perf event for sampling */
  35. struct list_head syswide_list; /* List system-wide sampling events */
  36. };
  37. struct paicrypt_mapptr {
  38. struct paicrypt_map *mapptr;
  39. };
  40. static struct paicrypt_root { /* Anchor to per CPU data */
  41. refcount_t refcnt; /* Overall active events */
  42. struct paicrypt_mapptr __percpu *mapptr;
  43. } paicrypt_root;
  44. /* Free per CPU data when the last event is removed. */
  45. static void paicrypt_root_free(void)
  46. {
  47. if (refcount_dec_and_test(&paicrypt_root.refcnt)) {
  48. free_percpu(paicrypt_root.mapptr);
  49. paicrypt_root.mapptr = NULL;
  50. }
  51. debug_sprintf_event(cfm_dbg, 5, "%s root.refcount %d\n", __func__,
  52. refcount_read(&paicrypt_root.refcnt));
  53. }
  54. /*
  55. * On initialization of first event also allocate per CPU data dynamically.
  56. * Start with an array of pointers, the array size is the maximum number of
  57. * CPUs possible, which might be larger than the number of CPUs currently
  58. * online.
  59. */
  60. static int paicrypt_root_alloc(void)
  61. {
  62. if (!refcount_inc_not_zero(&paicrypt_root.refcnt)) {
  63. /* The memory is already zeroed. */
  64. paicrypt_root.mapptr = alloc_percpu(struct paicrypt_mapptr);
  65. if (!paicrypt_root.mapptr)
  66. return -ENOMEM;
  67. refcount_set(&paicrypt_root.refcnt, 1);
  68. }
  69. return 0;
  70. }
  71. /* Release the PMU if event is the last perf event */
  72. static DEFINE_MUTEX(pai_reserve_mutex);
  73. /* Adjust usage counters and remove allocated memory when all users are
  74. * gone.
  75. */
  76. static void paicrypt_event_destroy_cpu(struct perf_event *event, int cpu)
  77. {
  78. struct paicrypt_mapptr *mp = per_cpu_ptr(paicrypt_root.mapptr, cpu);
  79. struct paicrypt_map *cpump = mp->mapptr;
  80. mutex_lock(&pai_reserve_mutex);
  81. debug_sprintf_event(cfm_dbg, 5, "%s event %#llx cpu %d users %d "
  82. "refcnt %u\n", __func__, event->attr.config,
  83. event->cpu, cpump->active_events,
  84. refcount_read(&cpump->refcnt));
  85. if (refcount_dec_and_test(&cpump->refcnt)) {
  86. debug_sprintf_event(cfm_dbg, 4, "%s page %#lx save %p\n",
  87. __func__, (unsigned long)cpump->page,
  88. cpump->save);
  89. free_page((unsigned long)cpump->page);
  90. kvfree(cpump->save);
  91. kfree(cpump);
  92. mp->mapptr = NULL;
  93. }
  94. paicrypt_root_free();
  95. mutex_unlock(&pai_reserve_mutex);
  96. }
  97. static void paicrypt_event_destroy(struct perf_event *event)
  98. {
  99. int cpu;
  100. static_branch_dec(&pai_key);
  101. free_page(PAI_SAVE_AREA(event));
  102. if (event->cpu == -1) {
  103. struct cpumask *mask = PAI_CPU_MASK(event);
  104. for_each_cpu(cpu, mask)
  105. paicrypt_event_destroy_cpu(event, cpu);
  106. kfree(mask);
  107. } else {
  108. paicrypt_event_destroy_cpu(event, event->cpu);
  109. }
  110. }
  111. static u64 paicrypt_getctr(unsigned long *page, int nr, bool kernel)
  112. {
  113. if (kernel)
  114. nr += PAI_CRYPTO_MAXCTR;
  115. return page[nr];
  116. }
  117. /* Read the counter values. Return value from location in CMP. For event
  118. * CRYPTO_ALL sum up all events.
  119. */
  120. static u64 paicrypt_getdata(struct perf_event *event, bool kernel)
  121. {
  122. struct paicrypt_mapptr *mp = this_cpu_ptr(paicrypt_root.mapptr);
  123. struct paicrypt_map *cpump = mp->mapptr;
  124. u64 sum = 0;
  125. int i;
  126. if (event->attr.config != PAI_CRYPTO_BASE) {
  127. return paicrypt_getctr(cpump->page,
  128. event->attr.config - PAI_CRYPTO_BASE,
  129. kernel);
  130. }
  131. for (i = 1; i <= paicrypt_cnt; i++) {
  132. u64 val = paicrypt_getctr(cpump->page, i, kernel);
  133. if (!val)
  134. continue;
  135. sum += val;
  136. }
  137. return sum;
  138. }
  139. static u64 paicrypt_getall(struct perf_event *event)
  140. {
  141. u64 sum = 0;
  142. if (!event->attr.exclude_kernel)
  143. sum += paicrypt_getdata(event, true);
  144. if (!event->attr.exclude_user)
  145. sum += paicrypt_getdata(event, false);
  146. return sum;
  147. }
  148. /* Check concurrent access of counting and sampling for crypto events.
  149. * This function is called in process context and it is save to block.
  150. * When the event initialization functions fails, no other call back will
  151. * be invoked.
  152. *
  153. * Allocate the memory for the event.
  154. */
  155. static struct paicrypt_map *paicrypt_busy(struct perf_event *event, int cpu)
  156. {
  157. struct paicrypt_map *cpump = NULL;
  158. struct paicrypt_mapptr *mp;
  159. int rc;
  160. mutex_lock(&pai_reserve_mutex);
  161. /* Allocate root node */
  162. rc = paicrypt_root_alloc();
  163. if (rc)
  164. goto unlock;
  165. /* Allocate node for this event */
  166. mp = per_cpu_ptr(paicrypt_root.mapptr, cpu);
  167. cpump = mp->mapptr;
  168. if (!cpump) { /* Paicrypt_map allocated? */
  169. cpump = kzalloc(sizeof(*cpump), GFP_KERNEL);
  170. if (!cpump) {
  171. rc = -ENOMEM;
  172. goto free_root;
  173. }
  174. INIT_LIST_HEAD(&cpump->syswide_list);
  175. }
  176. /* Allocate memory for counter page and counter extraction.
  177. * Only the first counting event has to allocate a page.
  178. */
  179. if (cpump->page) {
  180. refcount_inc(&cpump->refcnt);
  181. goto unlock;
  182. }
  183. rc = -ENOMEM;
  184. cpump->page = (unsigned long *)get_zeroed_page(GFP_KERNEL);
  185. if (!cpump->page)
  186. goto free_paicrypt_map;
  187. cpump->save = kvmalloc_array(paicrypt_cnt + 1,
  188. sizeof(struct pai_userdata), GFP_KERNEL);
  189. if (!cpump->save) {
  190. free_page((unsigned long)cpump->page);
  191. cpump->page = NULL;
  192. goto free_paicrypt_map;
  193. }
  194. /* Set mode and reference count */
  195. rc = 0;
  196. refcount_set(&cpump->refcnt, 1);
  197. mp->mapptr = cpump;
  198. debug_sprintf_event(cfm_dbg, 5, "%s users %d refcnt %u page %#lx "
  199. "save %p rc %d\n", __func__, cpump->active_events,
  200. refcount_read(&cpump->refcnt),
  201. (unsigned long)cpump->page, cpump->save, rc);
  202. goto unlock;
  203. free_paicrypt_map:
  204. /* Undo memory allocation */
  205. kfree(cpump);
  206. mp->mapptr = NULL;
  207. free_root:
  208. paicrypt_root_free();
  209. unlock:
  210. mutex_unlock(&pai_reserve_mutex);
  211. return rc ? ERR_PTR(rc) : cpump;
  212. }
  213. static int paicrypt_event_init_all(struct perf_event *event)
  214. {
  215. struct paicrypt_map *cpump;
  216. struct cpumask *maskptr;
  217. int cpu, rc = -ENOMEM;
  218. maskptr = kzalloc(sizeof(*maskptr), GFP_KERNEL);
  219. if (!maskptr)
  220. goto out;
  221. for_each_online_cpu(cpu) {
  222. cpump = paicrypt_busy(event, cpu);
  223. if (IS_ERR(cpump)) {
  224. for_each_cpu(cpu, maskptr)
  225. paicrypt_event_destroy_cpu(event, cpu);
  226. kfree(maskptr);
  227. rc = PTR_ERR(cpump);
  228. goto out;
  229. }
  230. cpumask_set_cpu(cpu, maskptr);
  231. }
  232. /*
  233. * On error all cpumask are freed and all events have been destroyed.
  234. * Save of which CPUs data structures have been allocated for.
  235. * Release them in paicrypt_event_destroy call back function
  236. * for this event.
  237. */
  238. PAI_CPU_MASK(event) = maskptr;
  239. rc = 0;
  240. out:
  241. return rc;
  242. }
  243. /* Might be called on different CPU than the one the event is intended for. */
  244. static int paicrypt_event_init(struct perf_event *event)
  245. {
  246. struct perf_event_attr *a = &event->attr;
  247. struct paicrypt_map *cpump;
  248. int rc = 0;
  249. /* PAI crypto PMU registered as PERF_TYPE_RAW, check event type */
  250. if (a->type != PERF_TYPE_RAW && event->pmu->type != a->type)
  251. return -ENOENT;
  252. /* PAI crypto event must be in valid range, try others if not */
  253. if (a->config < PAI_CRYPTO_BASE ||
  254. a->config > PAI_CRYPTO_BASE + paicrypt_cnt)
  255. return -ENOENT;
  256. /* Allow only CRYPTO_ALL for sampling */
  257. if (a->sample_period && a->config != PAI_CRYPTO_BASE)
  258. return -EINVAL;
  259. /* Get a page to store last counter values for sampling */
  260. if (a->sample_period) {
  261. PAI_SAVE_AREA(event) = get_zeroed_page(GFP_KERNEL);
  262. if (!PAI_SAVE_AREA(event)) {
  263. rc = -ENOMEM;
  264. goto out;
  265. }
  266. }
  267. if (event->cpu >= 0) {
  268. cpump = paicrypt_busy(event, event->cpu);
  269. if (IS_ERR(cpump))
  270. rc = PTR_ERR(cpump);
  271. } else {
  272. rc = paicrypt_event_init_all(event);
  273. }
  274. if (rc) {
  275. free_page(PAI_SAVE_AREA(event));
  276. goto out;
  277. }
  278. event->destroy = paicrypt_event_destroy;
  279. if (a->sample_period) {
  280. a->sample_period = 1;
  281. a->freq = 0;
  282. /* Register for paicrypt_sched_task() to be called */
  283. event->attach_state |= PERF_ATTACH_SCHED_CB;
  284. /* Add raw data which contain the memory mapped counters */
  285. a->sample_type |= PERF_SAMPLE_RAW;
  286. /* Turn off inheritance */
  287. a->inherit = 0;
  288. }
  289. static_branch_inc(&pai_key);
  290. out:
  291. return rc;
  292. }
  293. static void paicrypt_read(struct perf_event *event)
  294. {
  295. u64 prev, new, delta;
  296. prev = local64_read(&event->hw.prev_count);
  297. new = paicrypt_getall(event);
  298. local64_set(&event->hw.prev_count, new);
  299. delta = (prev <= new) ? new - prev
  300. : (-1ULL - prev) + new + 1; /* overflow */
  301. local64_add(delta, &event->count);
  302. }
  303. static void paicrypt_start(struct perf_event *event, int flags)
  304. {
  305. struct paicrypt_mapptr *mp = this_cpu_ptr(paicrypt_root.mapptr);
  306. struct paicrypt_map *cpump = mp->mapptr;
  307. u64 sum;
  308. if (!event->attr.sample_period) { /* Counting */
  309. sum = paicrypt_getall(event); /* Get current value */
  310. local64_set(&event->hw.prev_count, sum);
  311. } else { /* Sampling */
  312. memcpy((void *)PAI_SAVE_AREA(event), cpump->page, PAGE_SIZE);
  313. /* Enable context switch callback for system-wide sampling */
  314. if (!(event->attach_state & PERF_ATTACH_TASK)) {
  315. list_add_tail(PAI_SWLIST(event), &cpump->syswide_list);
  316. perf_sched_cb_inc(event->pmu);
  317. } else {
  318. cpump->event = event;
  319. }
  320. }
  321. }
  322. static int paicrypt_add(struct perf_event *event, int flags)
  323. {
  324. struct paicrypt_mapptr *mp = this_cpu_ptr(paicrypt_root.mapptr);
  325. struct paicrypt_map *cpump = mp->mapptr;
  326. unsigned long ccd;
  327. if (++cpump->active_events == 1) {
  328. ccd = virt_to_phys(cpump->page) | PAI_CRYPTO_KERNEL_OFFSET;
  329. WRITE_ONCE(get_lowcore()->ccd, ccd);
  330. local_ctl_set_bit(0, CR0_CRYPTOGRAPHY_COUNTER_BIT);
  331. }
  332. if (flags & PERF_EF_START)
  333. paicrypt_start(event, PERF_EF_RELOAD);
  334. event->hw.state = 0;
  335. return 0;
  336. }
  337. static void paicrypt_have_sample(struct perf_event *, struct paicrypt_map *);
  338. static void paicrypt_stop(struct perf_event *event, int flags)
  339. {
  340. struct paicrypt_mapptr *mp = this_cpu_ptr(paicrypt_root.mapptr);
  341. struct paicrypt_map *cpump = mp->mapptr;
  342. if (!event->attr.sample_period) { /* Counting */
  343. paicrypt_read(event);
  344. } else { /* Sampling */
  345. if (!(event->attach_state & PERF_ATTACH_TASK)) {
  346. perf_sched_cb_dec(event->pmu);
  347. list_del(PAI_SWLIST(event));
  348. } else {
  349. paicrypt_have_sample(event, cpump);
  350. cpump->event = NULL;
  351. }
  352. }
  353. event->hw.state = PERF_HES_STOPPED;
  354. }
  355. static void paicrypt_del(struct perf_event *event, int flags)
  356. {
  357. struct paicrypt_mapptr *mp = this_cpu_ptr(paicrypt_root.mapptr);
  358. struct paicrypt_map *cpump = mp->mapptr;
  359. paicrypt_stop(event, PERF_EF_UPDATE);
  360. if (--cpump->active_events == 0) {
  361. local_ctl_clear_bit(0, CR0_CRYPTOGRAPHY_COUNTER_BIT);
  362. WRITE_ONCE(get_lowcore()->ccd, 0);
  363. }
  364. }
  365. /* Create raw data and save it in buffer. Calculate the delta for each
  366. * counter between this invocation and the last invocation.
  367. * Returns number of bytes copied.
  368. * Saves only entries with positive counter difference of the form
  369. * 2 bytes: Number of counter
  370. * 8 bytes: Value of counter
  371. */
  372. static size_t paicrypt_copy(struct pai_userdata *userdata, unsigned long *page,
  373. unsigned long *page_old, bool exclude_user,
  374. bool exclude_kernel)
  375. {
  376. int i, outidx = 0;
  377. for (i = 1; i <= paicrypt_cnt; i++) {
  378. u64 val = 0, val_old = 0;
  379. if (!exclude_kernel) {
  380. val += paicrypt_getctr(page, i, true);
  381. val_old += paicrypt_getctr(page_old, i, true);
  382. }
  383. if (!exclude_user) {
  384. val += paicrypt_getctr(page, i, false);
  385. val_old += paicrypt_getctr(page_old, i, false);
  386. }
  387. if (val >= val_old)
  388. val -= val_old;
  389. else
  390. val = (~0ULL - val_old) + val + 1;
  391. if (val) {
  392. userdata[outidx].num = i;
  393. userdata[outidx].value = val;
  394. outidx++;
  395. }
  396. }
  397. return outidx * sizeof(struct pai_userdata);
  398. }
  399. static int paicrypt_push_sample(size_t rawsize, struct paicrypt_map *cpump,
  400. struct perf_event *event)
  401. {
  402. struct perf_sample_data data;
  403. struct perf_raw_record raw;
  404. struct pt_regs regs;
  405. int overflow;
  406. /* Setup perf sample */
  407. memset(&regs, 0, sizeof(regs));
  408. memset(&raw, 0, sizeof(raw));
  409. memset(&data, 0, sizeof(data));
  410. perf_sample_data_init(&data, 0, event->hw.last_period);
  411. if (event->attr.sample_type & PERF_SAMPLE_TID) {
  412. data.tid_entry.pid = task_tgid_nr(current);
  413. data.tid_entry.tid = task_pid_nr(current);
  414. }
  415. if (event->attr.sample_type & PERF_SAMPLE_TIME)
  416. data.time = event->clock();
  417. if (event->attr.sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
  418. data.id = event->id;
  419. if (event->attr.sample_type & PERF_SAMPLE_CPU) {
  420. data.cpu_entry.cpu = smp_processor_id();
  421. data.cpu_entry.reserved = 0;
  422. }
  423. if (event->attr.sample_type & PERF_SAMPLE_RAW) {
  424. raw.frag.size = rawsize;
  425. raw.frag.data = cpump->save;
  426. perf_sample_save_raw_data(&data, event, &raw);
  427. }
  428. overflow = perf_event_overflow(event, &data, &regs);
  429. perf_event_update_userpage(event);
  430. /* Save crypto counter lowcore page after reading event data. */
  431. memcpy((void *)PAI_SAVE_AREA(event), cpump->page, PAGE_SIZE);
  432. return overflow;
  433. }
  434. /* Check if there is data to be saved on schedule out of a task. */
  435. static void paicrypt_have_sample(struct perf_event *event,
  436. struct paicrypt_map *cpump)
  437. {
  438. size_t rawsize;
  439. if (!event) /* No event active */
  440. return;
  441. rawsize = paicrypt_copy(cpump->save, cpump->page,
  442. (unsigned long *)PAI_SAVE_AREA(event),
  443. event->attr.exclude_user,
  444. event->attr.exclude_kernel);
  445. if (rawsize) /* No incremented counters */
  446. paicrypt_push_sample(rawsize, cpump, event);
  447. }
  448. /* Check if there is data to be saved on schedule out of a task. */
  449. static void paicrypt_have_samples(void)
  450. {
  451. struct paicrypt_mapptr *mp = this_cpu_ptr(paicrypt_root.mapptr);
  452. struct paicrypt_map *cpump = mp->mapptr;
  453. struct perf_event *event;
  454. list_for_each_entry(event, &cpump->syswide_list, hw.tp_list)
  455. paicrypt_have_sample(event, cpump);
  456. }
  457. /* Called on schedule-in and schedule-out. No access to event structure,
  458. * but for sampling only event CRYPTO_ALL is allowed.
  459. */
  460. static void paicrypt_sched_task(struct perf_event_pmu_context *pmu_ctx, bool sched_in)
  461. {
  462. /* We started with a clean page on event installation. So read out
  463. * results on schedule_out and if page was dirty, save old values.
  464. */
  465. if (!sched_in)
  466. paicrypt_have_samples();
  467. }
  468. /* Attribute definitions for paicrypt interface. As with other CPU
  469. * Measurement Facilities, there is one attribute per mapped counter.
  470. * The number of mapped counters may vary per machine generation. Use
  471. * the QUERY PROCESSOR ACTIVITY COUNTER INFORMATION (QPACI) instruction
  472. * to determine the number of mapped counters. The instructions returns
  473. * a positive number, which is the highest number of supported counters.
  474. * All counters less than this number are also supported, there are no
  475. * holes. A returned number of zero means no support for mapped counters.
  476. *
  477. * The identification of the counter is a unique number. The chosen range
  478. * is 0x1000 + offset in mapped kernel page.
  479. * All CPU Measurement Facility counters identifiers must be unique and
  480. * the numbers from 0 to 496 are already used for the CPU Measurement
  481. * Counter facility. Numbers 0xb0000, 0xbc000 and 0xbd000 are already
  482. * used for the CPU Measurement Sampling facility.
  483. */
  484. PMU_FORMAT_ATTR(event, "config:0-63");
  485. static struct attribute *paicrypt_format_attr[] = {
  486. &format_attr_event.attr,
  487. NULL,
  488. };
  489. static struct attribute_group paicrypt_events_group = {
  490. .name = "events",
  491. .attrs = NULL /* Filled in attr_event_init() */
  492. };
  493. static struct attribute_group paicrypt_format_group = {
  494. .name = "format",
  495. .attrs = paicrypt_format_attr,
  496. };
  497. static const struct attribute_group *paicrypt_attr_groups[] = {
  498. &paicrypt_events_group,
  499. &paicrypt_format_group,
  500. NULL,
  501. };
  502. /* Performance monitoring unit for mapped counters */
  503. static struct pmu paicrypt = {
  504. .task_ctx_nr = perf_hw_context,
  505. .event_init = paicrypt_event_init,
  506. .add = paicrypt_add,
  507. .del = paicrypt_del,
  508. .start = paicrypt_start,
  509. .stop = paicrypt_stop,
  510. .read = paicrypt_read,
  511. .sched_task = paicrypt_sched_task,
  512. .attr_groups = paicrypt_attr_groups
  513. };
  514. /* List of symbolic PAI counter names. */
  515. static const char * const paicrypt_ctrnames[] = {
  516. [0] = "CRYPTO_ALL",
  517. [1] = "KM_DEA",
  518. [2] = "KM_TDEA_128",
  519. [3] = "KM_TDEA_192",
  520. [4] = "KM_ENCRYPTED_DEA",
  521. [5] = "KM_ENCRYPTED_TDEA_128",
  522. [6] = "KM_ENCRYPTED_TDEA_192",
  523. [7] = "KM_AES_128",
  524. [8] = "KM_AES_192",
  525. [9] = "KM_AES_256",
  526. [10] = "KM_ENCRYPTED_AES_128",
  527. [11] = "KM_ENCRYPTED_AES_192",
  528. [12] = "KM_ENCRYPTED_AES_256",
  529. [13] = "KM_XTS_AES_128",
  530. [14] = "KM_XTS_AES_256",
  531. [15] = "KM_XTS_ENCRYPTED_AES_128",
  532. [16] = "KM_XTS_ENCRYPTED_AES_256",
  533. [17] = "KMC_DEA",
  534. [18] = "KMC_TDEA_128",
  535. [19] = "KMC_TDEA_192",
  536. [20] = "KMC_ENCRYPTED_DEA",
  537. [21] = "KMC_ENCRYPTED_TDEA_128",
  538. [22] = "KMC_ENCRYPTED_TDEA_192",
  539. [23] = "KMC_AES_128",
  540. [24] = "KMC_AES_192",
  541. [25] = "KMC_AES_256",
  542. [26] = "KMC_ENCRYPTED_AES_128",
  543. [27] = "KMC_ENCRYPTED_AES_192",
  544. [28] = "KMC_ENCRYPTED_AES_256",
  545. [29] = "KMC_PRNG",
  546. [30] = "KMA_GCM_AES_128",
  547. [31] = "KMA_GCM_AES_192",
  548. [32] = "KMA_GCM_AES_256",
  549. [33] = "KMA_GCM_ENCRYPTED_AES_128",
  550. [34] = "KMA_GCM_ENCRYPTED_AES_192",
  551. [35] = "KMA_GCM_ENCRYPTED_AES_256",
  552. [36] = "KMF_DEA",
  553. [37] = "KMF_TDEA_128",
  554. [38] = "KMF_TDEA_192",
  555. [39] = "KMF_ENCRYPTED_DEA",
  556. [40] = "KMF_ENCRYPTED_TDEA_128",
  557. [41] = "KMF_ENCRYPTED_TDEA_192",
  558. [42] = "KMF_AES_128",
  559. [43] = "KMF_AES_192",
  560. [44] = "KMF_AES_256",
  561. [45] = "KMF_ENCRYPTED_AES_128",
  562. [46] = "KMF_ENCRYPTED_AES_192",
  563. [47] = "KMF_ENCRYPTED_AES_256",
  564. [48] = "KMCTR_DEA",
  565. [49] = "KMCTR_TDEA_128",
  566. [50] = "KMCTR_TDEA_192",
  567. [51] = "KMCTR_ENCRYPTED_DEA",
  568. [52] = "KMCTR_ENCRYPTED_TDEA_128",
  569. [53] = "KMCTR_ENCRYPTED_TDEA_192",
  570. [54] = "KMCTR_AES_128",
  571. [55] = "KMCTR_AES_192",
  572. [56] = "KMCTR_AES_256",
  573. [57] = "KMCTR_ENCRYPTED_AES_128",
  574. [58] = "KMCTR_ENCRYPTED_AES_192",
  575. [59] = "KMCTR_ENCRYPTED_AES_256",
  576. [60] = "KMO_DEA",
  577. [61] = "KMO_TDEA_128",
  578. [62] = "KMO_TDEA_192",
  579. [63] = "KMO_ENCRYPTED_DEA",
  580. [64] = "KMO_ENCRYPTED_TDEA_128",
  581. [65] = "KMO_ENCRYPTED_TDEA_192",
  582. [66] = "KMO_AES_128",
  583. [67] = "KMO_AES_192",
  584. [68] = "KMO_AES_256",
  585. [69] = "KMO_ENCRYPTED_AES_128",
  586. [70] = "KMO_ENCRYPTED_AES_192",
  587. [71] = "KMO_ENCRYPTED_AES_256",
  588. [72] = "KIMD_SHA_1",
  589. [73] = "KIMD_SHA_256",
  590. [74] = "KIMD_SHA_512",
  591. [75] = "KIMD_SHA3_224",
  592. [76] = "KIMD_SHA3_256",
  593. [77] = "KIMD_SHA3_384",
  594. [78] = "KIMD_SHA3_512",
  595. [79] = "KIMD_SHAKE_128",
  596. [80] = "KIMD_SHAKE_256",
  597. [81] = "KIMD_GHASH",
  598. [82] = "KLMD_SHA_1",
  599. [83] = "KLMD_SHA_256",
  600. [84] = "KLMD_SHA_512",
  601. [85] = "KLMD_SHA3_224",
  602. [86] = "KLMD_SHA3_256",
  603. [87] = "KLMD_SHA3_384",
  604. [88] = "KLMD_SHA3_512",
  605. [89] = "KLMD_SHAKE_128",
  606. [90] = "KLMD_SHAKE_256",
  607. [91] = "KMAC_DEA",
  608. [92] = "KMAC_TDEA_128",
  609. [93] = "KMAC_TDEA_192",
  610. [94] = "KMAC_ENCRYPTED_DEA",
  611. [95] = "KMAC_ENCRYPTED_TDEA_128",
  612. [96] = "KMAC_ENCRYPTED_TDEA_192",
  613. [97] = "KMAC_AES_128",
  614. [98] = "KMAC_AES_192",
  615. [99] = "KMAC_AES_256",
  616. [100] = "KMAC_ENCRYPTED_AES_128",
  617. [101] = "KMAC_ENCRYPTED_AES_192",
  618. [102] = "KMAC_ENCRYPTED_AES_256",
  619. [103] = "PCC_COMPUTE_LAST_BLOCK_CMAC_USING_DEA",
  620. [104] = "PCC_COMPUTE_LAST_BLOCK_CMAC_USING_TDEA_128",
  621. [105] = "PCC_COMPUTE_LAST_BLOCK_CMAC_USING_TDEA_192",
  622. [106] = "PCC_COMPUTE_LAST_BLOCK_CMAC_USING_ENCRYPTED_DEA",
  623. [107] = "PCC_COMPUTE_LAST_BLOCK_CMAC_USING_ENCRYPTED_TDEA_128",
  624. [108] = "PCC_COMPUTE_LAST_BLOCK_CMAC_USING_ENCRYPTED_TDEA_192",
  625. [109] = "PCC_COMPUTE_LAST_BLOCK_CMAC_USING_AES_128",
  626. [110] = "PCC_COMPUTE_LAST_BLOCK_CMAC_USING_AES_192",
  627. [111] = "PCC_COMPUTE_LAST_BLOCK_CMAC_USING_AES_256",
  628. [112] = "PCC_COMPUTE_LAST_BLOCK_CMAC_USING_ENCRYPTED_AES_128",
  629. [113] = "PCC_COMPUTE_LAST_BLOCK_CMAC_USING_ENCRYPTED_AES_192",
  630. [114] = "PCC_COMPUTE_LAST_BLOCK_CMAC_USING_ENCRYPTED_AES_256A",
  631. [115] = "PCC_COMPUTE_XTS_PARAMETER_USING_AES_128",
  632. [116] = "PCC_COMPUTE_XTS_PARAMETER_USING_AES_256",
  633. [117] = "PCC_COMPUTE_XTS_PARAMETER_USING_ENCRYPTED_AES_128",
  634. [118] = "PCC_COMPUTE_XTS_PARAMETER_USING_ENCRYPTED_AES_256",
  635. [119] = "PCC_SCALAR_MULTIPLY_P256",
  636. [120] = "PCC_SCALAR_MULTIPLY_P384",
  637. [121] = "PCC_SCALAR_MULTIPLY_P521",
  638. [122] = "PCC_SCALAR_MULTIPLY_ED25519",
  639. [123] = "PCC_SCALAR_MULTIPLY_ED448",
  640. [124] = "PCC_SCALAR_MULTIPLY_X25519",
  641. [125] = "PCC_SCALAR_MULTIPLY_X448",
  642. [126] = "PRNO_SHA_512_DRNG",
  643. [127] = "PRNO_TRNG_QUERY_RAW_TO_CONDITIONED_RATIO",
  644. [128] = "PRNO_TRNG",
  645. [129] = "KDSA_ECDSA_VERIFY_P256",
  646. [130] = "KDSA_ECDSA_VERIFY_P384",
  647. [131] = "KDSA_ECDSA_VERIFY_P521",
  648. [132] = "KDSA_ECDSA_SIGN_P256",
  649. [133] = "KDSA_ECDSA_SIGN_P384",
  650. [134] = "KDSA_ECDSA_SIGN_P521",
  651. [135] = "KDSA_ENCRYPTED_ECDSA_SIGN_P256",
  652. [136] = "KDSA_ENCRYPTED_ECDSA_SIGN_P384",
  653. [137] = "KDSA_ENCRYPTED_ECDSA_SIGN_P521",
  654. [138] = "KDSA_EDDSA_VERIFY_ED25519",
  655. [139] = "KDSA_EDDSA_VERIFY_ED448",
  656. [140] = "KDSA_EDDSA_SIGN_ED25519",
  657. [141] = "KDSA_EDDSA_SIGN_ED448",
  658. [142] = "KDSA_ENCRYPTED_EDDSA_SIGN_ED25519",
  659. [143] = "KDSA_ENCRYPTED_EDDSA_SIGN_ED448",
  660. [144] = "PCKMO_ENCRYPT_DEA_KEY",
  661. [145] = "PCKMO_ENCRYPT_TDEA_128_KEY",
  662. [146] = "PCKMO_ENCRYPT_TDEA_192_KEY",
  663. [147] = "PCKMO_ENCRYPT_AES_128_KEY",
  664. [148] = "PCKMO_ENCRYPT_AES_192_KEY",
  665. [149] = "PCKMO_ENCRYPT_AES_256_KEY",
  666. [150] = "PCKMO_ENCRYPT_ECC_P256_KEY",
  667. [151] = "PCKMO_ENCRYPT_ECC_P384_KEY",
  668. [152] = "PCKMO_ENCRYPT_ECC_P521_KEY",
  669. [153] = "PCKMO_ENCRYPT_ECC_ED25519_KEY",
  670. [154] = "PCKMO_ENCRYPT_ECC_ED448_KEY",
  671. [155] = "IBM_RESERVED_155",
  672. [156] = "IBM_RESERVED_156",
  673. [157] = "KM_FULL_XTS_AES_128",
  674. [158] = "KM_FULL_XTS_AES_256",
  675. [159] = "KM_FULL_XTS_ENCRYPTED_AES_128",
  676. [160] = "KM_FULL_XTS_ENCRYPTED_AES_256",
  677. [161] = "KMAC_HMAC_SHA_224",
  678. [162] = "KMAC_HMAC_SHA_256",
  679. [163] = "KMAC_HMAC_SHA_384",
  680. [164] = "KMAC_HMAC_SHA_512",
  681. [165] = "KMAC_HMAC_ENCRYPTED_SHA_224",
  682. [166] = "KMAC_HMAC_ENCRYPTED_SHA_256",
  683. [167] = "KMAC_HMAC_ENCRYPTED_SHA_384",
  684. [168] = "KMAC_HMAC_ENCRYPTED_SHA_512",
  685. [169] = "PCKMO_ENCRYPT_HMAC_512_KEY",
  686. [170] = "PCKMO_ENCRYPT_HMAC_1024_KEY",
  687. [171] = "PCKMO_ENCRYPT_AES_XTS_128",
  688. [172] = "PCKMO_ENCRYPT_AES_XTS_256",
  689. };
  690. static void __init attr_event_free(struct attribute **attrs, int num)
  691. {
  692. struct perf_pmu_events_attr *pa;
  693. int i;
  694. for (i = 0; i < num; i++) {
  695. struct device_attribute *dap;
  696. dap = container_of(attrs[i], struct device_attribute, attr);
  697. pa = container_of(dap, struct perf_pmu_events_attr, attr);
  698. kfree(pa);
  699. }
  700. kfree(attrs);
  701. }
  702. static int __init attr_event_init_one(struct attribute **attrs, int num)
  703. {
  704. struct perf_pmu_events_attr *pa;
  705. /* Index larger than array_size, no counter name available */
  706. if (num >= ARRAY_SIZE(paicrypt_ctrnames)) {
  707. attrs[num] = NULL;
  708. return 0;
  709. }
  710. pa = kzalloc(sizeof(*pa), GFP_KERNEL);
  711. if (!pa)
  712. return -ENOMEM;
  713. sysfs_attr_init(&pa->attr.attr);
  714. pa->id = PAI_CRYPTO_BASE + num;
  715. pa->attr.attr.name = paicrypt_ctrnames[num];
  716. pa->attr.attr.mode = 0444;
  717. pa->attr.show = cpumf_events_sysfs_show;
  718. pa->attr.store = NULL;
  719. attrs[num] = &pa->attr.attr;
  720. return 0;
  721. }
  722. /* Create PMU sysfs event attributes on the fly. */
  723. static int __init attr_event_init(void)
  724. {
  725. struct attribute **attrs;
  726. int ret, i;
  727. attrs = kmalloc_array(paicrypt_cnt + 2, sizeof(*attrs), GFP_KERNEL);
  728. if (!attrs)
  729. return -ENOMEM;
  730. for (i = 0; i <= paicrypt_cnt; i++) {
  731. ret = attr_event_init_one(attrs, i);
  732. if (ret) {
  733. attr_event_free(attrs, i);
  734. return ret;
  735. }
  736. }
  737. attrs[i] = NULL;
  738. paicrypt_events_group.attrs = attrs;
  739. return 0;
  740. }
  741. static int __init paicrypt_init(void)
  742. {
  743. struct qpaci_info_block ib;
  744. int rc;
  745. if (!test_facility(196))
  746. return 0;
  747. qpaci(&ib);
  748. paicrypt_cnt = ib.num_cc;
  749. if (paicrypt_cnt == 0)
  750. return 0;
  751. if (paicrypt_cnt >= PAI_CRYPTO_MAXCTR) {
  752. pr_err("Too many PMU pai_crypto counters %d\n", paicrypt_cnt);
  753. return -E2BIG;
  754. }
  755. rc = attr_event_init(); /* Export known PAI crypto events */
  756. if (rc) {
  757. pr_err("Creation of PMU pai_crypto /sysfs failed\n");
  758. return rc;
  759. }
  760. /* Setup s390dbf facility */
  761. cfm_dbg = debug_register(KMSG_COMPONENT, 2, 256, 128);
  762. if (!cfm_dbg) {
  763. pr_err("Registration of s390dbf pai_crypto failed\n");
  764. return -ENOMEM;
  765. }
  766. debug_register_view(cfm_dbg, &debug_sprintf_view);
  767. rc = perf_pmu_register(&paicrypt, "pai_crypto", -1);
  768. if (rc) {
  769. pr_err("Registering the pai_crypto PMU failed with rc=%i\n",
  770. rc);
  771. debug_unregister_view(cfm_dbg, &debug_sprintf_view);
  772. debug_unregister(cfm_dbg);
  773. return rc;
  774. }
  775. return 0;
  776. }
  777. device_initcall(paicrypt_init);