1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Performance event support for the System z CPU-measurement Sampling Facility 4 * 5 * Copyright IBM Corp. 2013, 2018 6 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com> 7 */ 8 #define pr_fmt(fmt) "cpum_sf: " fmt 9 10 #include <linux/kernel.h> 11 #include <linux/kernel_stat.h> 12 #include <linux/perf_event.h> 13 #include <linux/percpu.h> 14 #include <linux/pid.h> 15 #include <linux/notifier.h> 16 #include <linux/slab.h> 17 #include <linux/mm.h> 18 #include <linux/moduleparam.h> 19 #include <asm/cpu_mf.h> 20 #include <asm/irq.h> 21 #include <asm/debug.h> 22 #include <asm/timex.h> 23 #include <linux/io.h> 24 25 /* Perf PMU definitions for the sampling facility */ 26 #define PERF_CPUM_SF_MAX_CTR 2 27 #define PERF_EVENT_CPUM_SF 0xB0000UL /* Event: Basic-sampling */ 28 #define PERF_EVENT_CPUM_SF_DIAG 0xBD000UL /* Event: Combined-sampling */ 29 #define PERF_CPUM_SF_BASIC_MODE 0x0001 /* Basic-sampling flag */ 30 #define PERF_CPUM_SF_DIAG_MODE 0x0002 /* Diagnostic-sampling flag */ 31 #define PERF_CPUM_SF_FREQ_MODE 0x0008 /* Sampling with frequency */ 32 33 #define OVERFLOW_REG(hwc) ((hwc)->extra_reg.config) 34 #define SFB_ALLOC_REG(hwc) ((hwc)->extra_reg.alloc) 35 #define TEAR_REG(hwc) ((hwc)->last_tag) 36 #define SAMPL_RATE(hwc) ((hwc)->event_base) 37 #define SAMPL_FLAGS(hwc) ((hwc)->config_base) 38 #define SAMPL_DIAG_MODE(hwc) (SAMPL_FLAGS(hwc) & PERF_CPUM_SF_DIAG_MODE) 39 #define SAMPL_FREQ_MODE(hwc) (SAMPL_FLAGS(hwc) & PERF_CPUM_SF_FREQ_MODE) 40 41 /* Minimum number of sample-data-block-tables: 42 * At least one table is required for the sampling buffer structure. 43 * A single table contains up to 511 pointers to sample-data-blocks. 44 */ 45 #define CPUM_SF_MIN_SDBT 1 46 47 /* Number of sample-data-blocks per sample-data-block-table (SDBT): 48 * A table contains SDB pointers (8 bytes) and one table-link entry 49 * that points to the origin of the next SDBT. 50 */ 51 #define CPUM_SF_SDB_PER_TABLE ((PAGE_SIZE - 8) / 8) 52 53 /* Maximum page offset for an SDBT table-link entry: 54 * If this page offset is reached, a table-link entry to the next SDBT 55 * must be added. 56 */ 57 #define CPUM_SF_SDBT_TL_OFFSET (CPUM_SF_SDB_PER_TABLE * 8) 58 static inline int require_table_link(const void *sdbt) 59 { 60 return ((unsigned long)sdbt & ~PAGE_MASK) == CPUM_SF_SDBT_TL_OFFSET; 61 } 62 63 /* Minimum and maximum sampling buffer sizes: 64 * 65 * This number represents the maximum size of the sampling buffer taking 66 * the number of sample-data-block-tables into account. Note that these 67 * numbers apply to the basic-sampling function only. 68 * The maximum number of SDBs is increased by CPUM_SF_SDB_DIAG_FACTOR if 69 * the diagnostic-sampling function is active. 70 * 71 * Sampling buffer size Buffer characteristics 72 * --------------------------------------------------- 73 * 64KB == 16 pages (4KB per page) 74 * 1 page for SDB-tables 75 * 15 pages for SDBs 76 * 77 * 32MB == 8192 pages (4KB per page) 78 * 16 pages for SDB-tables 79 * 8176 pages for SDBs 80 */ 81 static unsigned long __read_mostly CPUM_SF_MIN_SDB = 15; 82 static unsigned long __read_mostly CPUM_SF_MAX_SDB = 8176; 83 static unsigned long __read_mostly CPUM_SF_SDB_DIAG_FACTOR = 1; 84 85 struct sf_buffer { 86 unsigned long *sdbt; /* Sample-data-block-table origin */ 87 /* buffer characteristics (required for buffer increments) */ 88 unsigned long num_sdb; /* Number of sample-data-blocks */ 89 unsigned long num_sdbt; /* Number of sample-data-block-tables */ 90 unsigned long *tail; /* last sample-data-block-table */ 91 }; 92 93 struct aux_buffer { 94 struct sf_buffer sfb; 95 unsigned long head; /* index of SDB of buffer head */ 96 unsigned long alert_mark; /* index of SDB of alert request position */ 97 unsigned long empty_mark; /* mark of SDB not marked full */ 98 unsigned long *sdb_index; /* SDB address for fast lookup */ 99 unsigned long *sdbt_index; /* SDBT address for fast lookup */ 100 }; 101 102 struct cpu_hw_sf { 103 /* CPU-measurement sampling information block */ 104 struct hws_qsi_info_block qsi; 105 /* CPU-measurement sampling control block */ 106 struct hws_lsctl_request_block lsctl; 107 struct sf_buffer sfb; /* Sampling buffer */ 108 unsigned int flags; /* Status flags */ 109 struct perf_event *event; /* Scheduled perf event */ 110 struct perf_output_handle handle; /* AUX buffer output handle */ 111 }; 112 static DEFINE_PER_CPU(struct cpu_hw_sf, cpu_hw_sf); 113 114 /* Debug feature */ 115 static debug_info_t *sfdbg; 116 117 /* Sampling control helper functions */ 118 static inline unsigned long freq_to_sample_rate(struct hws_qsi_info_block *qsi, 119 unsigned long freq) 120 { 121 return (USEC_PER_SEC / freq) * qsi->cpu_speed; 122 } 123 124 static inline unsigned long sample_rate_to_freq(struct hws_qsi_info_block *qsi, 125 unsigned long rate) 126 { 127 return USEC_PER_SEC * qsi->cpu_speed / rate; 128 } 129 130 /* Return pointer to trailer entry of an sample data block */ 131 static inline struct hws_trailer_entry *trailer_entry_ptr(unsigned long v) 132 { 133 void *ret; 134 135 ret = (void *)v; 136 ret += PAGE_SIZE; 137 ret -= sizeof(struct hws_trailer_entry); 138 139 return ret; 140 } 141 142 /* 143 * Return true if the entry in the sample data block table (sdbt) 144 * is a link to the next sdbt 145 */ 146 static inline int is_link_entry(unsigned long *s) 147 { 148 return *s & 0x1UL ? 1 : 0; 149 } 150 151 /* Return pointer to the linked sdbt */ 152 static inline unsigned long *get_next_sdbt(unsigned long *s) 153 { 154 return phys_to_virt(*s & ~0x1UL); 155 } 156 157 /* 158 * sf_disable() - Switch off sampling facility 159 */ 160 static void sf_disable(void) 161 { 162 struct hws_lsctl_request_block sreq; 163 164 memset(&sreq, 0, sizeof(sreq)); 165 lsctl(&sreq); 166 } 167 168 /* 169 * sf_buffer_available() - Check for an allocated sampling buffer 170 */ 171 static int sf_buffer_available(struct cpu_hw_sf *cpuhw) 172 { 173 return !!cpuhw->sfb.sdbt; 174 } 175 176 /* 177 * deallocate sampling facility buffer 178 */ 179 static void free_sampling_buffer(struct sf_buffer *sfb) 180 { 181 unsigned long *sdbt, *curr, *head; 182 183 sdbt = sfb->sdbt; 184 if (!sdbt) 185 return; 186 sfb->sdbt = NULL; 187 /* Free the SDBT after all SDBs are processed... */ 188 head = sdbt; 189 curr = sdbt; 190 do { 191 if (is_link_entry(curr)) { 192 /* Process table-link entries */ 193 curr = get_next_sdbt(curr); 194 free_page((unsigned long)sdbt); 195 sdbt = curr; 196 } else { 197 /* Process SDB pointer */ 198 free_page((unsigned long)phys_to_virt(*curr)); 199 curr++; 200 } 201 } while (curr != head); 202 memset(sfb, 0, sizeof(*sfb)); 203 } 204 205 static int alloc_sample_data_block(unsigned long *sdbt, gfp_t gfp_flags) 206 { 207 struct hws_trailer_entry *te; 208 unsigned long sdb; 209 210 /* Allocate and initialize sample-data-block */ 211 sdb = get_zeroed_page(gfp_flags); 212 if (!sdb) 213 return -ENOMEM; 214 te = trailer_entry_ptr(sdb); 215 te->header.a = 1; 216 217 /* Link SDB into the sample-data-block-table */ 218 *sdbt = virt_to_phys((void *)sdb); 219 220 return 0; 221 } 222 223 /* 224 * realloc_sampling_buffer() - extend sampler memory 225 * 226 * Allocates new sample-data-blocks and adds them to the specified sampling 227 * buffer memory. 228 * 229 * Important: This modifies the sampling buffer and must be called when the 230 * sampling facility is disabled. 231 * 232 * Returns zero on success, non-zero otherwise. 233 */ 234 static int realloc_sampling_buffer(struct sf_buffer *sfb, 235 unsigned long num_sdb, gfp_t gfp_flags) 236 { 237 int i, rc; 238 unsigned long *new, *tail, *tail_prev = NULL; 239 240 if (!sfb->sdbt || !sfb->tail) 241 return -EINVAL; 242 243 if (!is_link_entry(sfb->tail)) 244 return -EINVAL; 245 246 /* Append to the existing sampling buffer, overwriting the table-link 247 * register. 248 * The tail variables always points to the "tail" (last and table-link) 249 * entry in an SDB-table. 250 */ 251 tail = sfb->tail; 252 253 /* Do a sanity check whether the table-link entry points to 254 * the sampling buffer origin. 255 */ 256 if (sfb->sdbt != get_next_sdbt(tail)) { 257 debug_sprintf_event(sfdbg, 3, "%s buffer not linked origin %#lx tail %#lx\n", 258 __func__, (unsigned long)sfb->sdbt, 259 (unsigned long)tail); 260 return -EINVAL; 261 } 262 263 /* Allocate remaining SDBs */ 264 rc = 0; 265 for (i = 0; i < num_sdb; i++) { 266 /* Allocate a new SDB-table if it is full. */ 267 if (require_table_link(tail)) { 268 new = (unsigned long *)get_zeroed_page(gfp_flags); 269 if (!new) { 270 rc = -ENOMEM; 271 break; 272 } 273 sfb->num_sdbt++; 274 /* Link current page to tail of chain */ 275 *tail = virt_to_phys((void *)new) + 1; 276 tail_prev = tail; 277 tail = new; 278 } 279 280 /* Allocate a new sample-data-block. 281 * If there is not enough memory, stop the realloc process 282 * and simply use what was allocated. If this is a temporary 283 * issue, a new realloc call (if required) might succeed. 284 */ 285 rc = alloc_sample_data_block(tail, gfp_flags); 286 if (rc) { 287 /* Undo last SDBT. An SDBT with no SDB at its first 288 * entry but with an SDBT entry instead can not be 289 * handled by the interrupt handler code. 290 * Avoid this situation. 291 */ 292 if (tail_prev) { 293 sfb->num_sdbt--; 294 free_page((unsigned long)new); 295 tail = tail_prev; 296 } 297 break; 298 } 299 sfb->num_sdb++; 300 tail++; 301 tail_prev = new = NULL; /* Allocated at least one SBD */ 302 } 303 304 /* Link sampling buffer to its origin */ 305 *tail = virt_to_phys(sfb->sdbt) + 1; 306 sfb->tail = tail; 307 308 return rc; 309 } 310 311 /* 312 * allocate_sampling_buffer() - allocate sampler memory 313 * 314 * Allocates and initializes a sampling buffer structure using the 315 * specified number of sample-data-blocks (SDB). For each allocation, 316 * a 4K page is used. The number of sample-data-block-tables (SDBT) 317 * are calculated from SDBs. 318 * Also set the ALERT_REQ mask in each SDBs trailer. 319 * 320 * Returns zero on success, non-zero otherwise. 321 */ 322 static int alloc_sampling_buffer(struct sf_buffer *sfb, unsigned long num_sdb) 323 { 324 int rc; 325 326 if (sfb->sdbt) 327 return -EINVAL; 328 329 /* Allocate the sample-data-block-table origin */ 330 sfb->sdbt = (unsigned long *)get_zeroed_page(GFP_KERNEL); 331 if (!sfb->sdbt) 332 return -ENOMEM; 333 sfb->num_sdb = 0; 334 sfb->num_sdbt = 1; 335 336 /* Link the table origin to point to itself to prepare for 337 * realloc_sampling_buffer() invocation. 338 */ 339 sfb->tail = sfb->sdbt; 340 *sfb->tail = virt_to_phys((void *)sfb->sdbt) + 1; 341 342 /* Allocate requested number of sample-data-blocks */ 343 rc = realloc_sampling_buffer(sfb, num_sdb, GFP_KERNEL); 344 if (rc) 345 free_sampling_buffer(sfb); 346 return rc; 347 } 348 349 static void sfb_set_limits(unsigned long min, unsigned long max) 350 { 351 struct hws_qsi_info_block si; 352 353 CPUM_SF_MIN_SDB = min; 354 CPUM_SF_MAX_SDB = max; 355 356 memset(&si, 0, sizeof(si)); 357 qsi(&si); 358 CPUM_SF_SDB_DIAG_FACTOR = DIV_ROUND_UP(si.dsdes, si.bsdes); 359 } 360 361 static unsigned long sfb_max_limit(struct hw_perf_event *hwc) 362 { 363 return SAMPL_DIAG_MODE(hwc) ? CPUM_SF_MAX_SDB * CPUM_SF_SDB_DIAG_FACTOR 364 : CPUM_SF_MAX_SDB; 365 } 366 367 static unsigned long sfb_pending_allocs(struct sf_buffer *sfb, 368 struct hw_perf_event *hwc) 369 { 370 if (!sfb->sdbt) 371 return SFB_ALLOC_REG(hwc); 372 if (SFB_ALLOC_REG(hwc) > sfb->num_sdb) 373 return SFB_ALLOC_REG(hwc) - sfb->num_sdb; 374 return 0; 375 } 376 377 static void sfb_account_allocs(unsigned long num, struct hw_perf_event *hwc) 378 { 379 /* Limit the number of SDBs to not exceed the maximum */ 380 num = min_t(unsigned long, num, sfb_max_limit(hwc) - SFB_ALLOC_REG(hwc)); 381 if (num) 382 SFB_ALLOC_REG(hwc) += num; 383 } 384 385 static void sfb_init_allocs(unsigned long num, struct hw_perf_event *hwc) 386 { 387 SFB_ALLOC_REG(hwc) = 0; 388 sfb_account_allocs(num, hwc); 389 } 390 391 static void deallocate_buffers(struct cpu_hw_sf *cpuhw) 392 { 393 if (sf_buffer_available(cpuhw)) 394 free_sampling_buffer(&cpuhw->sfb); 395 } 396 397 static int allocate_buffers(struct cpu_hw_sf *cpuhw, struct hw_perf_event *hwc) 398 { 399 unsigned long n_sdb, freq; 400 401 /* Calculate sampling buffers using 4K pages 402 * 403 * 1. The sampling size is 32 bytes for basic sampling. This size 404 * is the same for all machine types. Diagnostic 405 * sampling uses auxlilary data buffer setup which provides the 406 * memory for SDBs using linux common code auxiliary trace 407 * setup. 408 * 409 * 2. Function alloc_sampling_buffer() sets the Alert Request 410 * Control indicator to trigger a measurement-alert to harvest 411 * sample-data-blocks (SDB). This is done per SDB. This 412 * measurement alert interrupt fires quick enough to handle 413 * one SDB, on very high frequency and work loads there might 414 * be 2 to 3 SBDs available for sample processing. 415 * Currently there is no need for setup alert request on every 416 * n-th page. This is counterproductive as one IRQ triggers 417 * a very high number of samples to be processed at one IRQ. 418 * 419 * 3. Use the sampling frequency as input. 420 * Compute the number of SDBs and ensure a minimum 421 * of CPUM_SF_MIN_SDB. Depending on frequency add some more 422 * SDBs to handle a higher sampling rate. 423 * Use a minimum of CPUM_SF_MIN_SDB and allow for 100 samples 424 * (one SDB) for every 10000 HZ frequency increment. 425 * 426 * 4. Compute the number of sample-data-block-tables (SDBT) and 427 * ensure a minimum of CPUM_SF_MIN_SDBT (one table can manage up 428 * to 511 SDBs). 429 */ 430 freq = sample_rate_to_freq(&cpuhw->qsi, SAMPL_RATE(hwc)); 431 n_sdb = CPUM_SF_MIN_SDB + DIV_ROUND_UP(freq, 10000); 432 433 /* If there is already a sampling buffer allocated, it is very likely 434 * that the sampling facility is enabled too. If the event to be 435 * initialized requires a greater sampling buffer, the allocation must 436 * be postponed. Changing the sampling buffer requires the sampling 437 * facility to be in the disabled state. So, account the number of 438 * required SDBs and let cpumsf_pmu_enable() resize the buffer just 439 * before the event is started. 440 */ 441 sfb_init_allocs(n_sdb, hwc); 442 if (sf_buffer_available(cpuhw)) 443 return 0; 444 445 return alloc_sampling_buffer(&cpuhw->sfb, 446 sfb_pending_allocs(&cpuhw->sfb, hwc)); 447 } 448 449 static unsigned long min_percent(unsigned int percent, unsigned long base, 450 unsigned long min) 451 { 452 return min_t(unsigned long, min, DIV_ROUND_UP(percent * base, 100)); 453 } 454 455 static unsigned long compute_sfb_extent(unsigned long ratio, unsigned long base) 456 { 457 /* Use a percentage-based approach to extend the sampling facility 458 * buffer. Accept up to 5% sample data loss. 459 * Vary the extents between 1% to 5% of the current number of 460 * sample-data-blocks. 461 */ 462 if (ratio <= 5) 463 return 0; 464 if (ratio <= 25) 465 return min_percent(1, base, 1); 466 if (ratio <= 50) 467 return min_percent(1, base, 1); 468 if (ratio <= 75) 469 return min_percent(2, base, 2); 470 if (ratio <= 100) 471 return min_percent(3, base, 3); 472 if (ratio <= 250) 473 return min_percent(4, base, 4); 474 475 return min_percent(5, base, 8); 476 } 477 478 static void sfb_account_overflows(struct cpu_hw_sf *cpuhw, 479 struct hw_perf_event *hwc) 480 { 481 unsigned long ratio, num; 482 483 if (!OVERFLOW_REG(hwc)) 484 return; 485 486 /* The sample_overflow contains the average number of sample data 487 * that has been lost because sample-data-blocks were full. 488 * 489 * Calculate the total number of sample data entries that has been 490 * discarded. Then calculate the ratio of lost samples to total samples 491 * per second in percent. 492 */ 493 ratio = DIV_ROUND_UP(100 * OVERFLOW_REG(hwc) * cpuhw->sfb.num_sdb, 494 sample_rate_to_freq(&cpuhw->qsi, SAMPL_RATE(hwc))); 495 496 /* Compute number of sample-data-blocks */ 497 num = compute_sfb_extent(ratio, cpuhw->sfb.num_sdb); 498 if (num) 499 sfb_account_allocs(num, hwc); 500 501 OVERFLOW_REG(hwc) = 0; 502 } 503 504 /* extend_sampling_buffer() - Extend sampling buffer 505 * @sfb: Sampling buffer structure (for local CPU) 506 * @hwc: Perf event hardware structure 507 * 508 * Use this function to extend the sampling buffer based on the overflow counter 509 * and postponed allocation extents stored in the specified Perf event hardware. 510 * 511 * Important: This function disables the sampling facility in order to safely 512 * change the sampling buffer structure. Do not call this function 513 * when the PMU is active. 514 */ 515 static void extend_sampling_buffer(struct sf_buffer *sfb, 516 struct hw_perf_event *hwc) 517 { 518 unsigned long num; 519 520 num = sfb_pending_allocs(sfb, hwc); 521 if (!num) 522 return; 523 524 /* Disable the sampling facility to reset any states and also 525 * clear pending measurement alerts. 526 */ 527 sf_disable(); 528 529 /* Extend the sampling buffer. 530 * This memory allocation typically happens in an atomic context when 531 * called by perf. Because this is a reallocation, it is fine if the 532 * new SDB-request cannot be satisfied immediately. 533 */ 534 realloc_sampling_buffer(sfb, num, GFP_ATOMIC); 535 } 536 537 /* Number of perf events counting hardware events */ 538 static refcount_t num_events; 539 /* Used to avoid races in calling reserve/release_cpumf_hardware */ 540 static DEFINE_MUTEX(pmc_reserve_mutex); 541 542 #define PMC_INIT 0 543 #define PMC_RELEASE 1 544 static void setup_pmc_cpu(void *flags) 545 { 546 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf); 547 548 sf_disable(); 549 switch (*((int *)flags)) { 550 case PMC_INIT: 551 memset(cpuhw, 0, sizeof(*cpuhw)); 552 qsi(&cpuhw->qsi); 553 cpuhw->flags |= PMU_F_RESERVED; 554 break; 555 case PMC_RELEASE: 556 cpuhw->flags &= ~PMU_F_RESERVED; 557 deallocate_buffers(cpuhw); 558 break; 559 } 560 } 561 562 static void release_pmc_hardware(void) 563 { 564 int flags = PMC_RELEASE; 565 566 irq_subclass_unregister(IRQ_SUBCLASS_MEASUREMENT_ALERT); 567 on_each_cpu(setup_pmc_cpu, &flags, 1); 568 } 569 570 static void reserve_pmc_hardware(void) 571 { 572 int flags = PMC_INIT; 573 574 on_each_cpu(setup_pmc_cpu, &flags, 1); 575 irq_subclass_register(IRQ_SUBCLASS_MEASUREMENT_ALERT); 576 } 577 578 static void hw_perf_event_destroy(struct perf_event *event) 579 { 580 /* Release PMC if this is the last perf event */ 581 if (refcount_dec_and_mutex_lock(&num_events, &pmc_reserve_mutex)) { 582 release_pmc_hardware(); 583 mutex_unlock(&pmc_reserve_mutex); 584 } 585 } 586 587 static void hw_init_period(struct hw_perf_event *hwc, u64 period) 588 { 589 hwc->sample_period = period; 590 hwc->last_period = hwc->sample_period; 591 local64_set(&hwc->period_left, hwc->sample_period); 592 } 593 594 static unsigned long hw_limit_rate(const struct hws_qsi_info_block *si, 595 unsigned long rate) 596 { 597 return clamp_t(unsigned long, rate, 598 si->min_sampl_rate, si->max_sampl_rate); 599 } 600 601 static u32 cpumsf_pid_type(struct perf_event *event, 602 u32 pid, enum pid_type type) 603 { 604 struct task_struct *tsk; 605 606 /* Idle process */ 607 if (!pid) 608 goto out; 609 610 tsk = find_task_by_pid_ns(pid, &init_pid_ns); 611 pid = -1; 612 if (tsk) { 613 /* 614 * Only top level events contain the pid namespace in which 615 * they are created. 616 */ 617 if (event->parent) 618 event = event->parent; 619 pid = __task_pid_nr_ns(tsk, type, event->ns); 620 /* 621 * See also 1d953111b648 622 * "perf/core: Don't report zero PIDs for exiting tasks". 623 */ 624 if (!pid && !pid_alive(tsk)) 625 pid = -1; 626 } 627 out: 628 return pid; 629 } 630 631 static void cpumsf_output_event_pid(struct perf_event *event, 632 struct perf_sample_data *data, 633 struct pt_regs *regs) 634 { 635 u32 pid; 636 struct perf_event_header header; 637 struct perf_output_handle handle; 638 639 /* 640 * Obtain the PID from the basic-sampling data entry and 641 * correct the data->tid_entry.pid value. 642 */ 643 pid = data->tid_entry.pid; 644 645 /* Protect callchain buffers, tasks */ 646 rcu_read_lock(); 647 648 perf_prepare_sample(data, event, regs); 649 perf_prepare_header(&header, data, event, regs); 650 if (perf_output_begin(&handle, data, event, header.size)) 651 goto out; 652 653 /* Update the process ID (see also kernel/events/core.c) */ 654 data->tid_entry.pid = cpumsf_pid_type(event, pid, PIDTYPE_TGID); 655 data->tid_entry.tid = cpumsf_pid_type(event, pid, PIDTYPE_PID); 656 657 perf_output_sample(&handle, &header, data, event); 658 perf_output_end(&handle); 659 out: 660 rcu_read_unlock(); 661 } 662 663 static unsigned long getrate(bool freq, unsigned long sample, 664 struct hws_qsi_info_block *si) 665 { 666 unsigned long rate; 667 668 if (freq) { 669 rate = freq_to_sample_rate(si, sample); 670 rate = hw_limit_rate(si, rate); 671 } else { 672 /* The min/max sampling rates specifies the valid range 673 * of sample periods. If the specified sample period is 674 * out of range, limit the period to the range boundary. 675 */ 676 rate = hw_limit_rate(si, sample); 677 678 /* The perf core maintains a maximum sample rate that is 679 * configurable through the sysctl interface. Ensure the 680 * sampling rate does not exceed this value. This also helps 681 * to avoid throttling when pushing samples with 682 * perf_event_overflow(). 683 */ 684 if (sample_rate_to_freq(si, rate) > 685 sysctl_perf_event_sample_rate) { 686 rate = 0; 687 } 688 } 689 return rate; 690 } 691 692 /* The sampling information (si) contains information about the 693 * min/max sampling intervals and the CPU speed. So calculate the 694 * correct sampling interval and avoid the whole period adjust 695 * feedback loop. 696 * 697 * Since the CPU Measurement sampling facility can not handle frequency 698 * calculate the sampling interval when frequency is specified using 699 * this formula: 700 * interval := cpu_speed * 1000000 / sample_freq 701 * 702 * Returns errno on bad input and zero on success with parameter interval 703 * set to the correct sampling rate. 704 * 705 * Note: This function turns off freq bit to avoid calling function 706 * perf_adjust_period(). This causes frequency adjustment in the common 707 * code part which causes tremendous variations in the counter values. 708 */ 709 static int __hw_perf_event_init_rate(struct perf_event *event, 710 struct hws_qsi_info_block *si) 711 { 712 struct perf_event_attr *attr = &event->attr; 713 struct hw_perf_event *hwc = &event->hw; 714 unsigned long rate; 715 716 if (attr->freq) { 717 if (!attr->sample_freq) 718 return -EINVAL; 719 rate = getrate(attr->freq, attr->sample_freq, si); 720 attr->freq = 0; /* Don't call perf_adjust_period() */ 721 SAMPL_FLAGS(hwc) |= PERF_CPUM_SF_FREQ_MODE; 722 } else { 723 rate = getrate(attr->freq, attr->sample_period, si); 724 if (!rate) 725 return -EINVAL; 726 } 727 attr->sample_period = rate; 728 SAMPL_RATE(hwc) = rate; 729 hw_init_period(hwc, SAMPL_RATE(hwc)); 730 return 0; 731 } 732 733 static int __hw_perf_event_init(struct perf_event *event) 734 { 735 struct cpu_hw_sf *cpuhw; 736 struct hws_qsi_info_block si; 737 struct perf_event_attr *attr = &event->attr; 738 struct hw_perf_event *hwc = &event->hw; 739 int cpu, err = 0; 740 741 /* Reserve CPU-measurement sampling facility */ 742 mutex_lock(&pmc_reserve_mutex); 743 if (!refcount_inc_not_zero(&num_events)) { 744 reserve_pmc_hardware(); 745 refcount_set(&num_events, 1); 746 } 747 event->destroy = hw_perf_event_destroy; 748 749 /* Access per-CPU sampling information (query sampling info) */ 750 /* 751 * The event->cpu value can be -1 to count on every CPU, for example, 752 * when attaching to a task. If this is specified, use the query 753 * sampling info from the current CPU, otherwise use event->cpu to 754 * retrieve the per-CPU information. 755 * Later, cpuhw indicates whether to allocate sampling buffers for a 756 * particular CPU (cpuhw!=NULL) or each online CPU (cpuw==NULL). 757 */ 758 memset(&si, 0, sizeof(si)); 759 cpuhw = NULL; 760 if (event->cpu == -1) { 761 qsi(&si); 762 } else { 763 /* Event is pinned to a particular CPU, retrieve the per-CPU 764 * sampling structure for accessing the CPU-specific QSI. 765 */ 766 cpuhw = &per_cpu(cpu_hw_sf, event->cpu); 767 si = cpuhw->qsi; 768 } 769 770 /* Check sampling facility authorization and, if not authorized, 771 * fall back to other PMUs. It is safe to check any CPU because 772 * the authorization is identical for all configured CPUs. 773 */ 774 if (!si.as) { 775 err = -ENOENT; 776 goto out; 777 } 778 779 if (si.ribm & CPU_MF_SF_RIBM_NOTAV) { 780 pr_warn("CPU Measurement Facility sampling is temporarily not available\n"); 781 err = -EBUSY; 782 goto out; 783 } 784 785 /* Always enable basic sampling */ 786 SAMPL_FLAGS(hwc) = PERF_CPUM_SF_BASIC_MODE; 787 788 /* Check if diagnostic sampling is requested. Deny if the required 789 * sampling authorization is missing. 790 */ 791 if (attr->config == PERF_EVENT_CPUM_SF_DIAG) { 792 if (!si.ad) { 793 err = -EPERM; 794 goto out; 795 } 796 SAMPL_FLAGS(hwc) |= PERF_CPUM_SF_DIAG_MODE; 797 } 798 799 err = __hw_perf_event_init_rate(event, &si); 800 if (err) 801 goto out; 802 803 /* Use AUX buffer. No need to allocate it by ourself */ 804 if (attr->config == PERF_EVENT_CPUM_SF_DIAG) 805 goto out; 806 807 /* Allocate the per-CPU sampling buffer using the CPU information 808 * from the event. If the event is not pinned to a particular 809 * CPU (event->cpu == -1; or cpuhw == NULL), allocate sampling 810 * buffers for each online CPU. 811 */ 812 if (cpuhw) 813 /* Event is pinned to a particular CPU */ 814 err = allocate_buffers(cpuhw, hwc); 815 else { 816 /* Event is not pinned, allocate sampling buffer on 817 * each online CPU 818 */ 819 for_each_online_cpu(cpu) { 820 cpuhw = &per_cpu(cpu_hw_sf, cpu); 821 err = allocate_buffers(cpuhw, hwc); 822 if (err) 823 break; 824 } 825 } 826 827 /* If PID/TID sampling is active, replace the default overflow 828 * handler to extract and resolve the PIDs from the basic-sampling 829 * data entries. 830 */ 831 if (event->attr.sample_type & PERF_SAMPLE_TID) 832 if (is_default_overflow_handler(event)) 833 event->overflow_handler = cpumsf_output_event_pid; 834 out: 835 mutex_unlock(&pmc_reserve_mutex); 836 return err; 837 } 838 839 static bool is_callchain_event(struct perf_event *event) 840 { 841 u64 sample_type = event->attr.sample_type; 842 843 return sample_type & (PERF_SAMPLE_CALLCHAIN | PERF_SAMPLE_REGS_USER | 844 PERF_SAMPLE_REGS_INTR | PERF_SAMPLE_STACK_USER); 845 } 846 847 static int cpumsf_pmu_event_init(struct perf_event *event) 848 { 849 int err; 850 851 /* No support for taken branch sampling */ 852 /* No support for callchain, stacks and registers */ 853 if (has_branch_stack(event) || is_callchain_event(event)) 854 return -EOPNOTSUPP; 855 856 switch (event->attr.type) { 857 case PERF_TYPE_RAW: 858 if ((event->attr.config != PERF_EVENT_CPUM_SF) && 859 (event->attr.config != PERF_EVENT_CPUM_SF_DIAG)) 860 return -ENOENT; 861 break; 862 case PERF_TYPE_HARDWARE: 863 /* Support sampling of CPU cycles in addition to the 864 * counter facility. However, the counter facility 865 * is more precise and, hence, restrict this PMU to 866 * sampling events only. 867 */ 868 if (event->attr.config != PERF_COUNT_HW_CPU_CYCLES) 869 return -ENOENT; 870 if (!is_sampling_event(event)) 871 return -ENOENT; 872 break; 873 default: 874 return -ENOENT; 875 } 876 877 /* Force reset of idle/hv excludes regardless of what the 878 * user requested. 879 */ 880 if (event->attr.exclude_hv) 881 event->attr.exclude_hv = 0; 882 if (event->attr.exclude_idle) 883 event->attr.exclude_idle = 0; 884 885 err = __hw_perf_event_init(event); 886 return err; 887 } 888 889 static void cpumsf_pmu_enable(struct pmu *pmu) 890 { 891 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf); 892 struct hw_perf_event *hwc; 893 int err; 894 895 /* 896 * Event must be 897 * - added/started on this CPU (PMU_F_IN_USE set) 898 * - and CPU must be available (PMU_F_RESERVED set) 899 * - and not already enabled (PMU_F_ENABLED not set) 900 * - and not in error condition (PMU_F_ERR_MASK not set) 901 */ 902 if (cpuhw->flags != (PMU_F_IN_USE | PMU_F_RESERVED)) 903 return; 904 905 /* Check whether to extent the sampling buffer. 906 * 907 * Two conditions trigger an increase of the sampling buffer for a 908 * perf event: 909 * 1. Postponed buffer allocations from the event initialization. 910 * 2. Sampling overflows that contribute to pending allocations. 911 * 912 * Note that the extend_sampling_buffer() function disables the sampling 913 * facility, but it can be fully re-enabled using sampling controls that 914 * have been saved in cpumsf_pmu_disable(). 915 */ 916 hwc = &cpuhw->event->hw; 917 if (!(SAMPL_DIAG_MODE(hwc))) { 918 /* 919 * Account number of overflow-designated buffer extents 920 */ 921 sfb_account_overflows(cpuhw, hwc); 922 extend_sampling_buffer(&cpuhw->sfb, hwc); 923 } 924 /* Rate may be adjusted with ioctl() */ 925 cpuhw->lsctl.interval = SAMPL_RATE(hwc); 926 927 /* (Re)enable the PMU and sampling facility */ 928 err = lsctl(&cpuhw->lsctl); 929 if (err) { 930 pr_err("Loading sampling controls failed: op 1 err %i\n", err); 931 return; 932 } 933 934 /* Load current program parameter */ 935 lpp(&get_lowcore()->lpp); 936 cpuhw->flags |= PMU_F_ENABLED; 937 } 938 939 static void cpumsf_pmu_disable(struct pmu *pmu) 940 { 941 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf); 942 struct hws_lsctl_request_block inactive; 943 struct hws_qsi_info_block si; 944 int err; 945 946 if (!(cpuhw->flags & PMU_F_ENABLED)) 947 return; 948 949 if (cpuhw->flags & PMU_F_ERR_MASK) 950 return; 951 952 /* Switch off sampling activation control */ 953 inactive = cpuhw->lsctl; 954 inactive.cs = 0; 955 inactive.cd = 0; 956 957 err = lsctl(&inactive); 958 if (err) { 959 pr_err("Loading sampling controls failed: op 2 err %i\n", err); 960 return; 961 } 962 963 /* 964 * Save state of TEAR and DEAR register contents. 965 * TEAR/DEAR values are valid only if the sampling facility is 966 * enabled. Note that cpumsf_pmu_disable() might be called even 967 * for a disabled sampling facility because cpumsf_pmu_enable() 968 * controls the enable/disable state. 969 */ 970 qsi(&si); 971 if (si.es) { 972 cpuhw->lsctl.tear = si.tear; 973 cpuhw->lsctl.dear = si.dear; 974 } 975 976 cpuhw->flags &= ~PMU_F_ENABLED; 977 } 978 979 /* perf_event_exclude() - Filter event 980 * @event: The perf event 981 * @regs: pt_regs structure 982 * @sde_regs: Sample-data-entry (sde) regs structure 983 * 984 * Filter perf events according to their exclude specification. 985 * 986 * Return non-zero if the event shall be excluded. 987 */ 988 static int perf_event_exclude(struct perf_event *event, struct pt_regs *regs, 989 struct perf_sf_sde_regs *sde_regs) 990 { 991 if (event->attr.exclude_user && user_mode(regs)) 992 return 1; 993 if (event->attr.exclude_kernel && !user_mode(regs)) 994 return 1; 995 if (event->attr.exclude_guest && sde_regs->in_guest) 996 return 1; 997 if (event->attr.exclude_host && !sde_regs->in_guest) 998 return 1; 999 return 0; 1000 } 1001 1002 /* perf_push_sample() - Push samples to perf 1003 * @event: The perf event 1004 * @sample: Hardware sample data 1005 * 1006 * Use the hardware sample data to create perf event sample. The sample 1007 * is the pushed to the event subsystem and the function checks for 1008 * possible event overflows. If an event overflow occurs, the PMU is 1009 * stopped. 1010 * 1011 * Return non-zero if an event overflow occurred. 1012 */ 1013 static int perf_push_sample(struct perf_event *event, 1014 struct hws_basic_entry *basic) 1015 { 1016 int overflow; 1017 struct pt_regs regs; 1018 struct perf_sf_sde_regs *sde_regs; 1019 struct perf_sample_data data; 1020 1021 /* Setup perf sample */ 1022 perf_sample_data_init(&data, 0, event->hw.last_period); 1023 1024 /* Setup pt_regs to look like an CPU-measurement external interrupt 1025 * using the Program Request Alert code. The regs.int_parm_long 1026 * field which is unused contains additional sample-data-entry related 1027 * indicators. 1028 */ 1029 memset(®s, 0, sizeof(regs)); 1030 regs.int_code = 0x1407; 1031 regs.int_parm = CPU_MF_INT_SF_PRA; 1032 sde_regs = (struct perf_sf_sde_regs *) ®s.int_parm_long; 1033 1034 psw_bits(regs.psw).ia = basic->ia; 1035 psw_bits(regs.psw).dat = basic->T; 1036 psw_bits(regs.psw).wait = basic->W; 1037 psw_bits(regs.psw).pstate = basic->P; 1038 psw_bits(regs.psw).as = basic->AS; 1039 1040 /* 1041 * Use the hardware provided configuration level to decide if the 1042 * sample belongs to a guest or host. If that is not available, 1043 * fall back to the following heuristics: 1044 * A non-zero guest program parameter always indicates a guest 1045 * sample. Some early samples or samples from guests without 1046 * lpp usage would be misaccounted to the host. We use the asn 1047 * value as an addon heuristic to detect most of these guest samples. 1048 * If the value differs from 0xffff (the host value), we assume to 1049 * be a KVM guest. 1050 */ 1051 switch (basic->CL) { 1052 case 1: /* logical partition */ 1053 sde_regs->in_guest = 0; 1054 break; 1055 case 2: /* virtual machine */ 1056 sde_regs->in_guest = 1; 1057 break; 1058 default: /* old machine, use heuristics */ 1059 if (basic->gpp || basic->prim_asn != 0xffff) 1060 sde_regs->in_guest = 1; 1061 break; 1062 } 1063 1064 /* 1065 * Store the PID value from the sample-data-entry to be 1066 * processed and resolved by cpumsf_output_event_pid(). 1067 */ 1068 data.tid_entry.pid = basic->hpp & LPP_PID_MASK; 1069 1070 overflow = 0; 1071 if (perf_event_exclude(event, ®s, sde_regs)) 1072 goto out; 1073 overflow = perf_event_overflow(event, &data, ®s); 1074 perf_event_update_userpage(event); 1075 out: 1076 return overflow; 1077 } 1078 1079 static void perf_event_count_update(struct perf_event *event, u64 count) 1080 { 1081 local64_add(count, &event->count); 1082 } 1083 1084 /* hw_collect_samples() - Walk through a sample-data-block and collect samples 1085 * @event: The perf event 1086 * @sdbt: Sample-data-block table 1087 * @overflow: Event overflow counter 1088 * 1089 * Walks through a sample-data-block and collects sampling data entries that are 1090 * then pushed to the perf event subsystem. Depending on the sampling function, 1091 * there can be either basic-sampling or combined-sampling data entries. A 1092 * combined-sampling data entry consists of a basic- and a diagnostic-sampling 1093 * data entry. The sampling function is determined by the flags in the perf 1094 * event hardware structure. The function always works with a combined-sampling 1095 * data entry but ignores the diagnostic portion if it is not available. 1096 * 1097 * Note that the implementation focuses on basic-sampling data entries and, if 1098 * such an entry is not valid, the entire combined-sampling data entry is 1099 * ignored. 1100 * 1101 * The overflow variables counts the number of samples that has been discarded 1102 * due to a perf event overflow. 1103 */ 1104 static void hw_collect_samples(struct perf_event *event, unsigned long *sdbt, 1105 unsigned long long *overflow) 1106 { 1107 struct hws_trailer_entry *te; 1108 struct hws_basic_entry *sample; 1109 1110 te = trailer_entry_ptr((unsigned long)sdbt); 1111 sample = (struct hws_basic_entry *)sdbt; 1112 while ((unsigned long *)sample < (unsigned long *)te) { 1113 /* Check for an empty sample */ 1114 if (!sample->def || sample->LS) 1115 break; 1116 1117 /* Update perf event period */ 1118 perf_event_count_update(event, SAMPL_RATE(&event->hw)); 1119 1120 /* Check whether sample is valid */ 1121 if (sample->def == 0x0001) { 1122 /* If an event overflow occurred, the PMU is stopped to 1123 * throttle event delivery. Remaining sample data is 1124 * discarded. 1125 */ 1126 if (!*overflow) { 1127 /* Check whether sample is consistent */ 1128 if (sample->I == 0 && sample->W == 0) { 1129 /* Deliver sample data to perf */ 1130 *overflow = perf_push_sample(event, 1131 sample); 1132 } 1133 } else 1134 /* Count discarded samples */ 1135 *overflow += 1; 1136 } else { 1137 /* Sample slot is not yet written or other record. 1138 * 1139 * This condition can occur if the buffer was reused 1140 * from a combined basic- and diagnostic-sampling. 1141 * If only basic-sampling is then active, entries are 1142 * written into the larger diagnostic entries. 1143 * This is typically the case for sample-data-blocks 1144 * that are not full. Stop processing if the first 1145 * invalid format was detected. 1146 */ 1147 if (!te->header.f) 1148 break; 1149 } 1150 1151 /* Reset sample slot and advance to next sample */ 1152 sample->def = 0; 1153 sample++; 1154 } 1155 } 1156 1157 /* hw_perf_event_update() - Process sampling buffer 1158 * @event: The perf event 1159 * @flush_all: Flag to also flush partially filled sample-data-blocks 1160 * 1161 * Processes the sampling buffer and create perf event samples. 1162 * The sampling buffer position are retrieved and saved in the TEAR_REG 1163 * register of the specified perf event. 1164 * 1165 * Only full sample-data-blocks are processed. Specify the flush_all flag 1166 * to also walk through partially filled sample-data-blocks. 1167 */ 1168 static void hw_perf_event_update(struct perf_event *event, int flush_all) 1169 { 1170 unsigned long long event_overflow, sampl_overflow, num_sdb; 1171 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf); 1172 struct hw_perf_event *hwc = &event->hw; 1173 union hws_trailer_header prev, new; 1174 struct hws_trailer_entry *te; 1175 unsigned long *sdbt, sdb; 1176 int done; 1177 1178 /* 1179 * AUX buffer is used when in diagnostic sampling mode. 1180 * No perf events/samples are created. 1181 */ 1182 if (SAMPL_DIAG_MODE(hwc)) 1183 return; 1184 1185 sdbt = (unsigned long *)TEAR_REG(hwc); 1186 done = event_overflow = sampl_overflow = num_sdb = 0; 1187 while (!done) { 1188 /* Get the trailer entry of the sample-data-block */ 1189 sdb = (unsigned long)phys_to_virt(*sdbt); 1190 te = trailer_entry_ptr(sdb); 1191 1192 /* Leave loop if no more work to do (block full indicator) */ 1193 if (!te->header.f) { 1194 done = 1; 1195 if (!flush_all) 1196 break; 1197 } 1198 1199 /* Check the sample overflow count */ 1200 if (te->header.overflow) 1201 /* Account sample overflows and, if a particular limit 1202 * is reached, extend the sampling buffer. 1203 * For details, see sfb_account_overflows(). 1204 */ 1205 sampl_overflow += te->header.overflow; 1206 1207 /* Collect all samples from a single sample-data-block and 1208 * flag if an (perf) event overflow happened. If so, the PMU 1209 * is stopped and remaining samples will be discarded. 1210 */ 1211 hw_collect_samples(event, (unsigned long *)sdb, &event_overflow); 1212 num_sdb++; 1213 1214 /* Reset trailer (using compare-double-and-swap) */ 1215 prev.val = READ_ONCE_ALIGNED_128(te->header.val); 1216 do { 1217 new.val = prev.val; 1218 new.f = 0; 1219 new.a = 1; 1220 new.overflow = 0; 1221 } while (!try_cmpxchg128(&te->header.val, &prev.val, new.val)); 1222 1223 /* Advance to next sample-data-block */ 1224 sdbt++; 1225 if (is_link_entry(sdbt)) 1226 sdbt = get_next_sdbt(sdbt); 1227 1228 /* Update event hardware registers */ 1229 TEAR_REG(hwc) = (unsigned long)sdbt; 1230 1231 /* Stop processing sample-data if all samples of the current 1232 * sample-data-block were flushed even if it was not full. 1233 */ 1234 if (flush_all && done) 1235 break; 1236 } 1237 1238 /* Account sample overflows in the event hardware structure */ 1239 if (sampl_overflow) 1240 OVERFLOW_REG(hwc) = DIV_ROUND_UP(OVERFLOW_REG(hwc) + 1241 sampl_overflow, 1 + num_sdb); 1242 1243 /* Perf_event_overflow() and perf_event_account_interrupt() limit 1244 * the interrupt rate to an upper limit. Roughly 1000 samples per 1245 * task tick. 1246 * Hitting this limit results in a large number 1247 * of throttled REF_REPORT_THROTTLE entries and the samples 1248 * are dropped. 1249 * Slightly increase the interval to avoid hitting this limit. 1250 */ 1251 if (event_overflow) { 1252 SAMPL_RATE(hwc) += DIV_ROUND_UP(SAMPL_RATE(hwc), 10); 1253 if (SAMPL_RATE(hwc) > cpuhw->qsi.max_sampl_rate) 1254 SAMPL_RATE(hwc) = cpuhw->qsi.max_sampl_rate; 1255 } 1256 } 1257 1258 static inline unsigned long aux_sdb_index(struct aux_buffer *aux, 1259 unsigned long i) 1260 { 1261 return i % aux->sfb.num_sdb; 1262 } 1263 1264 static inline unsigned long aux_sdb_num(unsigned long start, unsigned long end) 1265 { 1266 return end >= start ? end - start + 1 : 0; 1267 } 1268 1269 static inline unsigned long aux_sdb_num_alert(struct aux_buffer *aux) 1270 { 1271 return aux_sdb_num(aux->head, aux->alert_mark); 1272 } 1273 1274 static inline unsigned long aux_sdb_num_empty(struct aux_buffer *aux) 1275 { 1276 return aux_sdb_num(aux->head, aux->empty_mark); 1277 } 1278 1279 /* 1280 * Get trailer entry by index of SDB. 1281 */ 1282 static struct hws_trailer_entry *aux_sdb_trailer(struct aux_buffer *aux, 1283 unsigned long index) 1284 { 1285 unsigned long sdb; 1286 1287 index = aux_sdb_index(aux, index); 1288 sdb = aux->sdb_index[index]; 1289 return trailer_entry_ptr(sdb); 1290 } 1291 1292 /* 1293 * Finish sampling on the cpu. Called by cpumsf_pmu_del() with pmu 1294 * disabled. Collect the full SDBs in AUX buffer which have not reached 1295 * the point of alert indicator. And ignore the SDBs which are not 1296 * full. 1297 * 1298 * 1. Scan SDBs to see how much data is there and consume them. 1299 * 2. Remove alert indicator in the buffer. 1300 */ 1301 static void aux_output_end(struct perf_output_handle *handle) 1302 { 1303 unsigned long i, range_scan, idx; 1304 struct aux_buffer *aux; 1305 struct hws_trailer_entry *te; 1306 1307 aux = perf_get_aux(handle); 1308 if (!aux) 1309 return; 1310 1311 range_scan = aux_sdb_num_alert(aux); 1312 for (i = 0, idx = aux->head; i < range_scan; i++, idx++) { 1313 te = aux_sdb_trailer(aux, idx); 1314 if (!te->header.f) 1315 break; 1316 } 1317 /* i is num of SDBs which are full */ 1318 perf_aux_output_end(handle, i << PAGE_SHIFT); 1319 1320 /* Remove alert indicators in the buffer */ 1321 te = aux_sdb_trailer(aux, aux->alert_mark); 1322 te->header.a = 0; 1323 } 1324 1325 /* 1326 * Start sampling on the CPU. Called by cpumsf_pmu_add() when an event 1327 * is first added to the CPU or rescheduled again to the CPU. It is called 1328 * with pmu disabled. 1329 * 1330 * 1. Reset the trailer of SDBs to get ready for new data. 1331 * 2. Tell the hardware where to put the data by reset the SDBs buffer 1332 * head(tear/dear). 1333 */ 1334 static int aux_output_begin(struct perf_output_handle *handle, 1335 struct aux_buffer *aux, 1336 struct cpu_hw_sf *cpuhw) 1337 { 1338 unsigned long range, i, range_scan, idx, head, base, offset; 1339 struct hws_trailer_entry *te; 1340 1341 if (handle->head & ~PAGE_MASK) 1342 return -EINVAL; 1343 1344 aux->head = handle->head >> PAGE_SHIFT; 1345 range = (handle->size + 1) >> PAGE_SHIFT; 1346 if (range <= 1) 1347 return -ENOMEM; 1348 1349 /* 1350 * SDBs between aux->head and aux->empty_mark are already ready 1351 * for new data. range_scan is num of SDBs not within them. 1352 */ 1353 if (range > aux_sdb_num_empty(aux)) { 1354 range_scan = range - aux_sdb_num_empty(aux); 1355 idx = aux->empty_mark + 1; 1356 for (i = 0; i < range_scan; i++, idx++) { 1357 te = aux_sdb_trailer(aux, idx); 1358 te->header.f = 0; 1359 te->header.a = 0; 1360 te->header.overflow = 0; 1361 } 1362 /* Save the position of empty SDBs */ 1363 aux->empty_mark = aux->head + range - 1; 1364 } 1365 1366 /* Set alert indicator */ 1367 aux->alert_mark = aux->head + range/2 - 1; 1368 te = aux_sdb_trailer(aux, aux->alert_mark); 1369 te->header.a = 1; 1370 1371 /* Reset hardware buffer head */ 1372 head = aux_sdb_index(aux, aux->head); 1373 base = aux->sdbt_index[head / CPUM_SF_SDB_PER_TABLE]; 1374 offset = head % CPUM_SF_SDB_PER_TABLE; 1375 cpuhw->lsctl.tear = virt_to_phys((void *)base) + offset * sizeof(unsigned long); 1376 cpuhw->lsctl.dear = virt_to_phys((void *)aux->sdb_index[head]); 1377 1378 return 0; 1379 } 1380 1381 /* 1382 * Set alert indicator on SDB at index @alert_index while sampler is running. 1383 * 1384 * Return true if successfully. 1385 * Return false if full indicator is already set by hardware sampler. 1386 */ 1387 static bool aux_set_alert(struct aux_buffer *aux, unsigned long alert_index, 1388 unsigned long long *overflow) 1389 { 1390 union hws_trailer_header prev, new; 1391 struct hws_trailer_entry *te; 1392 1393 te = aux_sdb_trailer(aux, alert_index); 1394 prev.val = READ_ONCE_ALIGNED_128(te->header.val); 1395 do { 1396 new.val = prev.val; 1397 *overflow = prev.overflow; 1398 if (prev.f) { 1399 /* 1400 * SDB is already set by hardware. 1401 * Abort and try to set somewhere 1402 * behind. 1403 */ 1404 return false; 1405 } 1406 new.a = 1; 1407 new.overflow = 0; 1408 } while (!try_cmpxchg128(&te->header.val, &prev.val, new.val)); 1409 return true; 1410 } 1411 1412 /* 1413 * aux_reset_buffer() - Scan and setup SDBs for new samples 1414 * @aux: The AUX buffer to set 1415 * @range: The range of SDBs to scan started from aux->head 1416 * @overflow: Set to overflow count 1417 * 1418 * Set alert indicator on the SDB at index of aux->alert_mark. If this SDB is 1419 * marked as empty, check if it is already set full by the hardware sampler. 1420 * If yes, that means new data is already there before we can set an alert 1421 * indicator. Caller should try to set alert indicator to some position behind. 1422 * 1423 * Scan the SDBs in AUX buffer from behind aux->empty_mark. They are used 1424 * previously and have already been consumed by user space. Reset these SDBs 1425 * (clear full indicator and alert indicator) for new data. 1426 * If aux->alert_mark fall in this area, just set it. Overflow count is 1427 * recorded while scanning. 1428 * 1429 * SDBs between aux->head and aux->empty_mark are already reset at last time. 1430 * and ready for new samples. So scanning on this area could be skipped. 1431 * 1432 * Return true if alert indicator is set successfully and false if not. 1433 */ 1434 static bool aux_reset_buffer(struct aux_buffer *aux, unsigned long range, 1435 unsigned long long *overflow) 1436 { 1437 union hws_trailer_header prev, new; 1438 unsigned long i, range_scan, idx; 1439 unsigned long long orig_overflow; 1440 struct hws_trailer_entry *te; 1441 1442 if (range <= aux_sdb_num_empty(aux)) 1443 /* 1444 * No need to scan. All SDBs in range are marked as empty. 1445 * Just set alert indicator. Should check race with hardware 1446 * sampler. 1447 */ 1448 return aux_set_alert(aux, aux->alert_mark, overflow); 1449 1450 if (aux->alert_mark <= aux->empty_mark) 1451 /* 1452 * Set alert indicator on empty SDB. Should check race 1453 * with hardware sampler. 1454 */ 1455 if (!aux_set_alert(aux, aux->alert_mark, overflow)) 1456 return false; 1457 1458 /* 1459 * Scan the SDBs to clear full and alert indicator used previously. 1460 * Start scanning from one SDB behind empty_mark. If the new alert 1461 * indicator fall into this range, set it. 1462 */ 1463 range_scan = range - aux_sdb_num_empty(aux); 1464 idx = aux->empty_mark + 1; 1465 for (i = 0; i < range_scan; i++, idx++) { 1466 te = aux_sdb_trailer(aux, idx); 1467 prev.val = READ_ONCE_ALIGNED_128(te->header.val); 1468 do { 1469 new.val = prev.val; 1470 orig_overflow = prev.overflow; 1471 new.f = 0; 1472 new.overflow = 0; 1473 if (idx == aux->alert_mark) 1474 new.a = 1; 1475 else 1476 new.a = 0; 1477 } while (!try_cmpxchg128(&te->header.val, &prev.val, new.val)); 1478 *overflow += orig_overflow; 1479 } 1480 1481 /* Update empty_mark to new position */ 1482 aux->empty_mark = aux->head + range - 1; 1483 1484 return true; 1485 } 1486 1487 /* 1488 * Measurement alert handler for diagnostic mode sampling. 1489 */ 1490 static void hw_collect_aux(struct cpu_hw_sf *cpuhw) 1491 { 1492 struct aux_buffer *aux; 1493 int done = 0; 1494 unsigned long range = 0, size; 1495 unsigned long long overflow = 0; 1496 struct perf_output_handle *handle = &cpuhw->handle; 1497 unsigned long num_sdb; 1498 1499 aux = perf_get_aux(handle); 1500 if (!aux) 1501 return; 1502 1503 /* Inform user space new data arrived */ 1504 size = aux_sdb_num_alert(aux) << PAGE_SHIFT; 1505 debug_sprintf_event(sfdbg, 6, "%s #alert %ld\n", __func__, 1506 size >> PAGE_SHIFT); 1507 perf_aux_output_end(handle, size); 1508 1509 num_sdb = aux->sfb.num_sdb; 1510 while (!done) { 1511 /* Get an output handle */ 1512 aux = perf_aux_output_begin(handle, cpuhw->event); 1513 if (handle->size == 0) { 1514 pr_err("The AUX buffer with %lu pages for the " 1515 "diagnostic-sampling mode is full\n", 1516 num_sdb); 1517 break; 1518 } 1519 if (!aux) 1520 return; 1521 1522 /* Update head and alert_mark to new position */ 1523 aux->head = handle->head >> PAGE_SHIFT; 1524 range = (handle->size + 1) >> PAGE_SHIFT; 1525 if (range == 1) 1526 aux->alert_mark = aux->head; 1527 else 1528 aux->alert_mark = aux->head + range/2 - 1; 1529 1530 if (aux_reset_buffer(aux, range, &overflow)) { 1531 if (!overflow) { 1532 done = 1; 1533 break; 1534 } 1535 size = range << PAGE_SHIFT; 1536 perf_aux_output_end(&cpuhw->handle, size); 1537 pr_err("Sample data caused the AUX buffer with %lu " 1538 "pages to overflow\n", aux->sfb.num_sdb); 1539 } else { 1540 size = aux_sdb_num_alert(aux) << PAGE_SHIFT; 1541 perf_aux_output_end(&cpuhw->handle, size); 1542 } 1543 } 1544 } 1545 1546 /* 1547 * Callback when freeing AUX buffers. 1548 */ 1549 static void aux_buffer_free(void *data) 1550 { 1551 struct aux_buffer *aux = data; 1552 unsigned long i, num_sdbt; 1553 1554 if (!aux) 1555 return; 1556 1557 /* Free SDBT. SDB is freed by the caller */ 1558 num_sdbt = aux->sfb.num_sdbt; 1559 for (i = 0; i < num_sdbt; i++) 1560 free_page(aux->sdbt_index[i]); 1561 1562 kfree(aux->sdbt_index); 1563 kfree(aux->sdb_index); 1564 kfree(aux); 1565 } 1566 1567 static void aux_sdb_init(unsigned long sdb) 1568 { 1569 struct hws_trailer_entry *te; 1570 1571 te = trailer_entry_ptr(sdb); 1572 1573 /* Save clock base */ 1574 te->clock_base = 1; 1575 te->progusage2 = tod_clock_base.tod; 1576 } 1577 1578 /* 1579 * aux_buffer_setup() - Setup AUX buffer for diagnostic mode sampling 1580 * @event: Event the buffer is setup for, event->cpu == -1 means current 1581 * @pages: Array of pointers to buffer pages passed from perf core 1582 * @nr_pages: Total pages 1583 * @snapshot: Flag for snapshot mode 1584 * 1585 * This is the callback when setup an event using AUX buffer. Perf tool can 1586 * trigger this by an additional mmap() call on the event. Unlike the buffer 1587 * for basic samples, AUX buffer belongs to the event. It is scheduled with 1588 * the task among online cpus when it is a per-thread event. 1589 * 1590 * Return the private AUX buffer structure if success or NULL if fails. 1591 */ 1592 static void *aux_buffer_setup(struct perf_event *event, void **pages, 1593 int nr_pages, bool snapshot) 1594 { 1595 struct sf_buffer *sfb; 1596 struct aux_buffer *aux; 1597 unsigned long *new, *tail; 1598 int i, n_sdbt; 1599 1600 if (!nr_pages || !pages) 1601 return NULL; 1602 1603 if (nr_pages > CPUM_SF_MAX_SDB * CPUM_SF_SDB_DIAG_FACTOR) { 1604 pr_err("AUX buffer size (%i pages) is larger than the " 1605 "maximum sampling buffer limit\n", 1606 nr_pages); 1607 return NULL; 1608 } else if (nr_pages < CPUM_SF_MIN_SDB * CPUM_SF_SDB_DIAG_FACTOR) { 1609 pr_err("AUX buffer size (%i pages) is less than the " 1610 "minimum sampling buffer limit\n", 1611 nr_pages); 1612 return NULL; 1613 } 1614 1615 /* Allocate aux_buffer struct for the event */ 1616 aux = kzalloc_obj(struct aux_buffer); 1617 if (!aux) 1618 goto no_aux; 1619 sfb = &aux->sfb; 1620 1621 /* Allocate sdbt_index for fast reference */ 1622 n_sdbt = DIV_ROUND_UP(nr_pages, CPUM_SF_SDB_PER_TABLE); 1623 aux->sdbt_index = kmalloc_array(n_sdbt, sizeof(void *), GFP_KERNEL); 1624 if (!aux->sdbt_index) 1625 goto no_sdbt_index; 1626 1627 /* Allocate sdb_index for fast reference */ 1628 aux->sdb_index = kmalloc_array(nr_pages, sizeof(void *), GFP_KERNEL); 1629 if (!aux->sdb_index) 1630 goto no_sdb_index; 1631 1632 /* Allocate the first SDBT */ 1633 sfb->num_sdbt = 0; 1634 sfb->sdbt = (unsigned long *)get_zeroed_page(GFP_KERNEL); 1635 if (!sfb->sdbt) 1636 goto no_sdbt; 1637 aux->sdbt_index[sfb->num_sdbt++] = (unsigned long)sfb->sdbt; 1638 tail = sfb->tail = sfb->sdbt; 1639 1640 /* 1641 * Link the provided pages of AUX buffer to SDBT. 1642 * Allocate SDBT if needed. 1643 */ 1644 for (i = 0; i < nr_pages; i++, tail++) { 1645 if (require_table_link(tail)) { 1646 new = (unsigned long *)get_zeroed_page(GFP_KERNEL); 1647 if (!new) 1648 goto no_sdbt; 1649 aux->sdbt_index[sfb->num_sdbt++] = (unsigned long)new; 1650 /* Link current page to tail of chain */ 1651 *tail = virt_to_phys(new) + 1; 1652 tail = new; 1653 } 1654 /* Tail is the entry in a SDBT */ 1655 *tail = virt_to_phys(pages[i]); 1656 aux->sdb_index[i] = (unsigned long)pages[i]; 1657 aux_sdb_init((unsigned long)pages[i]); 1658 } 1659 sfb->num_sdb = nr_pages; 1660 1661 /* Link the last entry in the SDBT to the first SDBT */ 1662 *tail = virt_to_phys(sfb->sdbt) + 1; 1663 sfb->tail = tail; 1664 1665 /* 1666 * Initial all SDBs are zeroed. Mark it as empty. 1667 * So there is no need to clear the full indicator 1668 * when this event is first added. 1669 */ 1670 aux->empty_mark = sfb->num_sdb - 1; 1671 1672 return aux; 1673 1674 no_sdbt: 1675 /* SDBs (AUX buffer pages) are freed by caller */ 1676 for (i = 0; i < sfb->num_sdbt; i++) 1677 free_page(aux->sdbt_index[i]); 1678 kfree(aux->sdb_index); 1679 no_sdb_index: 1680 kfree(aux->sdbt_index); 1681 no_sdbt_index: 1682 kfree(aux); 1683 no_aux: 1684 return NULL; 1685 } 1686 1687 static void cpumsf_pmu_read(struct perf_event *event) 1688 { 1689 /* Nothing to do ... updates are interrupt-driven */ 1690 } 1691 1692 /* Check if the new sampling period/frequency is appropriate. 1693 * 1694 * Return non-zero on error and zero on passed checks. 1695 */ 1696 static int cpumsf_pmu_check_period(struct perf_event *event, u64 value) 1697 { 1698 struct hws_qsi_info_block si; 1699 unsigned long rate; 1700 bool do_freq; 1701 1702 memset(&si, 0, sizeof(si)); 1703 if (event->cpu == -1) { 1704 qsi(&si); 1705 } else { 1706 /* Event is pinned to a particular CPU, retrieve the per-CPU 1707 * sampling structure for accessing the CPU-specific QSI. 1708 */ 1709 struct cpu_hw_sf *cpuhw = &per_cpu(cpu_hw_sf, event->cpu); 1710 1711 si = cpuhw->qsi; 1712 } 1713 1714 do_freq = !!SAMPL_FREQ_MODE(&event->hw); 1715 rate = getrate(do_freq, value, &si); 1716 if (!rate) 1717 return -EINVAL; 1718 1719 event->attr.sample_period = rate; 1720 SAMPL_RATE(&event->hw) = rate; 1721 hw_init_period(&event->hw, SAMPL_RATE(&event->hw)); 1722 return 0; 1723 } 1724 1725 /* Activate sampling control. 1726 * Next call of pmu_enable() starts sampling. 1727 */ 1728 static void cpumsf_pmu_start(struct perf_event *event, int flags) 1729 { 1730 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf); 1731 1732 if (!(event->hw.state & PERF_HES_STOPPED)) 1733 return; 1734 perf_pmu_disable(event->pmu); 1735 event->hw.state = 0; 1736 cpuhw->lsctl.cs = 1; 1737 if (SAMPL_DIAG_MODE(&event->hw)) 1738 cpuhw->lsctl.cd = 1; 1739 perf_pmu_enable(event->pmu); 1740 } 1741 1742 /* Deactivate sampling control. 1743 * Next call of pmu_enable() stops sampling. 1744 */ 1745 static void cpumsf_pmu_stop(struct perf_event *event, int flags) 1746 { 1747 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf); 1748 1749 if (event->hw.state & PERF_HES_STOPPED) 1750 return; 1751 1752 perf_pmu_disable(event->pmu); 1753 cpuhw->lsctl.cs = 0; 1754 cpuhw->lsctl.cd = 0; 1755 event->hw.state |= PERF_HES_STOPPED; 1756 1757 if ((flags & PERF_EF_UPDATE) && !(event->hw.state & PERF_HES_UPTODATE)) { 1758 /* CPU hotplug off removes SDBs. No samples to extract. */ 1759 if (cpuhw->flags & PMU_F_RESERVED) 1760 hw_perf_event_update(event, 1); 1761 event->hw.state |= PERF_HES_UPTODATE; 1762 } 1763 perf_pmu_enable(event->pmu); 1764 } 1765 1766 static int cpumsf_pmu_add(struct perf_event *event, int flags) 1767 { 1768 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf); 1769 struct aux_buffer *aux; 1770 int err = 0; 1771 1772 if (cpuhw->flags & PMU_F_IN_USE) 1773 return -EAGAIN; 1774 1775 if (!SAMPL_DIAG_MODE(&event->hw) && !sf_buffer_available(cpuhw)) 1776 return -EINVAL; 1777 1778 perf_pmu_disable(event->pmu); 1779 1780 event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED; 1781 1782 /* Set up sampling controls. Always program the sampling register 1783 * using the SDB-table start. Reset TEAR_REG event hardware register 1784 * that is used by hw_perf_event_update() to store the sampling buffer 1785 * position after samples have been flushed. 1786 */ 1787 cpuhw->lsctl.s = 0; 1788 cpuhw->lsctl.h = 1; 1789 cpuhw->lsctl.interval = SAMPL_RATE(&event->hw); 1790 if (!SAMPL_DIAG_MODE(&event->hw)) { 1791 cpuhw->lsctl.tear = virt_to_phys(cpuhw->sfb.sdbt); 1792 cpuhw->lsctl.dear = *(unsigned long *)cpuhw->sfb.sdbt; 1793 TEAR_REG(&event->hw) = (unsigned long)cpuhw->sfb.sdbt; 1794 } 1795 1796 /* Ensure sampling functions are in the disabled state. If disabled, 1797 * switch on sampling enable control. */ 1798 if (WARN_ON_ONCE(cpuhw->lsctl.es == 1 || cpuhw->lsctl.ed == 1)) { 1799 err = -EAGAIN; 1800 goto out; 1801 } 1802 if (SAMPL_DIAG_MODE(&event->hw)) { 1803 aux = perf_aux_output_begin(&cpuhw->handle, event); 1804 if (!aux) { 1805 err = -EINVAL; 1806 goto out; 1807 } 1808 err = aux_output_begin(&cpuhw->handle, aux, cpuhw); 1809 if (err) 1810 goto out; 1811 cpuhw->lsctl.ed = 1; 1812 } 1813 cpuhw->lsctl.es = 1; 1814 1815 /* Set in_use flag and store event */ 1816 cpuhw->event = event; 1817 cpuhw->flags |= PMU_F_IN_USE; 1818 1819 if (flags & PERF_EF_START) 1820 cpumsf_pmu_start(event, PERF_EF_RELOAD); 1821 out: 1822 perf_event_update_userpage(event); 1823 perf_pmu_enable(event->pmu); 1824 return err; 1825 } 1826 1827 static void cpumsf_pmu_del(struct perf_event *event, int flags) 1828 { 1829 struct cpu_hw_sf *cpuhw = this_cpu_ptr(&cpu_hw_sf); 1830 1831 perf_pmu_disable(event->pmu); 1832 cpumsf_pmu_stop(event, PERF_EF_UPDATE); 1833 1834 cpuhw->lsctl.es = 0; 1835 cpuhw->lsctl.ed = 0; 1836 cpuhw->flags &= ~PMU_F_IN_USE; 1837 cpuhw->event = NULL; 1838 1839 if (SAMPL_DIAG_MODE(&event->hw)) 1840 aux_output_end(&cpuhw->handle); 1841 perf_event_update_userpage(event); 1842 perf_pmu_enable(event->pmu); 1843 } 1844 1845 CPUMF_EVENT_ATTR(SF, SF_CYCLES_BASIC, PERF_EVENT_CPUM_SF); 1846 CPUMF_EVENT_ATTR(SF, SF_CYCLES_BASIC_DIAG, PERF_EVENT_CPUM_SF_DIAG); 1847 1848 /* Attribute list for CPU_SF. 1849 * 1850 * The availablitiy depends on the CPU_MF sampling facility authorization 1851 * for basic + diagnositic samples. This is determined at initialization 1852 * time by the sampling facility device driver. 1853 * If the authorization for basic samples is turned off, it should be 1854 * also turned off for diagnostic sampling. 1855 * 1856 * During initialization of the device driver, check the authorization 1857 * level for diagnostic sampling and installs the attribute 1858 * file for diagnostic sampling if necessary. 1859 * 1860 * For now install a placeholder to reference all possible attributes: 1861 * SF_CYCLES_BASIC and SF_CYCLES_BASIC_DIAG. 1862 * Add another entry for the final NULL pointer. 1863 */ 1864 enum { 1865 SF_CYCLES_BASIC_ATTR_IDX = 0, 1866 SF_CYCLES_BASIC_DIAG_ATTR_IDX, 1867 SF_CYCLES_ATTR_MAX 1868 }; 1869 1870 static struct attribute *cpumsf_pmu_events_attr[SF_CYCLES_ATTR_MAX + 1] = { 1871 [SF_CYCLES_BASIC_ATTR_IDX] = CPUMF_EVENT_PTR(SF, SF_CYCLES_BASIC) 1872 }; 1873 1874 PMU_FORMAT_ATTR(event, "config:0-63"); 1875 1876 static struct attribute *cpumsf_pmu_format_attr[] = { 1877 &format_attr_event.attr, 1878 NULL, 1879 }; 1880 1881 static struct attribute_group cpumsf_pmu_events_group = { 1882 .name = "events", 1883 .attrs = cpumsf_pmu_events_attr, 1884 }; 1885 1886 static struct attribute_group cpumsf_pmu_format_group = { 1887 .name = "format", 1888 .attrs = cpumsf_pmu_format_attr, 1889 }; 1890 1891 static const struct attribute_group *cpumsf_pmu_attr_groups[] = { 1892 &cpumsf_pmu_events_group, 1893 &cpumsf_pmu_format_group, 1894 NULL, 1895 }; 1896 1897 static struct pmu cpumf_sampling = { 1898 .pmu_enable = cpumsf_pmu_enable, 1899 .pmu_disable = cpumsf_pmu_disable, 1900 1901 .event_init = cpumsf_pmu_event_init, 1902 .add = cpumsf_pmu_add, 1903 .del = cpumsf_pmu_del, 1904 1905 .start = cpumsf_pmu_start, 1906 .stop = cpumsf_pmu_stop, 1907 .read = cpumsf_pmu_read, 1908 1909 .attr_groups = cpumsf_pmu_attr_groups, 1910 1911 .setup_aux = aux_buffer_setup, 1912 .free_aux = aux_buffer_free, 1913 1914 .check_period = cpumsf_pmu_check_period, 1915 }; 1916 1917 static void cpumf_measurement_alert(struct ext_code ext_code, 1918 unsigned int alert, unsigned long unused) 1919 { 1920 struct cpu_hw_sf *cpuhw; 1921 1922 if (!(alert & CPU_MF_INT_SF_MASK)) 1923 return; 1924 inc_irq_stat(IRQEXT_CMS); 1925 cpuhw = this_cpu_ptr(&cpu_hw_sf); 1926 1927 /* Measurement alerts are shared and might happen when the PMU 1928 * is not reserved. Ignore these alerts in this case. */ 1929 if (!(cpuhw->flags & PMU_F_RESERVED)) 1930 return; 1931 1932 /* The processing below must take care of multiple alert events that 1933 * might be indicated concurrently. */ 1934 1935 /* Program alert request */ 1936 if (alert & CPU_MF_INT_SF_PRA) { 1937 if (cpuhw->flags & PMU_F_IN_USE) { 1938 if (SAMPL_DIAG_MODE(&cpuhw->event->hw)) 1939 hw_collect_aux(cpuhw); 1940 else 1941 hw_perf_event_update(cpuhw->event, 0); 1942 } 1943 } 1944 1945 /* Report measurement alerts only for non-PRA codes */ 1946 if (alert != CPU_MF_INT_SF_PRA) 1947 debug_sprintf_event(sfdbg, 6, "%s alert %#x\n", __func__, 1948 alert); 1949 1950 /* Sampling authorization change request */ 1951 if (alert & CPU_MF_INT_SF_SACA) 1952 qsi(&cpuhw->qsi); 1953 1954 /* Loss of sample data due to high-priority machine activities */ 1955 if (alert & CPU_MF_INT_SF_LSDA) { 1956 pr_err("Sample data was lost\n"); 1957 cpuhw->flags |= PMU_F_ERR_LSDA; 1958 sf_disable(); 1959 } 1960 1961 /* Invalid sampling buffer entry */ 1962 if (alert & (CPU_MF_INT_SF_IAE|CPU_MF_INT_SF_ISE)) { 1963 pr_err("A sampling buffer entry is incorrect (alert=%#x)\n", 1964 alert); 1965 cpuhw->flags |= PMU_F_ERR_IBE; 1966 sf_disable(); 1967 } 1968 } 1969 1970 static int cpusf_pmu_setup(unsigned int cpu, int flags) 1971 { 1972 /* Ignore the notification if no events are scheduled on the PMU. 1973 * This might be racy... 1974 */ 1975 if (!refcount_read(&num_events)) 1976 return 0; 1977 1978 local_irq_disable(); 1979 setup_pmc_cpu(&flags); 1980 local_irq_enable(); 1981 return 0; 1982 } 1983 1984 static int s390_pmu_sf_online_cpu(unsigned int cpu) 1985 { 1986 return cpusf_pmu_setup(cpu, PMC_INIT); 1987 } 1988 1989 static int s390_pmu_sf_offline_cpu(unsigned int cpu) 1990 { 1991 return cpusf_pmu_setup(cpu, PMC_RELEASE); 1992 } 1993 1994 static int param_get_sfb_size(char *buffer, const struct kernel_param *kp) 1995 { 1996 if (!cpum_sf_avail()) 1997 return -ENODEV; 1998 return sprintf(buffer, "%lu,%lu", CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB); 1999 } 2000 2001 static int param_set_sfb_size(const char *val, const struct kernel_param *kp) 2002 { 2003 int rc; 2004 unsigned long min, max; 2005 2006 if (!cpum_sf_avail()) 2007 return -ENODEV; 2008 if (!val || !strlen(val)) 2009 return -EINVAL; 2010 2011 /* Valid parameter values: "min,max" or "max" */ 2012 min = CPUM_SF_MIN_SDB; 2013 max = CPUM_SF_MAX_SDB; 2014 if (strchr(val, ',')) 2015 rc = (sscanf(val, "%lu,%lu", &min, &max) == 2) ? 0 : -EINVAL; 2016 else 2017 rc = kstrtoul(val, 10, &max); 2018 2019 if (min < 2 || min >= max || max > get_num_physpages()) 2020 rc = -EINVAL; 2021 if (rc) 2022 return rc; 2023 2024 sfb_set_limits(min, max); 2025 pr_info("The sampling buffer limits have changed to: " 2026 "min %lu max %lu (diag %lu)\n", 2027 CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB, CPUM_SF_SDB_DIAG_FACTOR); 2028 return 0; 2029 } 2030 2031 #define param_check_sfb_size(name, p) __param_check(name, p, void) 2032 static const struct kernel_param_ops param_ops_sfb_size = { 2033 .set = param_set_sfb_size, 2034 .get = param_get_sfb_size, 2035 }; 2036 2037 enum { 2038 RS_INIT_FAILURE_BSDES = 2, /* Bad basic sampling size */ 2039 RS_INIT_FAILURE_ALRT = 3, /* IRQ registration failure */ 2040 RS_INIT_FAILURE_PERF = 4 /* PMU registration failure */ 2041 }; 2042 2043 static void __init pr_cpumsf_err(unsigned int reason) 2044 { 2045 pr_err("Sampling facility support for perf is not available: " 2046 "reason %#x\n", reason); 2047 } 2048 2049 static int __init init_cpum_sampling_pmu(void) 2050 { 2051 struct hws_qsi_info_block si; 2052 int err; 2053 2054 if (!cpum_sf_avail()) 2055 return -ENODEV; 2056 2057 memset(&si, 0, sizeof(si)); 2058 qsi(&si); 2059 if (!si.as && !si.ad) 2060 return -ENODEV; 2061 2062 if (si.bsdes != sizeof(struct hws_basic_entry)) { 2063 pr_cpumsf_err(RS_INIT_FAILURE_BSDES); 2064 return -EINVAL; 2065 } 2066 2067 if (si.ad) { 2068 sfb_set_limits(CPUM_SF_MIN_SDB, CPUM_SF_MAX_SDB); 2069 /* Sampling of diagnostic data authorized, 2070 * install event into attribute list of PMU device. 2071 */ 2072 cpumsf_pmu_events_attr[SF_CYCLES_BASIC_DIAG_ATTR_IDX] = 2073 CPUMF_EVENT_PTR(SF, SF_CYCLES_BASIC_DIAG); 2074 } 2075 2076 sfdbg = debug_register("cpum_sf", 2, 1, 80); 2077 if (!sfdbg) { 2078 pr_err("Registering for s390dbf failed\n"); 2079 return -ENOMEM; 2080 } 2081 debug_register_view(sfdbg, &debug_sprintf_view); 2082 2083 err = register_external_irq(EXT_IRQ_MEASURE_ALERT, 2084 cpumf_measurement_alert); 2085 if (err) { 2086 pr_cpumsf_err(RS_INIT_FAILURE_ALRT); 2087 debug_unregister(sfdbg); 2088 goto out; 2089 } 2090 2091 err = perf_pmu_register(&cpumf_sampling, "cpum_sf", PERF_TYPE_RAW); 2092 if (err) { 2093 pr_cpumsf_err(RS_INIT_FAILURE_PERF); 2094 unregister_external_irq(EXT_IRQ_MEASURE_ALERT, 2095 cpumf_measurement_alert); 2096 debug_unregister(sfdbg); 2097 goto out; 2098 } 2099 2100 cpuhp_setup_state(CPUHP_AP_PERF_S390_SF_ONLINE, "perf/s390/sf:online", 2101 s390_pmu_sf_online_cpu, s390_pmu_sf_offline_cpu); 2102 out: 2103 return err; 2104 } 2105 2106 arch_initcall(init_cpum_sampling_pmu); 2107 core_param(cpum_sfb_size, CPUM_SF_MAX_SDB, sfb_size, 0644); 2108