perf_pai_ext.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Performance event support - Processor Activity Instrumentation Extension
  4. * Facility
  5. *
  6. * Copyright IBM Corp. 2022
  7. * Author(s): Thomas Richter <tmricht@linux.ibm.com>
  8. */
  9. #define KMSG_COMPONENT "pai_ext"
  10. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/kernel_stat.h>
  13. #include <linux/percpu.h>
  14. #include <linux/notifier.h>
  15. #include <linux/init.h>
  16. #include <linux/export.h>
  17. #include <linux/io.h>
  18. #include <linux/perf_event.h>
  19. #include <asm/ctlreg.h>
  20. #include <asm/pai.h>
  21. #include <asm/debug.h>
  22. #define PAIE1_CB_SZ 0x200 /* Size of PAIE1 control block */
  23. #define PAIE1_CTRBLOCK_SZ 0x400 /* Size of PAIE1 counter blocks */
  24. static debug_info_t *paiext_dbg;
  25. static unsigned int paiext_cnt; /* Extracted with QPACI instruction */
  26. struct pai_userdata {
  27. u16 num;
  28. u64 value;
  29. } __packed;
  30. /* Create the PAI extension 1 control block area.
  31. * The PAI extension control block 1 is pointed to by lowcore
  32. * address 0x1508 for each CPU. This control block is 512 bytes in size
  33. * and requires a 512 byte boundary alignment.
  34. */
  35. struct paiext_cb { /* PAI extension 1 control block */
  36. u64 header; /* Not used */
  37. u64 reserved1;
  38. u64 acc; /* Addr to analytics counter control block */
  39. u8 reserved2[488];
  40. } __packed;
  41. struct paiext_map {
  42. unsigned long *area; /* Area for CPU to store counters */
  43. struct pai_userdata *save; /* Area to store non-zero counters */
  44. unsigned int active_events; /* # of PAI Extension users */
  45. refcount_t refcnt;
  46. struct perf_event *event; /* Perf event for sampling */
  47. struct paiext_cb *paiext_cb; /* PAI extension control block area */
  48. struct list_head syswide_list; /* List system-wide sampling events */
  49. };
  50. struct paiext_mapptr {
  51. struct paiext_map *mapptr;
  52. };
  53. static struct paiext_root { /* Anchor to per CPU data */
  54. refcount_t refcnt; /* Overall active events */
  55. struct paiext_mapptr __percpu *mapptr;
  56. } paiext_root;
  57. /* Free per CPU data when the last event is removed. */
  58. static void paiext_root_free(void)
  59. {
  60. if (refcount_dec_and_test(&paiext_root.refcnt)) {
  61. free_percpu(paiext_root.mapptr);
  62. paiext_root.mapptr = NULL;
  63. }
  64. debug_sprintf_event(paiext_dbg, 5, "%s root.refcount %d\n", __func__,
  65. refcount_read(&paiext_root.refcnt));
  66. }
  67. /* On initialization of first event also allocate per CPU data dynamically.
  68. * Start with an array of pointers, the array size is the maximum number of
  69. * CPUs possible, which might be larger than the number of CPUs currently
  70. * online.
  71. */
  72. static int paiext_root_alloc(void)
  73. {
  74. if (!refcount_inc_not_zero(&paiext_root.refcnt)) {
  75. /* The memory is already zeroed. */
  76. paiext_root.mapptr = alloc_percpu(struct paiext_mapptr);
  77. if (!paiext_root.mapptr) {
  78. /* Returning without refcnt adjustment is ok. The
  79. * error code is handled by paiext_alloc() which
  80. * decrements refcnt when an event can not be
  81. * created.
  82. */
  83. return -ENOMEM;
  84. }
  85. refcount_set(&paiext_root.refcnt, 1);
  86. }
  87. return 0;
  88. }
  89. /* Protects against concurrent increment of sampler and counter member
  90. * increments at the same time and prohibits concurrent execution of
  91. * counting and sampling events.
  92. * Ensures that analytics counter block is deallocated only when the
  93. * sampling and counting on that cpu is zero.
  94. * For details see paiext_alloc().
  95. */
  96. static DEFINE_MUTEX(paiext_reserve_mutex);
  97. /* Free all memory allocated for event counting/sampling setup */
  98. static void paiext_free(struct paiext_mapptr *mp)
  99. {
  100. kfree(mp->mapptr->area);
  101. kfree(mp->mapptr->paiext_cb);
  102. kvfree(mp->mapptr->save);
  103. kfree(mp->mapptr);
  104. mp->mapptr = NULL;
  105. }
  106. /* Release the PMU if event is the last perf event */
  107. static void paiext_event_destroy_cpu(struct perf_event *event, int cpu)
  108. {
  109. struct paiext_mapptr *mp = per_cpu_ptr(paiext_root.mapptr, cpu);
  110. struct paiext_map *cpump = mp->mapptr;
  111. mutex_lock(&paiext_reserve_mutex);
  112. if (refcount_dec_and_test(&cpump->refcnt)) /* Last reference gone */
  113. paiext_free(mp);
  114. paiext_root_free();
  115. mutex_unlock(&paiext_reserve_mutex);
  116. }
  117. static void paiext_event_destroy(struct perf_event *event)
  118. {
  119. int cpu;
  120. free_page(PAI_SAVE_AREA(event));
  121. if (event->cpu == -1) {
  122. struct cpumask *mask = PAI_CPU_MASK(event);
  123. for_each_cpu(cpu, mask)
  124. paiext_event_destroy_cpu(event, cpu);
  125. kfree(mask);
  126. } else {
  127. paiext_event_destroy_cpu(event, event->cpu);
  128. }
  129. debug_sprintf_event(paiext_dbg, 4, "%s cpu %d\n", __func__,
  130. event->cpu);
  131. }
  132. /* Used to avoid races in checking concurrent access of counting and
  133. * sampling for pai_extension events.
  134. *
  135. * Only one instance of event pai_ext/NNPA_ALL/ for sampling is
  136. * allowed and when this event is running, no counting event is allowed.
  137. * Several counting events are allowed in parallel, but no sampling event
  138. * is allowed while one (or more) counting events are running.
  139. *
  140. * This function is called in process context and it is safe to block.
  141. * When the event initialization functions fails, no other call back will
  142. * be invoked.
  143. *
  144. * Allocate the memory for the event.
  145. */
  146. static int paiext_alloc_cpu(struct perf_event *event, int cpu)
  147. {
  148. struct paiext_mapptr *mp;
  149. struct paiext_map *cpump;
  150. int rc;
  151. mutex_lock(&paiext_reserve_mutex);
  152. rc = paiext_root_alloc();
  153. if (rc)
  154. goto unlock;
  155. mp = per_cpu_ptr(paiext_root.mapptr, cpu);
  156. cpump = mp->mapptr;
  157. if (!cpump) { /* Paiext_map allocated? */
  158. rc = -ENOMEM;
  159. cpump = kzalloc(sizeof(*cpump), GFP_KERNEL);
  160. if (!cpump)
  161. goto undo;
  162. /* Allocate memory for counter area and counter extraction.
  163. * These are
  164. * - a 512 byte block and requires 512 byte boundary alignment.
  165. * - a 1KB byte block and requires 1KB boundary alignment.
  166. * Only the first counting event has to allocate the area.
  167. *
  168. * Note: This works with commit 59bb47985c1d by default.
  169. * Backporting this to kernels without this commit might
  170. * need adjustment.
  171. */
  172. mp->mapptr = cpump;
  173. cpump->area = kzalloc(PAIE1_CTRBLOCK_SZ, GFP_KERNEL);
  174. cpump->paiext_cb = kzalloc(PAIE1_CB_SZ, GFP_KERNEL);
  175. cpump->save = kvmalloc_array(paiext_cnt + 1,
  176. sizeof(struct pai_userdata),
  177. GFP_KERNEL);
  178. if (!cpump->save || !cpump->area || !cpump->paiext_cb) {
  179. paiext_free(mp);
  180. goto undo;
  181. }
  182. INIT_LIST_HEAD(&cpump->syswide_list);
  183. refcount_set(&cpump->refcnt, 1);
  184. rc = 0;
  185. } else {
  186. refcount_inc(&cpump->refcnt);
  187. }
  188. undo:
  189. if (rc) {
  190. /* Error in allocation of event, decrement anchor. Since
  191. * the event in not created, its destroy() function is never
  192. * invoked. Adjust the reference counter for the anchor.
  193. */
  194. paiext_root_free();
  195. }
  196. unlock:
  197. mutex_unlock(&paiext_reserve_mutex);
  198. /* If rc is non-zero, no increment of counter/sampler was done. */
  199. return rc;
  200. }
  201. static int paiext_alloc(struct perf_event *event)
  202. {
  203. struct cpumask *maskptr;
  204. int cpu, rc = -ENOMEM;
  205. maskptr = kzalloc(sizeof(*maskptr), GFP_KERNEL);
  206. if (!maskptr)
  207. goto out;
  208. for_each_online_cpu(cpu) {
  209. rc = paiext_alloc_cpu(event, cpu);
  210. if (rc) {
  211. for_each_cpu(cpu, maskptr)
  212. paiext_event_destroy_cpu(event, cpu);
  213. kfree(maskptr);
  214. goto out;
  215. }
  216. cpumask_set_cpu(cpu, maskptr);
  217. }
  218. /*
  219. * On error all cpumask are freed and all events have been destroyed.
  220. * Save of which CPUs data structures have been allocated for.
  221. * Release them in paicrypt_event_destroy call back function
  222. * for this event.
  223. */
  224. PAI_CPU_MASK(event) = maskptr;
  225. rc = 0;
  226. out:
  227. return rc;
  228. }
  229. /* The PAI extension 1 control block supports up to 128 entries. Return
  230. * the index within PAIE1_CB given the event number. Also validate event
  231. * number.
  232. */
  233. static int paiext_event_valid(struct perf_event *event)
  234. {
  235. u64 cfg = event->attr.config;
  236. if (cfg >= PAI_NNPA_BASE && cfg <= PAI_NNPA_BASE + paiext_cnt) {
  237. /* Offset NNPA in paiext_cb */
  238. event->hw.config_base = offsetof(struct paiext_cb, acc);
  239. return 0;
  240. }
  241. return -ENOENT;
  242. }
  243. /* Might be called on different CPU than the one the event is intended for. */
  244. static int paiext_event_init(struct perf_event *event)
  245. {
  246. struct perf_event_attr *a = &event->attr;
  247. int rc;
  248. /* PMU pai_ext registered as PERF_TYPE_RAW, check event type */
  249. if (a->type != PERF_TYPE_RAW && event->pmu->type != a->type)
  250. return -ENOENT;
  251. /* PAI extension event must be valid and in supported range */
  252. rc = paiext_event_valid(event);
  253. if (rc)
  254. return rc;
  255. /* Allow only event NNPA_ALL for sampling. */
  256. if (a->sample_period && a->config != PAI_NNPA_BASE)
  257. return -EINVAL;
  258. /* Prohibit exclude_user event selection */
  259. if (a->exclude_user)
  260. return -EINVAL;
  261. /* Get a page to store last counter values for sampling */
  262. if (a->sample_period) {
  263. PAI_SAVE_AREA(event) = get_zeroed_page(GFP_KERNEL);
  264. if (!PAI_SAVE_AREA(event))
  265. return -ENOMEM;
  266. }
  267. if (event->cpu >= 0)
  268. rc = paiext_alloc_cpu(event, event->cpu);
  269. else
  270. rc = paiext_alloc(event);
  271. if (rc) {
  272. free_page(PAI_SAVE_AREA(event));
  273. return rc;
  274. }
  275. event->destroy = paiext_event_destroy;
  276. if (a->sample_period) {
  277. a->sample_period = 1;
  278. a->freq = 0;
  279. /* Register for paicrypt_sched_task() to be called */
  280. event->attach_state |= PERF_ATTACH_SCHED_CB;
  281. /* Add raw data which are the memory mapped counters */
  282. a->sample_type |= PERF_SAMPLE_RAW;
  283. /* Turn off inheritance */
  284. a->inherit = 0;
  285. }
  286. return 0;
  287. }
  288. static u64 paiext_getctr(unsigned long *area, int nr)
  289. {
  290. return area[nr];
  291. }
  292. /* Read the counter values. Return value from location in buffer. For event
  293. * NNPA_ALL sum up all events.
  294. */
  295. static u64 paiext_getdata(struct perf_event *event)
  296. {
  297. struct paiext_mapptr *mp = this_cpu_ptr(paiext_root.mapptr);
  298. struct paiext_map *cpump = mp->mapptr;
  299. u64 sum = 0;
  300. int i;
  301. if (event->attr.config != PAI_NNPA_BASE)
  302. return paiext_getctr(cpump->area,
  303. event->attr.config - PAI_NNPA_BASE);
  304. for (i = 1; i <= paiext_cnt; i++)
  305. sum += paiext_getctr(cpump->area, i);
  306. return sum;
  307. }
  308. static u64 paiext_getall(struct perf_event *event)
  309. {
  310. return paiext_getdata(event);
  311. }
  312. static void paiext_read(struct perf_event *event)
  313. {
  314. u64 prev, new, delta;
  315. prev = local64_read(&event->hw.prev_count);
  316. new = paiext_getall(event);
  317. local64_set(&event->hw.prev_count, new);
  318. delta = new - prev;
  319. local64_add(delta, &event->count);
  320. }
  321. static void paiext_start(struct perf_event *event, int flags)
  322. {
  323. struct paiext_mapptr *mp = this_cpu_ptr(paiext_root.mapptr);
  324. struct paiext_map *cpump = mp->mapptr;
  325. u64 sum;
  326. if (!event->attr.sample_period) { /* Counting */
  327. sum = paiext_getall(event); /* Get current value */
  328. local64_set(&event->hw.prev_count, sum);
  329. } else { /* Sampling */
  330. memcpy((void *)PAI_SAVE_AREA(event), cpump->area,
  331. PAIE1_CTRBLOCK_SZ);
  332. /* Enable context switch callback for system-wide sampling */
  333. if (!(event->attach_state & PERF_ATTACH_TASK)) {
  334. list_add_tail(PAI_SWLIST(event), &cpump->syswide_list);
  335. perf_sched_cb_inc(event->pmu);
  336. } else {
  337. cpump->event = event;
  338. }
  339. }
  340. }
  341. static int paiext_add(struct perf_event *event, int flags)
  342. {
  343. struct paiext_mapptr *mp = this_cpu_ptr(paiext_root.mapptr);
  344. struct paiext_map *cpump = mp->mapptr;
  345. struct paiext_cb *pcb = cpump->paiext_cb;
  346. if (++cpump->active_events == 1) {
  347. get_lowcore()->aicd = virt_to_phys(cpump->paiext_cb);
  348. pcb->acc = virt_to_phys(cpump->area) | 0x1;
  349. /* Enable CPU instruction lookup for PAIE1 control block */
  350. local_ctl_set_bit(0, CR0_PAI_EXTENSION_BIT);
  351. }
  352. if (flags & PERF_EF_START)
  353. paiext_start(event, PERF_EF_RELOAD);
  354. event->hw.state = 0;
  355. return 0;
  356. }
  357. static void paiext_have_sample(struct perf_event *, struct paiext_map *);
  358. static void paiext_stop(struct perf_event *event, int flags)
  359. {
  360. struct paiext_mapptr *mp = this_cpu_ptr(paiext_root.mapptr);
  361. struct paiext_map *cpump = mp->mapptr;
  362. if (!event->attr.sample_period) { /* Counting */
  363. paiext_read(event);
  364. } else { /* Sampling */
  365. if (!(event->attach_state & PERF_ATTACH_TASK)) {
  366. list_del(PAI_SWLIST(event));
  367. perf_sched_cb_dec(event->pmu);
  368. } else {
  369. paiext_have_sample(event, cpump);
  370. cpump->event = NULL;
  371. }
  372. }
  373. event->hw.state = PERF_HES_STOPPED;
  374. }
  375. static void paiext_del(struct perf_event *event, int flags)
  376. {
  377. struct paiext_mapptr *mp = this_cpu_ptr(paiext_root.mapptr);
  378. struct paiext_map *cpump = mp->mapptr;
  379. struct paiext_cb *pcb = cpump->paiext_cb;
  380. paiext_stop(event, PERF_EF_UPDATE);
  381. if (--cpump->active_events == 0) {
  382. /* Disable CPU instruction lookup for PAIE1 control block */
  383. local_ctl_clear_bit(0, CR0_PAI_EXTENSION_BIT);
  384. pcb->acc = 0;
  385. get_lowcore()->aicd = 0;
  386. }
  387. }
  388. /* Create raw data and save it in buffer. Returns number of bytes copied.
  389. * Saves only positive counter entries of the form
  390. * 2 bytes: Number of counter
  391. * 8 bytes: Value of counter
  392. */
  393. static size_t paiext_copy(struct pai_userdata *userdata, unsigned long *area,
  394. unsigned long *area_old)
  395. {
  396. int i, outidx = 0;
  397. for (i = 1; i <= paiext_cnt; i++) {
  398. u64 val = paiext_getctr(area, i);
  399. u64 val_old = paiext_getctr(area_old, i);
  400. if (val >= val_old)
  401. val -= val_old;
  402. else
  403. val = (~0ULL - val_old) + val + 1;
  404. if (val) {
  405. userdata[outidx].num = i;
  406. userdata[outidx].value = val;
  407. outidx++;
  408. }
  409. }
  410. return outidx * sizeof(*userdata);
  411. }
  412. /* Write sample when one or more counters values are nonzero.
  413. *
  414. * Note: The function paiext_sched_task() and paiext_push_sample() are not
  415. * invoked after function paiext_del() has been called because of function
  416. * perf_sched_cb_dec().
  417. * The function paiext_sched_task() and paiext_push_sample() are only
  418. * called when sampling is active. Function perf_sched_cb_inc()
  419. * has been invoked to install function paiext_sched_task() as call back
  420. * to run at context switch time (see paiext_add()).
  421. *
  422. * This causes function perf_event_context_sched_out() and
  423. * perf_event_context_sched_in() to check whether the PMU has installed an
  424. * sched_task() callback. That callback is not active after paiext_del()
  425. * returns and has deleted the event on that CPU.
  426. */
  427. static int paiext_push_sample(size_t rawsize, struct paiext_map *cpump,
  428. struct perf_event *event)
  429. {
  430. struct perf_sample_data data;
  431. struct perf_raw_record raw;
  432. struct pt_regs regs;
  433. int overflow;
  434. /* Setup perf sample */
  435. memset(&regs, 0, sizeof(regs));
  436. memset(&raw, 0, sizeof(raw));
  437. memset(&data, 0, sizeof(data));
  438. perf_sample_data_init(&data, 0, event->hw.last_period);
  439. if (event->attr.sample_type & PERF_SAMPLE_TID) {
  440. data.tid_entry.pid = task_tgid_nr(current);
  441. data.tid_entry.tid = task_pid_nr(current);
  442. }
  443. if (event->attr.sample_type & PERF_SAMPLE_TIME)
  444. data.time = event->clock();
  445. if (event->attr.sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
  446. data.id = event->id;
  447. if (event->attr.sample_type & PERF_SAMPLE_CPU)
  448. data.cpu_entry.cpu = smp_processor_id();
  449. if (event->attr.sample_type & PERF_SAMPLE_RAW) {
  450. raw.frag.size = rawsize;
  451. raw.frag.data = cpump->save;
  452. perf_sample_save_raw_data(&data, event, &raw);
  453. }
  454. overflow = perf_event_overflow(event, &data, &regs);
  455. perf_event_update_userpage(event);
  456. /* Save NNPA lowcore area after read in event */
  457. memcpy((void *)PAI_SAVE_AREA(event), cpump->area,
  458. PAIE1_CTRBLOCK_SZ);
  459. return overflow;
  460. }
  461. /* Check if there is data to be saved on schedule out of a task. */
  462. static void paiext_have_sample(struct perf_event *event,
  463. struct paiext_map *cpump)
  464. {
  465. size_t rawsize;
  466. if (!event)
  467. return;
  468. rawsize = paiext_copy(cpump->save, cpump->area,
  469. (unsigned long *)PAI_SAVE_AREA(event));
  470. if (rawsize) /* Incremented counters */
  471. paiext_push_sample(rawsize, cpump, event);
  472. }
  473. /* Check if there is data to be saved on schedule out of a task. */
  474. static void paiext_have_samples(void)
  475. {
  476. struct paiext_mapptr *mp = this_cpu_ptr(paiext_root.mapptr);
  477. struct paiext_map *cpump = mp->mapptr;
  478. struct perf_event *event;
  479. list_for_each_entry(event, &cpump->syswide_list, hw.tp_list)
  480. paiext_have_sample(event, cpump);
  481. }
  482. /* Called on schedule-in and schedule-out. No access to event structure,
  483. * but for sampling only event NNPA_ALL is allowed.
  484. */
  485. static void paiext_sched_task(struct perf_event_pmu_context *pmu_ctx, bool sched_in)
  486. {
  487. /* We started with a clean page on event installation. So read out
  488. * results on schedule_out and if page was dirty, save old values.
  489. */
  490. if (!sched_in)
  491. paiext_have_samples();
  492. }
  493. /* Attribute definitions for pai extension1 interface. As with other CPU
  494. * Measurement Facilities, there is one attribute per mapped counter.
  495. * The number of mapped counters may vary per machine generation. Use
  496. * the QUERY PROCESSOR ACTIVITY COUNTER INFORMATION (QPACI) instruction
  497. * to determine the number of mapped counters. The instructions returns
  498. * a positive number, which is the highest number of supported counters.
  499. * All counters less than this number are also supported, there are no
  500. * holes. A returned number of zero means no support for mapped counters.
  501. *
  502. * The identification of the counter is a unique number. The chosen range
  503. * is 0x1800 + offset in mapped kernel page.
  504. * All CPU Measurement Facility counters identifiers must be unique and
  505. * the numbers from 0 to 496 are already used for the CPU Measurement
  506. * Counter facility. Number 0x1000 to 0x103e are used for PAI cryptography
  507. * counters.
  508. * Numbers 0xb0000, 0xbc000 and 0xbd000 are already
  509. * used for the CPU Measurement Sampling facility.
  510. */
  511. PMU_FORMAT_ATTR(event, "config:0-63");
  512. static struct attribute *paiext_format_attr[] = {
  513. &format_attr_event.attr,
  514. NULL,
  515. };
  516. static struct attribute_group paiext_events_group = {
  517. .name = "events",
  518. .attrs = NULL, /* Filled in attr_event_init() */
  519. };
  520. static struct attribute_group paiext_format_group = {
  521. .name = "format",
  522. .attrs = paiext_format_attr,
  523. };
  524. static const struct attribute_group *paiext_attr_groups[] = {
  525. &paiext_events_group,
  526. &paiext_format_group,
  527. NULL,
  528. };
  529. /* Performance monitoring unit for mapped counters */
  530. static struct pmu paiext = {
  531. .task_ctx_nr = perf_hw_context,
  532. .event_init = paiext_event_init,
  533. .add = paiext_add,
  534. .del = paiext_del,
  535. .start = paiext_start,
  536. .stop = paiext_stop,
  537. .read = paiext_read,
  538. .sched_task = paiext_sched_task,
  539. .attr_groups = paiext_attr_groups,
  540. };
  541. /* List of symbolic PAI extension 1 NNPA counter names. */
  542. static const char * const paiext_ctrnames[] = {
  543. [0] = "NNPA_ALL",
  544. [1] = "NNPA_ADD",
  545. [2] = "NNPA_SUB",
  546. [3] = "NNPA_MUL",
  547. [4] = "NNPA_DIV",
  548. [5] = "NNPA_MIN",
  549. [6] = "NNPA_MAX",
  550. [7] = "NNPA_LOG",
  551. [8] = "NNPA_EXP",
  552. [9] = "NNPA_IBM_RESERVED_9",
  553. [10] = "NNPA_RELU",
  554. [11] = "NNPA_TANH",
  555. [12] = "NNPA_SIGMOID",
  556. [13] = "NNPA_SOFTMAX",
  557. [14] = "NNPA_BATCHNORM",
  558. [15] = "NNPA_MAXPOOL2D",
  559. [16] = "NNPA_AVGPOOL2D",
  560. [17] = "NNPA_LSTMACT",
  561. [18] = "NNPA_GRUACT",
  562. [19] = "NNPA_CONVOLUTION",
  563. [20] = "NNPA_MATMUL_OP",
  564. [21] = "NNPA_MATMUL_OP_BCAST23",
  565. [22] = "NNPA_SMALLBATCH",
  566. [23] = "NNPA_LARGEDIM",
  567. [24] = "NNPA_SMALLTENSOR",
  568. [25] = "NNPA_1MFRAME",
  569. [26] = "NNPA_2GFRAME",
  570. [27] = "NNPA_ACCESSEXCEPT",
  571. [28] = "NNPA_TRANSFORM",
  572. [29] = "NNPA_GELU",
  573. [30] = "NNPA_MOMENTS",
  574. [31] = "NNPA_LAYERNORM",
  575. [32] = "NNPA_MATMUL_OP_BCAST1",
  576. [33] = "NNPA_SQRT",
  577. [34] = "NNPA_INVSQRT",
  578. [35] = "NNPA_NORM",
  579. [36] = "NNPA_REDUCE",
  580. };
  581. static void __init attr_event_free(struct attribute **attrs, int num)
  582. {
  583. struct perf_pmu_events_attr *pa;
  584. struct device_attribute *dap;
  585. int i;
  586. for (i = 0; i < num; i++) {
  587. dap = container_of(attrs[i], struct device_attribute, attr);
  588. pa = container_of(dap, struct perf_pmu_events_attr, attr);
  589. kfree(pa);
  590. }
  591. kfree(attrs);
  592. }
  593. static int __init attr_event_init_one(struct attribute **attrs, int num)
  594. {
  595. struct perf_pmu_events_attr *pa;
  596. /* Index larger than array_size, no counter name available */
  597. if (num >= ARRAY_SIZE(paiext_ctrnames)) {
  598. attrs[num] = NULL;
  599. return 0;
  600. }
  601. pa = kzalloc(sizeof(*pa), GFP_KERNEL);
  602. if (!pa)
  603. return -ENOMEM;
  604. sysfs_attr_init(&pa->attr.attr);
  605. pa->id = PAI_NNPA_BASE + num;
  606. pa->attr.attr.name = paiext_ctrnames[num];
  607. pa->attr.attr.mode = 0444;
  608. pa->attr.show = cpumf_events_sysfs_show;
  609. pa->attr.store = NULL;
  610. attrs[num] = &pa->attr.attr;
  611. return 0;
  612. }
  613. /* Create PMU sysfs event attributes on the fly. */
  614. static int __init attr_event_init(void)
  615. {
  616. struct attribute **attrs;
  617. int ret, i;
  618. attrs = kmalloc_array(paiext_cnt + 2, sizeof(*attrs), GFP_KERNEL);
  619. if (!attrs)
  620. return -ENOMEM;
  621. for (i = 0; i <= paiext_cnt; i++) {
  622. ret = attr_event_init_one(attrs, i);
  623. if (ret) {
  624. attr_event_free(attrs, i);
  625. return ret;
  626. }
  627. }
  628. attrs[i] = NULL;
  629. paiext_events_group.attrs = attrs;
  630. return 0;
  631. }
  632. static int __init paiext_init(void)
  633. {
  634. struct qpaci_info_block ib;
  635. int rc = -ENOMEM;
  636. if (!test_facility(197))
  637. return 0;
  638. qpaci(&ib);
  639. paiext_cnt = ib.num_nnpa;
  640. if (paiext_cnt >= PAI_NNPA_MAXCTR)
  641. paiext_cnt = PAI_NNPA_MAXCTR;
  642. if (!paiext_cnt)
  643. return 0;
  644. rc = attr_event_init();
  645. if (rc) {
  646. pr_err("Creation of PMU " KMSG_COMPONENT " /sysfs failed\n");
  647. return rc;
  648. }
  649. /* Setup s390dbf facility */
  650. paiext_dbg = debug_register(KMSG_COMPONENT, 2, 256, 128);
  651. if (!paiext_dbg) {
  652. pr_err("Registration of s390dbf " KMSG_COMPONENT " failed\n");
  653. rc = -ENOMEM;
  654. goto out_init;
  655. }
  656. debug_register_view(paiext_dbg, &debug_sprintf_view);
  657. rc = perf_pmu_register(&paiext, KMSG_COMPONENT, -1);
  658. if (rc) {
  659. pr_err("Registration of " KMSG_COMPONENT " PMU failed with "
  660. "rc=%i\n", rc);
  661. goto out_pmu;
  662. }
  663. return 0;
  664. out_pmu:
  665. debug_unregister_view(paiext_dbg, &debug_sprintf_view);
  666. debug_unregister(paiext_dbg);
  667. out_init:
  668. attr_event_free(paiext_events_group.attrs,
  669. ARRAY_SIZE(paiext_ctrnames) + 1);
  670. return rc;
  671. }
  672. device_initcall(paiext_init);