Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 | /* * Copyright (c) 2012-2016 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #include <arm/cpu_data_internal.h> #include <arm/cpu_internal.h> #include <kern/kalloc.h> #include <kern/kpc.h> #include <kern/thread.h> #include <kern/processor.h> #include <mach/mach_types.h> #include <machine/machine_routines.h> #include <stdint.h> #include <sys/errno.h> #if MONOTONIC #include <kern/monotonic.h> #endif /* MONOTONIC */ void kpc_pmi_handler(unsigned int ctr); /* * PMCs 8 and 9 were added to Hurricane and to maintain the existing bit * positions of the other PMCs, their configuration bits start at position 32. */ #define PMCR_PMC_8_9_OFFSET (32) #define PMCR_PMC_8_9_SHIFT(PMC) (((PMC) - 8) + PMCR_PMC_8_9_OFFSET) #define PMCR_PMC_SHIFT(PMC) (((PMC) <= 7) ? (PMC) : \ PMCR_PMC_8_9_SHIFT(PMC)) /* * PMCR0 controls enabling, interrupts, and overflow of performance counters. */ /* PMC is enabled */ #define PMCR0_PMC_ENABLE_MASK(PMC) (UINT64_C(0x1) << PMCR_PMC_SHIFT(PMC)) #define PMCR0_PMC_DISABLE_MASK(PMC) (~PMCR0_PMC_ENABLE_MASK(PMC)) /* how interrupts are generated on PMIs */ #define PMCR0_INTGEN_SHIFT (8) #define PMCR0_INTGEN_MASK (UINT64_C(0x7) << PMCR0_INTGEN_SHIFT) #define PMCR0_INTGEN_OFF (UINT64_C(0) << PMCR0_INTGEN_SHIFT) #define PMCR0_INTGEN_PMI (UINT64_C(1) << PMCR0_INTGEN_SHIFT) #define PMCR0_INTGEN_AIC (UINT64_C(2) << PMCR0_INTGEN_SHIFT) #define PMCR0_INTGEN_DBG_HLT (UINT64_C(3) << PMCR0_INTGEN_SHIFT) #define PMCR0_INTGEN_FIQ (UINT64_C(4) << PMCR0_INTGEN_SHIFT) /* 10 unused */ /* set by hardware if PMI was generated */ #define PMCR0_PMAI_SHIFT (11) #define PMCR0_PMAI_MASK (UINT64_C(1) << PMCR0_PMAI_SHIFT) /* overflow on a PMC generates an interrupt */ #define PMCR0_PMI_OFFSET (12) #define PMCR0_PMI_SHIFT(PMC) (PMCR0_PMI_OFFSET + PMCR_PMC_SHIFT(PMC)) #define PMCR0_PMI_ENABLE_MASK(PMC) (UINT64_C(1) << PMCR0_PMI_SHIFT(PMC)) #define PMCR0_PMI_DISABLE_MASK(PMC) (~PMCR0_PMI_ENABLE_MASK(PMC)) /* disable counting when a PMI is signaled (except for AIC interrupts) */ #define PMCR0_DISCNT_SHIFT (20) #define PMCR0_DISCNT_ENABLE_MASK (UINT64_C(1) << PMCR0_DISCNT_SHIFT) #define PMCR0_DISCNT_DISABLE_MASK (~PMCR0_DISCNT_ENABLE_MASK) /* 21 unused */ /* block PMIs until ERET retires */ #define PMCR0_WFRFE_SHIFT (22) #define PMCR0_WFRFE_ENABLE_MASK (UINT64_C(1) << PMCR0_WFRE_SHIFT) #define PMCR0_WFRFE_DISABLE_MASK (~PMCR0_WFRFE_ENABLE_MASK) /* count global L2C events */ #define PMCR0_L2CGLOBAL_SHIFT (23) #define PMCR0_L2CGLOBAL_ENABLE_MASK (UINT64_C(1) << PMCR0_L2CGLOBAL_SHIFT) #define PMCR0_L2CGLOBAL_DISABLE_MASK (~PMCR0_L2CGLOBAL_ENABLE_MASK) /* allow user mode access to configuration registers */ #define PMCR0_USEREN_SHIFT (30) #define PMCR0_USEREN_ENABLE_MASK (UINT64_C(1) << PMCR0_USEREN_SHIFT) #define PMCR0_USEREN_DISABLE_MASK (~PMCR0_USEREN_ENABLE_MASK) /* force the CPMU clocks in case of a clocking bug */ #define PMCR0_CLKEN_SHIFT (31) #define PMCR0_CLKEN_ENABLE_MASK (UINT64_C(1) << PMCR0_USEREN_SHIFT) #define PMCR0_CLKEN_DISABLE_MASK (~PMCR0_CLKEN_ENABLE_MASK) /* 32 - 44 mirror the low bits for PMCs 8 and 9 */ /* PMCR1 enables counters in different processor modes */ #define PMCR1_EL0_A32_OFFSET (0) #define PMCR1_EL0_A64_OFFSET (8) #define PMCR1_EL1_A64_OFFSET (16) #define PMCR1_EL3_A64_OFFSET (24) #define PMCR1_EL0_A32_SHIFT(PMC) (PMCR1_EL0_A32_OFFSET + PMCR_PMC_SHIFT(PMC)) #define PMCR1_EL0_A64_SHIFT(PMC) (PMCR1_EL0_A64_OFFSET + PMCR_PMC_SHIFT(PMC)) #define PMCR1_EL1_A64_SHIFT(PMC) (PMCR1_EL1_A64_OFFSET + PMCR_PMC_SHIFT(PMC)) #define PMCR1_EL3_A64_SHIFT(PMC) (PMCR1_EL0_A64_OFFSET + PMCR_PMC_SHIFT(PMC)) #define PMCR1_EL0_A32_ENABLE_MASK(PMC) (UINT64_C(1) << PMCR1_EL0_A32_SHIFT(PMC)) #define PMCR1_EL0_A64_ENABLE_MASK(PMC) (UINT64_C(1) << PMCR1_EL0_A64_SHIFT(PMC)) #define PMCR1_EL1_A64_ENABLE_MASK(PMC) (UINT64_C(1) << PMCR1_EL1_A64_SHIFT(PMC)) /* PMCR1_EL3_A64 is not supported on PMCs 8 and 9 */ #if NO_MONITOR #define PMCR1_EL3_A64_ENABLE_MASK(PMC) UINT64_C(0) #else #define PMCR1_EL3_A64_ENABLE_MASK(PMC) (UINT64_C(1) << PMCR1_EL3_A64_SHIFT(PMC)) #endif #define PMCR1_EL_ALL_ENABLE_MASK(PMC) (PMCR1_EL0_A32_ENABLE_MASK(PMC) | \ PMCR1_EL0_A64_ENABLE_MASK(PMC) | \ PMCR1_EL1_A64_ENABLE_MASK(PMC) | \ PMCR1_EL3_A64_ENABLE_MASK(PMC)) #define PMCR1_EL_ALL_DISABLE_MASK(PMC) (~PMCR1_EL_ALL_ENABLE_MASK(PMC)) /* PMESR0 and PMESR1 are event selection registers */ /* PMESR0 selects which event is counted on PMCs 2, 3, 4, and 5 */ /* PMESR1 selects which event is counted on PMCs 6, 7, 8, and 9 */ #define PMESR_PMC_WIDTH (8) #define PMESR_PMC_MASK (UINT8_MAX) #define PMESR_SHIFT(PMC, OFF) (8 * ((PMC) - (OFF))) #define PMESR_EVT_MASK(PMC, OFF) (PMESR_PMC_MASK << PMESR_SHIFT(PMC, OFF)) #define PMESR_EVT_CLEAR(PMC, OFF) (~PMESR_EVT_MASK(PMC, OFF)) #define PMESR_EVT_DECODE(PMESR, PMC, OFF) \ (((PMESR) >> PMESR_SHIFT(PMC, OFF)) & PMESR_PMC_MASK) #define PMESR_EVT_ENCODE(EVT, PMC, OFF) \ (((EVT) & PMESR_PMC_MASK) << PMESR_SHIFT(PMC, OFF)) /* system registers in the CPMU */ #define SREG_PMCR0 "S3_1_c15_c0_0" #define SREG_PMCR1 "S3_1_c15_c1_0" #define SREG_PMCR2 "S3_1_c15_c2_0" #define SREG_PMCR3 "S3_1_c15_c3_0" #define SREG_PMCR4 "S3_1_c15_c4_0" #define SREG_PMESR0 "S3_1_c15_c5_0" #define SREG_PMESR1 "S3_1_c15_c6_0" #define SREG_PMSR "S3_1_c15_c13_0" #define SREG_OPMAT0 "S3_1_c15_c7_0" #define SREG_OPMAT1 "S3_1_c15_c8_0" #define SREG_OPMSK0 "S3_1_c15_c9_0" #define SREG_OPMSK1 "S3_1_c15_c10_0" #define SREG_PMC0 "S3_2_c15_c0_0" #define SREG_PMC1 "S3_2_c15_c1_0" #define SREG_PMC2 "S3_2_c15_c2_0" #define SREG_PMC3 "S3_2_c15_c3_0" #define SREG_PMC4 "S3_2_c15_c4_0" #define SREG_PMC5 "S3_2_c15_c5_0" #define SREG_PMC6 "S3_2_c15_c6_0" #define SREG_PMC7 "S3_2_c15_c7_0" #define SREG_PMC8 "S3_2_c15_c9_0" #define SREG_PMC9 "S3_2_c15_c10_0" #if !defined(APPLECYCLONE) #define SREG_PMMMAP "S3_2_c15_c15_0" #define SREG_PMTRHLD2 "S3_2_c15_c14_0" #define SREG_PMTRHLD4 "S3_2_c15_c13_0" #define SREG_PMTRHLD6 "S3_2_c15_c12_0" #endif /* * The low 8 bits of a configuration words select the event to program on * PMESR{0,1}. Bits 16-19 are mapped to PMCR1 bits. */ #define CFGWORD_EL0A32EN_MASK (0x10000) #define CFGWORD_EL0A64EN_MASK (0x20000) #define CFGWORD_EL1EN_MASK (0x40000) #define CFGWORD_EL3EN_MASK (0x80000) #define CFGWORD_ALLMODES_MASK (0xf0000) /* ACC offsets for PIO */ #define ACC_CPMU_PMC0_OFFSET (0x200) #define ACC_CPMU_PMC8_OFFSET (0x280) /* * Macros for reading and writing system registers. * * SR must be one of the SREG_* defines above. */ #define SREG_WRITE(SR, V) __asm__ volatile("msr " SR ", %0 ; isb" : : "r"(V)) #define SREG_READ(SR) ({ uint64_t VAL; \ __asm__ volatile("mrs %0, " SR : "=r"(VAL)); \ VAL; }) /* * Configuration registers that can be controlled by RAWPMU: * * All: PMCR2-4, OPMAT0-1, OPMSK0-1. * Typhoon/Twister/Hurricane: PMMMAP, PMTRHLD2/4/6. */ #if defined(APPLECYCLONE) #define RAWPMU_CONFIG_COUNT 7 #else #define RAWPMU_CONFIG_COUNT 11 #endif /* TODO: allocate dynamically */ static uint64_t saved_PMCR[MAX_CPUS][2]; static uint64_t saved_PMESR[MAX_CPUS][2]; static uint64_t saved_RAWPMU[MAX_CPUS][RAWPMU_CONFIG_COUNT]; static uint64_t saved_counter[MAX_CPUS][KPC_MAX_COUNTERS]; static uint64_t kpc_running_cfg_pmc_mask = 0; static uint32_t kpc_running_classes = 0; static uint32_t kpc_configured = 0; /* * The whitelist is disabled by default on development/debug kernel. This can * be changed via the kpc.disable_whitelist sysctl. The whitelist is enabled on * release kernel and cannot be disabled. */ #if DEVELOPMENT || DEBUG static boolean_t whitelist_disabled = TRUE; #else static boolean_t whitelist_disabled = FALSE; #endif /* List of counter events that are allowed externally */ static kpc_config_t whitelist[] = { 0, /* NO_EVENT */ #if defined(APPLECYCLONE) 0x02, /* CORE_CYCLE */ 0x19, /* BIU_UPSTREAM_CYCLE */ 0x1a, /* BIU_DOWNSTREAM_CYCLE */ 0x22, /* L2C_AGENT_LD */ 0x23, /* L2C_AGENT_LD_MISS */ 0x24, /* L2C_AGENT_ST */ 0x25, /* L2C_AGENT_ST_MISS */ 0x78, /* INST_A32 */ 0x79, /* INST_THUMB */ 0x7a, /* INST_A64 */ 0x7b, /* INST_BRANCH */ 0xb4, /* SYNC_DC_LOAD_MISS */ 0xb5, /* SYNC_DC_STORE_MISS */ 0xb6, /* SYNC_DTLB_MISS */ 0xb9, /* SYNC_ST_HIT_YNGR_LD */ 0xc0, /* SYNC_BR_ANY_MISP */ 0xce, /* FED_IC_MISS_DEM */ 0xcf, /* FED_ITLB_MISS */ #elif defined(APPLETYPHOON) 0x02, /* CORE_CYCLE */ 0x13, /* BIU_UPSTREAM_CYCLE */ 0x14, /* BIU_DOWNSTREAM_CYCLE */ 0x1a, /* L2C_AGENT_LD */ 0x1b, /* L2C_AGENT_LD_MISS */ 0x1c, /* L2C_AGENT_ST */ 0x1d, /* L2C_AGENT_ST_MISS */ 0x8a, /* INST_A32 */ 0x8b, /* INST_THUMB */ 0x8c, /* INST_A64 */ 0x8d, /* INST_BRANCH */ 0xbf, /* SYNC_DC_LOAD_MISS */ 0xc0, /* SYNC_DC_STORE_MISS */ 0xc1, /* SYNC_DTLB_MISS */ 0xc4, /* SYNC_ST_HIT_YNGR_LD */ 0xcb, /* SYNC_BR_ANY_MISP */ 0xd3, /* FED_IC_MISS_DEM */ 0xd4, /* FED_ITLB_MISS */ #elif defined(APPLETWISTER) || defined(APPLEHURRICANE) 0x02, /* CORE_CYCLE */ 0x1a, /* L2C_AGENT_LD */ 0x1b, /* L2C_AGENT_LD_MISS */ 0x1c, /* L2C_AGENT_ST */ 0x1d, /* L2C_AGENT_ST_MISS */ 0x8a, /* INST_A32 */ 0x8b, /* INST_THUMB */ 0x8c, /* INST_A64 */ 0x8d, /* INST_BRANCH */ 0xbf, /* SYNC_DC_LOAD_MISS */ 0xc0, /* SYNC_DC_STORE_MISS */ 0xc1, /* SYNC_DTLB_MISS */ 0xc4, /* SYNC_ST_HIT_YNGR_LD */ 0xcb, /* SYNC_BR_ANY_MISP */ 0xd3, /* FED_IC_MISS_DEM */ 0xd4, /* FED_ITLB_MISS */ #elif defined(APPLEMONSOON) 0x02, /* CORE_CYCLE */ 0x8a, /* INST_A32 */ 0x8b, /* INST_THUMB */ 0x8c, /* INST_A64 */ 0x8d, /* INST_BRANCH */ 0xbf, /* SYNC_DC_LOAD_MISS */ 0xc0, /* SYNC_DC_STORE_MISS */ 0xc1, /* SYNC_DTLB_MISS */ 0xc4, /* SYNC_ST_HIT_YNGR_LD */ 0xcb, /* SYNC_BR_ANY_MISP */ 0xd3, /* FED_IC_MISS_DEM */ 0xd4, /* FED_ITLB_MISS */ #else /* An unknown CPU gets a trivial { NO_EVENT } whitelist. */ #endif }; #define WHITELIST_COUNT (sizeof(whitelist)/sizeof(*whitelist)) static boolean_t config_in_whitelist(kpc_config_t cfg) { unsigned int i; for (i = 0; i < WHITELIST_COUNT; i++) { if (cfg == whitelist[i]) { return TRUE; } } return FALSE; } #ifdef KPC_DEBUG static void dump_regs(void) { uint64_t val; kprintf("PMCR0 = 0x%" PRIx64 "\n", SREG_READ(SREG_PMCR0)); kprintf("PMCR1 = 0x%" PRIx64 "\n", SREG_READ(SREG_PMCR1)); kprintf("PMCR2 = 0x%" PRIx64 "\n", SREG_READ(SREG_PMCR2)); kprintf("PMCR3 = 0x%" PRIx64 "\n", SREG_READ(SREG_PMCR3)); kprintf("PMCR4 = 0x%" PRIx64 "\n", SREG_READ(SREG_PMCR4)); kprintf("PMESR0 = 0x%" PRIx64 "\n", SREG_READ(SREG_PMESR0)); kprintf("PMESR1 = 0x%" PRIx64 "\n", SREG_READ(SREG_PMESR1)); kprintf("PMC0 = 0x%" PRIx64 "\n", SREG_READ(SREG_PMC0)); kprintf("PMC1 = 0x%" PRIx64 "\n", SREG_READ(SREG_PMC1)); kprintf("PMC2 = 0x%" PRIx64 "\n", SREG_READ(SREG_PMC2)); kprintf("PMC3 = 0x%" PRIx64 "\n", SREG_READ(SREG_PMC3)); kprintf("PMC4 = 0x%" PRIx64 "\n", SREG_READ(SREG_PMC4)); kprintf("PMC5 = 0x%" PRIx64 "\n", SREG_READ(SREG_PMC5)); kprintf("PMC6 = 0x%" PRIx64 "\n", SREG_READ(SREG_PMC6)); kprintf("PMC7 = 0x%" PRIx64 "\n", SREG_READ(SREG_PMC7)); #if (KPC_ARM64_CONFIGURABLE_COUNT > 6) kprintf("PMC8 = 0x%" PRIx64 "\n", SREG_READ(SREG_PMC8)); kprintf("PMC9 = 0x%" PRIx64 "\n", SREG_READ(SREG_PMC9)); #endif } #endif static boolean_t enable_counter(uint32_t counter) { int cpuid = cpu_number(); uint64_t pmcr0 = 0, intgen_type; boolean_t counter_running, pmi_enabled, intgen_correct, enabled; pmcr0 = SREG_READ(SREG_PMCR0) | 0x3 /* leave the fixed counters enabled for monotonic */; counter_running = (pmcr0 & PMCR0_PMC_ENABLE_MASK(counter)) != 0; pmi_enabled = (pmcr0 & PMCR0_PMI_ENABLE_MASK(counter)) != 0; /* TODO this should use the PMI path rather than AIC for the interrupt * as it is faster */ intgen_type = PMCR0_INTGEN_AIC; intgen_correct = (pmcr0 & PMCR0_INTGEN_MASK) == intgen_type; enabled = counter_running && pmi_enabled && intgen_correct; if (!enabled) { pmcr0 |= PMCR0_PMC_ENABLE_MASK(counter); pmcr0 |= PMCR0_PMI_ENABLE_MASK(counter); pmcr0 &= ~PMCR0_INTGEN_MASK; pmcr0 |= intgen_type; SREG_WRITE(SREG_PMCR0, pmcr0); } saved_PMCR[cpuid][0] = pmcr0; return enabled; } static boolean_t disable_counter(uint32_t counter) { uint64_t pmcr0; boolean_t enabled; int cpuid = cpu_number(); if (counter < 2) { return true; } pmcr0 = SREG_READ(SREG_PMCR0) | 0x3; enabled = (pmcr0 & PMCR0_PMC_ENABLE_MASK(counter)) != 0; if (enabled) { pmcr0 &= PMCR0_PMC_DISABLE_MASK(counter); SREG_WRITE(SREG_PMCR0, pmcr0); } saved_PMCR[cpuid][0] = pmcr0; return enabled; } /* * Enable counter in processor modes determined by configuration word. */ static void set_modes(uint32_t counter, kpc_config_t cfgword) { uint64_t bits = 0; int cpuid = cpu_number(); if (cfgword & CFGWORD_EL0A32EN_MASK) { bits |= PMCR1_EL0_A32_ENABLE_MASK(counter); } if (cfgword & CFGWORD_EL0A64EN_MASK) { bits |= PMCR1_EL0_A64_ENABLE_MASK(counter); } if (cfgword & CFGWORD_EL1EN_MASK) { bits |= PMCR1_EL1_A64_ENABLE_MASK(counter); } #if !NO_MONITOR if (cfgword & CFGWORD_EL3EN_MASK) { bits |= PMCR1_EL3_A64_ENABLE_MASK(counter); } #endif /* * Backwards compatibility: Writing a non-zero configuration word with * all zeros in bits 16-19 is interpreted as enabling in all modes. * This matches the behavior when the PMCR1 bits weren't exposed. */ if (bits == 0 && cfgword != 0) { bits = PMCR1_EL_ALL_ENABLE_MASK(counter); } uint64_t pmcr1 = SREG_READ(SREG_PMCR1); pmcr1 &= PMCR1_EL_ALL_DISABLE_MASK(counter); pmcr1 |= bits; pmcr1 |= 0x30303; /* monotonic compatibility */ SREG_WRITE(SREG_PMCR1, pmcr1); saved_PMCR[cpuid][1] = pmcr1; } static uint64_t read_counter(uint32_t counter) { switch (counter) { // case 0: return SREG_READ(SREG_PMC0); // case 1: return SREG_READ(SREG_PMC1); case 2: return SREG_READ(SREG_PMC2); case 3: return SREG_READ(SREG_PMC3); case 4: return SREG_READ(SREG_PMC4); case 5: return SREG_READ(SREG_PMC5); case 6: return SREG_READ(SREG_PMC6); case 7: return SREG_READ(SREG_PMC7); #if (KPC_ARM64_CONFIGURABLE_COUNT > 6) case 8: return SREG_READ(SREG_PMC8); case 9: return SREG_READ(SREG_PMC9); #endif default: return 0; } } static void write_counter(uint32_t counter, uint64_t value) { switch (counter) { // case 0: SREG_WRITE(SREG_PMC0, value); break; // case 1: SREG_WRITE(SREG_PMC1, value); break; case 2: SREG_WRITE(SREG_PMC2, value); break; case 3: SREG_WRITE(SREG_PMC3, value); break; case 4: SREG_WRITE(SREG_PMC4, value); break; case 5: SREG_WRITE(SREG_PMC5, value); break; case 6: SREG_WRITE(SREG_PMC6, value); break; case 7: SREG_WRITE(SREG_PMC7, value); break; #if (KPC_ARM64_CONFIGURABLE_COUNT > 6) case 8: SREG_WRITE(SREG_PMC8, value); break; case 9: SREG_WRITE(SREG_PMC9, value); break; #endif default: break; } } uint32_t kpc_rawpmu_config_count(void) { return RAWPMU_CONFIG_COUNT; } int kpc_get_rawpmu_config(kpc_config_t *configv) { configv[0] = SREG_READ(SREG_PMCR2); configv[1] = SREG_READ(SREG_PMCR3); configv[2] = SREG_READ(SREG_PMCR4); configv[3] = SREG_READ(SREG_OPMAT0); configv[4] = SREG_READ(SREG_OPMAT1); configv[5] = SREG_READ(SREG_OPMSK0); configv[6] = SREG_READ(SREG_OPMSK1); #if RAWPMU_CONFIG_COUNT > 7 configv[7] = SREG_READ(SREG_PMMMAP); configv[8] = SREG_READ(SREG_PMTRHLD2); configv[9] = SREG_READ(SREG_PMTRHLD4); configv[10] = SREG_READ(SREG_PMTRHLD6); #endif return 0; } static int kpc_set_rawpmu_config(kpc_config_t *configv) { SREG_WRITE(SREG_PMCR2, configv[0]); SREG_WRITE(SREG_PMCR3, configv[1]); SREG_WRITE(SREG_PMCR4, configv[2]); SREG_WRITE(SREG_OPMAT0, configv[3]); SREG_WRITE(SREG_OPMAT1, configv[4]); SREG_WRITE(SREG_OPMSK0, configv[5]); SREG_WRITE(SREG_OPMSK1, configv[6]); #if RAWPMU_CONFIG_COUNT > 7 SREG_WRITE(SREG_PMMMAP, configv[7]); SREG_WRITE(SREG_PMTRHLD2, configv[8]); SREG_WRITE(SREG_PMTRHLD4, configv[9]); SREG_WRITE(SREG_PMTRHLD6, configv[10]); #endif return 0; } static void save_regs(void) { int cpuid = cpu_number(); __asm__ volatile ("dmb ish"); assert(ml_get_interrupts_enabled() == FALSE); /* Save current PMCR0/1 values. PMCR2-4 are in the RAWPMU set. */ saved_PMCR[cpuid][0] = SREG_READ(SREG_PMCR0) | 0x3; /* Save event selections. */ saved_PMESR[cpuid][0] = SREG_READ(SREG_PMESR0); saved_PMESR[cpuid][1] = SREG_READ(SREG_PMESR1); kpc_get_rawpmu_config(saved_RAWPMU[cpuid]); /* Disable the counters. */ // SREG_WRITE(SREG_PMCR0, clear); /* Finally, save state for each counter*/ for (int i = 2; i < KPC_ARM64_PMC_COUNT; i++) { saved_counter[cpuid][i] = read_counter(i); } } static void restore_regs(void) { int cpuid = cpu_number(); /* Restore PMESR values. */ SREG_WRITE(SREG_PMESR0, saved_PMESR[cpuid][0]); SREG_WRITE(SREG_PMESR1, saved_PMESR[cpuid][1]); kpc_set_rawpmu_config(saved_RAWPMU[cpuid]); /* Restore counter values */ for (int i = 2; i < KPC_ARM64_PMC_COUNT; i++) { write_counter(i, saved_counter[cpuid][i]); } /* Restore PMCR0/1 values (with PMCR0 last to enable). */ SREG_WRITE(SREG_PMCR1, saved_PMCR[cpuid][1] | 0x30303); SREG_WRITE(SREG_PMCR0, saved_PMCR[cpuid][0] | 0x3); } static uint64_t get_counter_config(uint32_t counter) { uint64_t pmesr; switch (counter) { case 2: /* FALLTHROUGH */ case 3: /* FALLTHROUGH */ case 4: /* FALLTHROUGH */ case 5: pmesr = PMESR_EVT_DECODE(SREG_READ(SREG_PMESR0), counter, 2); break; case 6: /* FALLTHROUGH */ case 7: #if (KPC_ARM64_CONFIGURABLE_COUNT > 6) /* FALLTHROUGH */ case 8: /* FALLTHROUGH */ case 9: #endif pmesr = PMESR_EVT_DECODE(SREG_READ(SREG_PMESR1), counter, 6); break; default: pmesr = 0; break; } kpc_config_t config = pmesr; uint64_t pmcr1 = SREG_READ(SREG_PMCR1); if (pmcr1 & PMCR1_EL0_A32_ENABLE_MASK(counter)) { config |= CFGWORD_EL0A32EN_MASK; } if (pmcr1 & PMCR1_EL0_A64_ENABLE_MASK(counter)) { config |= CFGWORD_EL0A64EN_MASK; } if (pmcr1 & PMCR1_EL1_A64_ENABLE_MASK(counter)) { config |= CFGWORD_EL1EN_MASK; #if NO_MONITOR config |= CFGWORD_EL3EN_MASK; #endif } #if !NO_MONITOR if (pmcr1 & PMCR1_EL3_A64_ENABLE_MASK(counter)) { config |= CFGWORD_EL3EN_MASK; } #endif return config; } static void set_counter_config(uint32_t counter, uint64_t config) { int cpuid = cpu_number(); uint64_t pmesr = 0; switch (counter) { case 2: /* FALLTHROUGH */ case 3: /* FALLTHROUGH */ case 4: /* FALLTHROUGH */ case 5: pmesr = SREG_READ(SREG_PMESR0); pmesr &= PMESR_EVT_CLEAR(counter, 2); pmesr |= PMESR_EVT_ENCODE(config, counter, 2); SREG_WRITE(SREG_PMESR0, pmesr); saved_PMESR[cpuid][0] = pmesr; break; case 6: /* FALLTHROUGH */ case 7: #if KPC_ARM64_CONFIGURABLE_COUNT > 6 /* FALLTHROUGH */ case 8: /* FALLTHROUGH */ case 9: #endif pmesr = SREG_READ(SREG_PMESR1); pmesr &= PMESR_EVT_CLEAR(counter, 6); pmesr |= PMESR_EVT_ENCODE(config, counter, 6); SREG_WRITE(SREG_PMESR1, pmesr); saved_PMESR[cpuid][1] = pmesr; break; default: break; } set_modes(counter, config); } /* internal functions */ void kpc_arch_init(void) { } boolean_t kpc_is_running_fixed(void) { return (kpc_running_classes & KPC_CLASS_FIXED_MASK) == KPC_CLASS_FIXED_MASK; } boolean_t kpc_is_running_configurable(uint64_t pmc_mask) { assert(kpc_popcount(pmc_mask) <= kpc_configurable_count()); return ((kpc_running_classes & KPC_CLASS_CONFIGURABLE_MASK) == KPC_CLASS_CONFIGURABLE_MASK) && ((kpc_running_cfg_pmc_mask & pmc_mask) == pmc_mask); } uint32_t kpc_fixed_count(void) { return KPC_ARM64_FIXED_COUNT; } uint32_t kpc_configurable_count(void) { return KPC_ARM64_CONFIGURABLE_COUNT; } uint32_t kpc_fixed_config_count(void) { return 0; } uint32_t kpc_configurable_config_count(uint64_t pmc_mask) { assert(kpc_popcount(pmc_mask) <= kpc_configurable_count()); return kpc_popcount(pmc_mask); } int kpc_get_fixed_config(kpc_config_t *configv __unused) { return 0; } uint64_t kpc_fixed_max(void) { return (1ULL << KPC_ARM64_COUNTER_WIDTH) - 1; } uint64_t kpc_configurable_max(void) { return (1ULL << KPC_ARM64_COUNTER_WIDTH) - 1; } static void set_running_configurable(uint64_t target_mask, uint64_t state_mask) { uint32_t cfg_count = kpc_configurable_count(), offset = kpc_fixed_count(); boolean_t enabled; enabled = ml_set_interrupts_enabled(FALSE); for (uint32_t i = 0; i < cfg_count; ++i) { if (((1ULL << i) & target_mask) == 0) { continue; } assert(kpc_controls_counter(offset + i)); if ((1ULL << i) & state_mask) { enable_counter(offset + i); } else { disable_counter(offset + i); } } ml_set_interrupts_enabled(enabled); } static uint32_t kpc_xcall_sync; static void kpc_set_running_xcall( void *vstate ) { struct kpc_running_remote *mp_config = (struct kpc_running_remote*) vstate; assert(mp_config); set_running_configurable(mp_config->cfg_target_mask, mp_config->cfg_state_mask); if (hw_atomic_sub(&kpc_xcall_sync, 1) == 0) { thread_wakeup((event_t) &kpc_xcall_sync); } } static uint32_t kpc_xread_sync; static void kpc_get_curcpu_counters_xcall(void *args) { struct kpc_get_counters_remote *handler = args; assert(handler != NULL); assert(handler->buf != NULL); int offset = cpu_number() * handler->buf_stride; int r = kpc_get_curcpu_counters(handler->classes, NULL, &handler->buf[offset]); /* number of counters added by this CPU, needs to be atomic */ hw_atomic_add(&(handler->nb_counters), r); if (hw_atomic_sub(&kpc_xread_sync, 1) == 0) { thread_wakeup((event_t) &kpc_xread_sync); } } int kpc_get_all_cpus_counters(uint32_t classes, int *curcpu, uint64_t *buf) { assert(buf != NULL); int enabled = ml_set_interrupts_enabled(FALSE); /* grab counters and CPU number as close as possible */ if (curcpu) { *curcpu = current_processor()->cpu_id; } struct kpc_get_counters_remote hdl = { .classes = classes, .nb_counters = 0, .buf = buf, .buf_stride = kpc_get_counter_count(classes) }; cpu_broadcast_xcall(&kpc_xread_sync, TRUE, kpc_get_curcpu_counters_xcall, &hdl); int offset = hdl.nb_counters; (void)ml_set_interrupts_enabled(enabled); return offset; } int kpc_get_fixed_counters(uint64_t *counterv) { #if MONOTONIC mt_fixed_counts(counterv); return 0; #else /* MONOTONIC */ #pragma unused(counterv) return ENOTSUP; #endif /* !MONOTONIC */ } int kpc_get_configurable_counters(uint64_t *counterv, uint64_t pmc_mask) { uint32_t cfg_count = kpc_configurable_count(), offset = kpc_fixed_count(); uint64_t ctr = 0ULL; assert(counterv); for (uint32_t i = 0; i < cfg_count; ++i) { if (((1ULL << i) & pmc_mask) == 0) { continue; } ctr = read_counter(i + offset); if (ctr & KPC_ARM64_COUNTER_OVF_MASK) { ctr = CONFIGURABLE_SHADOW(i) + (kpc_configurable_max() - CONFIGURABLE_RELOAD(i) + 1 /* Wrap */) + (ctr & KPC_ARM64_COUNTER_MASK); } else { ctr = CONFIGURABLE_SHADOW(i) + (ctr - CONFIGURABLE_RELOAD(i)); } *counterv++ = ctr; } return 0; } int kpc_get_configurable_config(kpc_config_t *configv, uint64_t pmc_mask) { uint32_t cfg_count = kpc_configurable_count(), offset = kpc_fixed_count(); assert(configv); for (uint32_t i = 0; i < cfg_count; ++i) { if ((1ULL << i) & pmc_mask) { *configv++ = get_counter_config(i + offset); } } return 0; } static int kpc_set_configurable_config(kpc_config_t *configv, uint64_t pmc_mask) { uint32_t cfg_count = kpc_configurable_count(), offset = kpc_fixed_count(); boolean_t enabled; assert(configv); enabled = ml_set_interrupts_enabled(FALSE); for (uint32_t i = 0; i < cfg_count; ++i) { if (((1ULL << i) & pmc_mask) == 0) { continue; } assert(kpc_controls_counter(i + offset)); set_counter_config(i + offset, *configv++); } ml_set_interrupts_enabled(enabled); return 0; } static uint32_t kpc_config_sync; static void kpc_set_config_xcall(void *vmp_config) { struct kpc_config_remote *mp_config = vmp_config; kpc_config_t *new_config = NULL; uint32_t classes = 0ULL; assert(mp_config); assert(mp_config->configv); classes = mp_config->classes; new_config = mp_config->configv; if (classes & KPC_CLASS_CONFIGURABLE_MASK) { kpc_set_configurable_config(new_config, mp_config->pmc_mask); new_config += kpc_popcount(mp_config->pmc_mask); } if (classes & KPC_CLASS_RAWPMU_MASK) { kpc_set_rawpmu_config(new_config); new_config += RAWPMU_CONFIG_COUNT; } if (hw_atomic_sub(&kpc_config_sync, 1) == 0) { thread_wakeup((event_t) &kpc_config_sync); } } static uint64_t kpc_reload_counter(uint32_t ctr) { assert(ctr < (kpc_configurable_count() + kpc_fixed_count())); uint64_t old = read_counter(ctr); if (kpc_controls_counter(ctr)) { write_counter(ctr, FIXED_RELOAD(ctr)); return old & KPC_ARM64_COUNTER_MASK; } else { /* * Unset the overflow bit to clear the condition that drives * PMIs. The power manager is not interested in handling PMIs. */ write_counter(ctr, old & KPC_ARM64_COUNTER_MASK); return 0; } } static uint32_t kpc_reload_sync; static void kpc_set_reload_xcall(void *vmp_config) { struct kpc_config_remote *mp_config = vmp_config; uint32_t classes = 0, count = 0, offset = kpc_fixed_count(); uint64_t *new_period = NULL, max = kpc_configurable_max(); boolean_t enabled; assert(mp_config); assert(mp_config->configv); classes = mp_config->classes; new_period = mp_config->configv; enabled = ml_set_interrupts_enabled(FALSE); if (classes & KPC_CLASS_CONFIGURABLE_MASK) { /* * Update _all_ shadow counters, this cannot be done for only * selected PMCs. Otherwise, we would corrupt the configurable * shadow buffer since the PMCs are muxed according to the pmc * mask. */ uint64_t all_cfg_mask = (1ULL << kpc_configurable_count()) - 1; kpc_get_configurable_counters(&CONFIGURABLE_SHADOW(0), all_cfg_mask); /* set the new period */ count = kpc_configurable_count(); for (uint32_t i = 0; i < count; ++i) { /* ignore the counter */ if (((1ULL << i) & mp_config->pmc_mask) == 0) { continue; } if (*new_period == 0) { *new_period = kpc_configurable_max(); } CONFIGURABLE_RELOAD(i) = max - *new_period; /* reload the counter */ kpc_reload_counter(offset + i); /* next period value */ new_period++; } } ml_set_interrupts_enabled(enabled); if (hw_atomic_sub(&kpc_reload_sync, 1) == 0) { thread_wakeup((event_t) &kpc_reload_sync); } } void kpc_pmi_handler(unsigned int ctr) { uint64_t extra = kpc_reload_counter(ctr); FIXED_SHADOW(ctr) += (kpc_fixed_max() - FIXED_RELOAD(ctr) + 1 /* Wrap */) + extra; if (FIXED_ACTIONID(ctr)) { kpc_sample_kperf(FIXED_ACTIONID(ctr)); } } uint32_t kpc_get_classes(void) { return KPC_CLASS_FIXED_MASK | KPC_CLASS_CONFIGURABLE_MASK | KPC_CLASS_RAWPMU_MASK; } int kpc_set_running_arch(struct kpc_running_remote *mp_config) { assert(mp_config != NULL); /* dispatch to all CPUs */ cpu_broadcast_xcall(&kpc_xcall_sync, TRUE, kpc_set_running_xcall, mp_config); kpc_running_cfg_pmc_mask = mp_config->cfg_state_mask; kpc_running_classes = mp_config->classes; kpc_configured = 1; return 0; } int kpc_set_period_arch(struct kpc_config_remote *mp_config) { assert(mp_config); /* dispatch to all CPUs */ cpu_broadcast_xcall(&kpc_reload_sync, TRUE, kpc_set_reload_xcall, mp_config); kpc_configured = 1; return 0; } int kpc_set_config_arch(struct kpc_config_remote *mp_config) { uint32_t count = kpc_popcount(mp_config->pmc_mask); assert(mp_config); assert(mp_config->configv); /* check config against whitelist for external devs */ for (uint32_t i = 0; i < count; ++i) { if (!whitelist_disabled && !config_in_whitelist(mp_config->configv[i])) { return EPERM; } } /* dispatch to all CPUs */ cpu_broadcast_xcall(&kpc_config_sync, TRUE, kpc_set_config_xcall, mp_config); kpc_configured = 1; return 0; } void kpc_idle(void) { if (kpc_configured) { save_regs(); } } void kpc_idle_exit(void) { if (kpc_configured) { restore_regs(); } } int kpc_set_sw_inc( uint32_t mask __unused ) { return ENOTSUP; } int kpc_disable_whitelist( int val ) { whitelist_disabled = val; return 0; } int kpc_get_whitelist_disabled( void ) { return whitelist_disabled; } int kpc_get_pmu_version(void) { return KPC_PMU_ARM_APPLE; } |