1 /*- 2 * Copyright (c) 2010 Fabien Thomas 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * Intel Uncore PMCs. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/bus.h> 36 #include <sys/pmc.h> 37 #include <sys/pmckern.h> 38 #include <sys/systm.h> 39 40 #include <machine/intr_machdep.h> 41 #include <machine/apicvar.h> 42 #include <machine/cpu.h> 43 #include <machine/cpufunc.h> 44 #include <machine/specialreg.h> 45 46 #define UCF_PMC_CAPS \ 47 (PMC_CAP_READ | PMC_CAP_WRITE) 48 49 #define UCP_PMC_CAPS \ 50 (PMC_CAP_EDGE | PMC_CAP_THRESHOLD | PMC_CAP_READ | PMC_CAP_WRITE | \ 51 PMC_CAP_INVERT | PMC_CAP_QUALIFIER | PMC_CAP_PRECISE) 52 53 static enum pmc_cputype uncore_cputype; 54 55 struct uncore_cpu { 56 volatile uint32_t pc_resync; 57 volatile uint32_t pc_ucfctrl; /* Fixed function control. */ 58 volatile uint64_t pc_globalctrl; /* Global control register. */ 59 struct pmc_hw pc_uncorepmcs[]; 60 }; 61 62 static struct uncore_cpu **uncore_pcpu; 63 64 static uint64_t uncore_pmcmask; 65 66 static int uncore_ucf_ri; /* relative index of fixed counters */ 67 static int uncore_ucf_width; 68 static int uncore_ucf_npmc; 69 70 static int uncore_ucp_width; 71 static int uncore_ucp_npmc; 72 73 static int 74 uncore_pcpu_noop(struct pmc_mdep *md, int cpu) 75 { 76 (void) md; 77 (void) cpu; 78 return (0); 79 } 80 81 static int 82 uncore_pcpu_init(struct pmc_mdep *md, int cpu) 83 { 84 struct pmc_cpu *pc; 85 struct uncore_cpu *cc; 86 struct pmc_hw *phw; 87 int uncore_ri, n, npmc; 88 89 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 90 ("[ucf,%d] insane cpu number %d", __LINE__, cpu)); 91 92 PMCDBG(MDP,INI,1,"uncore-init cpu=%d", cpu); 93 94 uncore_ri = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_UCP].pcd_ri; 95 npmc = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_UCP].pcd_num; 96 npmc += md->pmd_classdep[PMC_MDEP_CLASS_INDEX_UCF].pcd_num; 97 98 cc = malloc(sizeof(struct uncore_cpu) + npmc * sizeof(struct pmc_hw), 99 M_PMC, M_WAITOK | M_ZERO); 100 101 uncore_pcpu[cpu] = cc; 102 pc = pmc_pcpu[cpu]; 103 104 KASSERT(pc != NULL && cc != NULL, 105 ("[uncore,%d] NULL per-cpu structures cpu=%d", __LINE__, cpu)); 106 107 for (n = 0, phw = cc->pc_uncorepmcs; n < npmc; n++, phw++) { 108 phw->phw_state = PMC_PHW_FLAG_IS_ENABLED | 109 PMC_PHW_CPU_TO_STATE(cpu) | 110 PMC_PHW_INDEX_TO_STATE(n + uncore_ri); 111 phw->phw_pmc = NULL; 112 pc->pc_hwpmcs[n + uncore_ri] = phw; 113 } 114 115 return (0); 116 } 117 118 static int 119 uncore_pcpu_fini(struct pmc_mdep *md, int cpu) 120 { 121 int uncore_ri, n, npmc; 122 struct pmc_cpu *pc; 123 struct uncore_cpu *cc; 124 125 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 126 ("[uncore,%d] insane cpu number (%d)", __LINE__, cpu)); 127 128 PMCDBG(MDP,INI,1,"uncore-pcpu-fini cpu=%d", cpu); 129 130 if ((cc = uncore_pcpu[cpu]) == NULL) 131 return (0); 132 133 uncore_pcpu[cpu] = NULL; 134 135 pc = pmc_pcpu[cpu]; 136 137 KASSERT(pc != NULL, ("[uncore,%d] NULL per-cpu %d state", __LINE__, 138 cpu)); 139 140 npmc = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_UCP].pcd_num; 141 uncore_ri = md->pmd_classdep[PMC_MDEP_CLASS_INDEX_UCP].pcd_ri; 142 143 for (n = 0; n < npmc; n++) 144 wrmsr(UCP_EVSEL0 + n, 0); 145 146 wrmsr(UCF_CTRL, 0); 147 npmc += md->pmd_classdep[PMC_MDEP_CLASS_INDEX_UCF].pcd_num; 148 149 for (n = 0; n < npmc; n++) 150 pc->pc_hwpmcs[n + uncore_ri] = NULL; 151 152 free(cc, M_PMC); 153 154 return (0); 155 } 156 157 /* 158 * Fixed function counters. 159 */ 160 161 static pmc_value_t 162 ucf_perfctr_value_to_reload_count(pmc_value_t v) 163 { 164 v &= (1ULL << uncore_ucf_width) - 1; 165 return (1ULL << uncore_ucf_width) - v; 166 } 167 168 static pmc_value_t 169 ucf_reload_count_to_perfctr_value(pmc_value_t rlc) 170 { 171 return (1ULL << uncore_ucf_width) - rlc; 172 } 173 174 static int 175 ucf_allocate_pmc(int cpu, int ri, struct pmc *pm, 176 const struct pmc_op_pmcallocate *a) 177 { 178 enum pmc_event ev; 179 uint32_t caps, flags; 180 181 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 182 ("[uncore,%d] illegal CPU %d", __LINE__, cpu)); 183 184 PMCDBG(MDP,ALL,1, "ucf-allocate ri=%d reqcaps=0x%x", ri, pm->pm_caps); 185 186 if (ri < 0 || ri > uncore_ucf_npmc) 187 return (EINVAL); 188 189 caps = a->pm_caps; 190 191 if (a->pm_class != PMC_CLASS_UCF || 192 (caps & UCF_PMC_CAPS) != caps) 193 return (EINVAL); 194 195 ev = pm->pm_event; 196 if (ev < PMC_EV_UCF_FIRST || ev > PMC_EV_UCF_LAST) 197 return (EINVAL); 198 199 flags = UCF_EN; 200 201 pm->pm_md.pm_ucf.pm_ucf_ctrl = (flags << (ri * 4)); 202 203 PMCDBG(MDP,ALL,2, "ucf-allocate config=0x%jx", 204 (uintmax_t) pm->pm_md.pm_ucf.pm_ucf_ctrl); 205 206 return (0); 207 } 208 209 static int 210 ucf_config_pmc(int cpu, int ri, struct pmc *pm) 211 { 212 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 213 ("[uncore,%d] illegal CPU %d", __LINE__, cpu)); 214 215 KASSERT(ri >= 0 && ri < uncore_ucf_npmc, 216 ("[uncore,%d] illegal row-index %d", __LINE__, ri)); 217 218 PMCDBG(MDP,CFG,1, "ucf-config cpu=%d ri=%d pm=%p", cpu, ri, pm); 219 220 KASSERT(uncore_pcpu[cpu] != NULL, ("[uncore,%d] null per-cpu %d", __LINE__, 221 cpu)); 222 223 uncore_pcpu[cpu]->pc_uncorepmcs[ri + uncore_ucf_ri].phw_pmc = pm; 224 225 return (0); 226 } 227 228 static int 229 ucf_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc) 230 { 231 int error; 232 struct pmc_hw *phw; 233 char ucf_name[PMC_NAME_MAX]; 234 235 phw = &uncore_pcpu[cpu]->pc_uncorepmcs[ri + uncore_ucf_ri]; 236 237 (void) snprintf(ucf_name, sizeof(ucf_name), "UCF-%d", ri); 238 if ((error = copystr(ucf_name, pi->pm_name, PMC_NAME_MAX, 239 NULL)) != 0) 240 return (error); 241 242 pi->pm_class = PMC_CLASS_UCF; 243 244 if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) { 245 pi->pm_enabled = TRUE; 246 *ppmc = phw->phw_pmc; 247 } else { 248 pi->pm_enabled = FALSE; 249 *ppmc = NULL; 250 } 251 252 return (0); 253 } 254 255 static int 256 ucf_get_config(int cpu, int ri, struct pmc **ppm) 257 { 258 *ppm = uncore_pcpu[cpu]->pc_uncorepmcs[ri + uncore_ucf_ri].phw_pmc; 259 260 return (0); 261 } 262 263 static int 264 ucf_read_pmc(int cpu, int ri, pmc_value_t *v) 265 { 266 struct pmc *pm; 267 pmc_value_t tmp; 268 269 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 270 ("[uncore,%d] illegal cpu value %d", __LINE__, cpu)); 271 KASSERT(ri >= 0 && ri < uncore_ucf_npmc, 272 ("[uncore,%d] illegal row-index %d", __LINE__, ri)); 273 274 pm = uncore_pcpu[cpu]->pc_uncorepmcs[ri + uncore_ucf_ri].phw_pmc; 275 276 KASSERT(pm, 277 ("[uncore,%d] cpu %d ri %d(%d) pmc not configured", __LINE__, cpu, 278 ri, ri + uncore_ucf_ri)); 279 280 tmp = rdmsr(UCF_CTR0 + ri); 281 282 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) 283 *v = ucf_perfctr_value_to_reload_count(tmp); 284 else 285 *v = tmp; 286 287 PMCDBG(MDP,REA,1, "ucf-read cpu=%d ri=%d -> v=%jx", cpu, ri, *v); 288 289 return (0); 290 } 291 292 static int 293 ucf_release_pmc(int cpu, int ri, struct pmc *pmc) 294 { 295 PMCDBG(MDP,REL,1, "ucf-release cpu=%d ri=%d pm=%p", cpu, ri, pmc); 296 297 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 298 ("[uncore,%d] illegal CPU value %d", __LINE__, cpu)); 299 KASSERT(ri >= 0 && ri < uncore_ucf_npmc, 300 ("[uncore,%d] illegal row-index %d", __LINE__, ri)); 301 302 KASSERT(uncore_pcpu[cpu]->pc_uncorepmcs[ri + uncore_ucf_ri].phw_pmc == NULL, 303 ("[uncore,%d] PHW pmc non-NULL", __LINE__)); 304 305 return (0); 306 } 307 308 static int 309 ucf_start_pmc(int cpu, int ri) 310 { 311 struct pmc *pm; 312 struct uncore_cpu *ucfc; 313 314 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 315 ("[uncore,%d] illegal CPU value %d", __LINE__, cpu)); 316 KASSERT(ri >= 0 && ri < uncore_ucf_npmc, 317 ("[uncore,%d] illegal row-index %d", __LINE__, ri)); 318 319 PMCDBG(MDP,STA,1,"ucf-start cpu=%d ri=%d", cpu, ri); 320 321 ucfc = uncore_pcpu[cpu]; 322 pm = ucfc->pc_uncorepmcs[ri + uncore_ucf_ri].phw_pmc; 323 324 ucfc->pc_ucfctrl |= pm->pm_md.pm_ucf.pm_ucf_ctrl; 325 326 wrmsr(UCF_CTRL, ucfc->pc_ucfctrl); 327 328 do { 329 ucfc->pc_resync = 0; 330 ucfc->pc_globalctrl |= (1ULL << (ri + UCF_OFFSET)); 331 wrmsr(UC_GLOBAL_CTRL, ucfc->pc_globalctrl); 332 } while (ucfc->pc_resync != 0); 333 334 PMCDBG(MDP,STA,1,"ucfctrl=%x(%x) globalctrl=%jx(%jx)", 335 ucfc->pc_ucfctrl, (uint32_t) rdmsr(UCF_CTRL), 336 ucfc->pc_globalctrl, rdmsr(UC_GLOBAL_CTRL)); 337 338 return (0); 339 } 340 341 static int 342 ucf_stop_pmc(int cpu, int ri) 343 { 344 uint32_t fc; 345 struct uncore_cpu *ucfc; 346 347 PMCDBG(MDP,STO,1,"ucf-stop cpu=%d ri=%d", cpu, ri); 348 349 ucfc = uncore_pcpu[cpu]; 350 351 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 352 ("[uncore,%d] illegal CPU value %d", __LINE__, cpu)); 353 KASSERT(ri >= 0 && ri < uncore_ucf_npmc, 354 ("[uncore,%d] illegal row-index %d", __LINE__, ri)); 355 356 fc = (UCF_MASK << (ri * 4)); 357 358 ucfc->pc_ucfctrl &= ~fc; 359 360 PMCDBG(MDP,STO,1,"ucf-stop ucfctrl=%x", ucfc->pc_ucfctrl); 361 wrmsr(UCF_CTRL, ucfc->pc_ucfctrl); 362 363 do { 364 ucfc->pc_resync = 0; 365 ucfc->pc_globalctrl &= ~(1ULL << (ri + UCF_OFFSET)); 366 wrmsr(UC_GLOBAL_CTRL, ucfc->pc_globalctrl); 367 } while (ucfc->pc_resync != 0); 368 369 PMCDBG(MDP,STO,1,"ucfctrl=%x(%x) globalctrl=%jx(%jx)", 370 ucfc->pc_ucfctrl, (uint32_t) rdmsr(UCF_CTRL), 371 ucfc->pc_globalctrl, rdmsr(UC_GLOBAL_CTRL)); 372 373 return (0); 374 } 375 376 static int 377 ucf_write_pmc(int cpu, int ri, pmc_value_t v) 378 { 379 struct uncore_cpu *cc; 380 struct pmc *pm; 381 382 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 383 ("[uncore,%d] illegal cpu value %d", __LINE__, cpu)); 384 KASSERT(ri >= 0 && ri < uncore_ucf_npmc, 385 ("[uncore,%d] illegal row-index %d", __LINE__, ri)); 386 387 cc = uncore_pcpu[cpu]; 388 pm = cc->pc_uncorepmcs[ri + uncore_ucf_ri].phw_pmc; 389 390 KASSERT(pm, 391 ("[uncore,%d] cpu %d ri %d pmc not configured", __LINE__, cpu, ri)); 392 393 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) 394 v = ucf_reload_count_to_perfctr_value(v); 395 396 wrmsr(UCF_CTRL, 0); /* Turn off fixed counters */ 397 wrmsr(UCF_CTR0 + ri, v); 398 wrmsr(UCF_CTRL, cc->pc_ucfctrl); 399 400 PMCDBG(MDP,WRI,1, "ucf-write cpu=%d ri=%d v=%jx ucfctrl=%jx ", 401 cpu, ri, v, (uintmax_t) rdmsr(UCF_CTRL)); 402 403 return (0); 404 } 405 406 407 static void 408 ucf_initialize(struct pmc_mdep *md, int maxcpu, int npmc, int pmcwidth) 409 { 410 struct pmc_classdep *pcd; 411 412 KASSERT(md != NULL, ("[ucf,%d] md is NULL", __LINE__)); 413 414 PMCDBG(MDP,INI,1, "%s", "ucf-initialize"); 415 416 pcd = &md->pmd_classdep[PMC_MDEP_CLASS_INDEX_UCF]; 417 418 pcd->pcd_caps = UCF_PMC_CAPS; 419 pcd->pcd_class = PMC_CLASS_UCF; 420 pcd->pcd_num = npmc; 421 pcd->pcd_ri = md->pmd_npmc; 422 pcd->pcd_width = pmcwidth; 423 424 pcd->pcd_allocate_pmc = ucf_allocate_pmc; 425 pcd->pcd_config_pmc = ucf_config_pmc; 426 pcd->pcd_describe = ucf_describe; 427 pcd->pcd_get_config = ucf_get_config; 428 pcd->pcd_get_msr = NULL; 429 pcd->pcd_pcpu_fini = uncore_pcpu_noop; 430 pcd->pcd_pcpu_init = uncore_pcpu_noop; 431 pcd->pcd_read_pmc = ucf_read_pmc; 432 pcd->pcd_release_pmc = ucf_release_pmc; 433 pcd->pcd_start_pmc = ucf_start_pmc; 434 pcd->pcd_stop_pmc = ucf_stop_pmc; 435 pcd->pcd_write_pmc = ucf_write_pmc; 436 437 md->pmd_npmc += npmc; 438 } 439 440 /* 441 * Intel programmable PMCs. 442 */ 443 444 /* 445 * Event descriptor tables. 446 * 447 * For each event id, we track: 448 * 449 * 1. The CPUs that the event is valid for. 450 * 451 * 2. If the event uses a fixed UMASK, the value of the umask field. 452 * If the event doesn't use a fixed UMASK, a mask of legal bits 453 * to check against. 454 */ 455 456 struct ucp_event_descr { 457 enum pmc_event ucp_ev; 458 unsigned char ucp_evcode; 459 unsigned char ucp_umask; 460 unsigned char ucp_flags; 461 }; 462 463 #define UCP_F_I7 (1 << 0) /* CPU: Core i7 */ 464 #define UCP_F_WM (1 << 1) /* CPU: Westmere */ 465 #define UCP_F_FM (1 << 2) /* Fixed mask */ 466 467 #define UCP_F_ALLCPUS \ 468 (UCP_F_I7 | UCP_F_WM) 469 470 #define UCP_F_CMASK 0xFF000000 471 472 static struct ucp_event_descr ucp_events[] = { 473 #undef UCPDESCR 474 #define UCPDESCR(N,EV,UM,FLAGS) { \ 475 .ucp_ev = PMC_EV_UCP_EVENT_##N, \ 476 .ucp_evcode = (EV), \ 477 .ucp_umask = (UM), \ 478 .ucp_flags = (FLAGS) \ 479 } 480 481 UCPDESCR(00H_01H, 0x00, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 482 UCPDESCR(00H_02H, 0x00, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 483 UCPDESCR(00H_04H, 0x00, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 484 485 UCPDESCR(01H_01H, 0x01, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 486 UCPDESCR(01H_02H, 0x01, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 487 UCPDESCR(01H_04H, 0x01, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 488 489 UCPDESCR(02H_01H, 0x02, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 490 UCPDESCR(03H_01H, 0x03, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 491 UCPDESCR(03H_02H, 0x03, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 492 UCPDESCR(03H_04H, 0x03, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 493 UCPDESCR(03H_08H, 0x03, 0x08, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 494 UCPDESCR(03H_10H, 0x03, 0x10, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 495 UCPDESCR(03H_20H, 0x03, 0x20, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 496 UCPDESCR(03H_40H, 0x03, 0x40, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 497 498 UCPDESCR(04H_01H, 0x04, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 499 UCPDESCR(04H_02H, 0x04, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 500 UCPDESCR(04H_04H, 0x04, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 501 UCPDESCR(04H_08H, 0x04, 0x08, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 502 UCPDESCR(04H_10H, 0x04, 0x10, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 503 504 UCPDESCR(05H_01H, 0x05, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 505 UCPDESCR(05H_02H, 0x05, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 506 UCPDESCR(05H_04H, 0x05, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 507 508 UCPDESCR(06H_01H, 0x06, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 509 UCPDESCR(06H_02H, 0x06, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 510 UCPDESCR(06H_04H, 0x06, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 511 UCPDESCR(06H_08H, 0x06, 0x08, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 512 UCPDESCR(06H_10H, 0x06, 0x10, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 513 UCPDESCR(06H_20H, 0x06, 0x20, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 514 515 UCPDESCR(07H_01H, 0x07, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 516 UCPDESCR(07H_02H, 0x07, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 517 UCPDESCR(07H_04H, 0x07, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 518 UCPDESCR(07H_08H, 0x07, 0x08, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 519 UCPDESCR(07H_10H, 0x07, 0x10, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 520 UCPDESCR(07H_20H, 0x07, 0x20, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 521 UCPDESCR(07H_24H, 0x07, 0x24, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 522 523 UCPDESCR(08H_01H, 0x08, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 524 UCPDESCR(08H_02H, 0x08, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 525 UCPDESCR(08H_04H, 0x08, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 526 UCPDESCR(08H_03H, 0x08, 0x03, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 527 528 UCPDESCR(09H_01H, 0x09, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 529 UCPDESCR(09H_02H, 0x09, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 530 UCPDESCR(09H_04H, 0x09, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 531 UCPDESCR(09H_03H, 0x09, 0x03, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 532 533 UCPDESCR(0AH_01H, 0x0A, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 534 UCPDESCR(0AH_02H, 0x0A, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 535 UCPDESCR(0AH_04H, 0x0A, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 536 UCPDESCR(0AH_08H, 0x0A, 0x08, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 537 UCPDESCR(0AH_0FH, 0x0A, 0x0F, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 538 539 UCPDESCR(0BH_01H, 0x0B, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 540 UCPDESCR(0BH_02H, 0x0B, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 541 UCPDESCR(0BH_04H, 0x0B, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 542 UCPDESCR(0BH_08H, 0x0B, 0x08, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 543 UCPDESCR(0BH_10H, 0x0B, 0x10, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 544 UCPDESCR(0BH_1FH, 0x0B, 0x1F, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 545 546 UCPDESCR(0CH_01H, 0x0C, 0x01, UCP_F_FM | UCP_F_WM), 547 UCPDESCR(0CH_02H, 0x0C, 0x02, UCP_F_FM | UCP_F_WM), 548 UCPDESCR(0CH_04H, 0x0C, 0x04, UCP_F_FM | UCP_F_WM), 549 UCPDESCR(0CH_08H, 0x0C, 0x08, UCP_F_FM | UCP_F_WM), 550 551 UCPDESCR(20H_01H, 0x20, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 552 UCPDESCR(20H_02H, 0x20, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 553 UCPDESCR(20H_04H, 0x20, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 554 UCPDESCR(20H_08H, 0x20, 0x08, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 555 UCPDESCR(20H_10H, 0x20, 0x10, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 556 UCPDESCR(20H_20H, 0x20, 0x20, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 557 558 UCPDESCR(21H_01H, 0x21, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 559 UCPDESCR(21H_02H, 0x21, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 560 UCPDESCR(21H_04H, 0x21, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 561 562 UCPDESCR(22H_01H, 0x22, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 563 UCPDESCR(22H_02H, 0x22, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 564 UCPDESCR(22H_04H, 0x22, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 565 566 UCPDESCR(23H_01H, 0x23, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 567 UCPDESCR(23H_02H, 0x23, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 568 UCPDESCR(23H_04H, 0x23, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 569 570 UCPDESCR(24H_02H, 0x24, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 571 UCPDESCR(24H_04H, 0x24, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 572 573 UCPDESCR(25H_01H, 0x25, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 574 UCPDESCR(25H_02H, 0x25, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 575 UCPDESCR(25H_04H, 0x25, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 576 577 UCPDESCR(26H_01H, 0x26, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 578 579 UCPDESCR(27H_01H, 0x27, 0x01, UCP_F_FM | UCP_F_I7), 580 UCPDESCR(27H_02H, 0x27, 0x02, UCP_F_FM | UCP_F_I7), 581 UCPDESCR(27H_04H, 0x27, 0x04, UCP_F_FM | UCP_F_I7), 582 UCPDESCR(27H_08H, 0x27, 0x08, UCP_F_FM | UCP_F_I7), 583 UCPDESCR(27H_10H, 0x27, 0x10, UCP_F_FM | UCP_F_I7), 584 UCPDESCR(27H_20H, 0x27, 0x20, UCP_F_FM | UCP_F_I7), 585 586 UCPDESCR(28H_01H, 0x28, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 587 UCPDESCR(28H_02H, 0x28, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 588 UCPDESCR(28H_04H, 0x28, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 589 UCPDESCR(28H_08H, 0x28, 0x08, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 590 UCPDESCR(28H_10H, 0x28, 0x10, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 591 UCPDESCR(28H_20H, 0x28, 0x20, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 592 593 UCPDESCR(29H_01H, 0x29, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 594 UCPDESCR(29H_02H, 0x29, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 595 UCPDESCR(29H_04H, 0x29, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 596 UCPDESCR(29H_08H, 0x29, 0x08, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 597 UCPDESCR(29H_10H, 0x29, 0x10, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 598 UCPDESCR(29H_20H, 0x29, 0x20, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 599 600 UCPDESCR(2AH_01H, 0x2A, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 601 UCPDESCR(2AH_02H, 0x2A, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 602 UCPDESCR(2AH_04H, 0x2A, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 603 UCPDESCR(2AH_07H, 0x2A, 0x07, UCP_F_FM | UCP_F_WM), 604 605 UCPDESCR(2BH_01H, 0x2B, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 606 UCPDESCR(2BH_02H, 0x2B, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 607 UCPDESCR(2BH_04H, 0x2B, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 608 UCPDESCR(2BH_07H, 0x2B, 0x07, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 609 610 UCPDESCR(2CH_01H, 0x2C, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 611 UCPDESCR(2CH_02H, 0x2C, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 612 UCPDESCR(2CH_04H, 0x2C, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 613 UCPDESCR(2CH_07H, 0x2C, 0x07, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 614 615 UCPDESCR(2DH_01H, 0x2D, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 616 UCPDESCR(2DH_02H, 0x2D, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 617 UCPDESCR(2DH_04H, 0x2D, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 618 UCPDESCR(2DH_07H, 0x2D, 0x07, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 619 620 UCPDESCR(2EH_01H, 0x2E, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 621 UCPDESCR(2EH_02H, 0x2E, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 622 UCPDESCR(2EH_04H, 0x2E, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 623 UCPDESCR(2EH_07H, 0x2E, 0x07, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 624 625 UCPDESCR(2FH_01H, 0x2F, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 626 UCPDESCR(2FH_02H, 0x2F, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 627 UCPDESCR(2FH_04H, 0x2F, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 628 UCPDESCR(2FH_07H, 0x2F, 0x07, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 629 UCPDESCR(2FH_08H, 0x2F, 0x08, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 630 UCPDESCR(2FH_10H, 0x2F, 0x10, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 631 UCPDESCR(2FH_20H, 0x2F, 0x20, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 632 UCPDESCR(2FH_38H, 0x2F, 0x38, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 633 634 UCPDESCR(30H_01H, 0x30, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 635 UCPDESCR(30H_02H, 0x30, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 636 UCPDESCR(30H_04H, 0x30, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 637 UCPDESCR(30H_07H, 0x30, 0x07, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 638 639 UCPDESCR(31H_01H, 0x31, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 640 UCPDESCR(31H_02H, 0x31, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 641 UCPDESCR(31H_04H, 0x31, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 642 UCPDESCR(31H_07H, 0x31, 0x07, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 643 644 UCPDESCR(32H_01H, 0x32, 0x01, UCP_F_FM | UCP_F_WM), 645 UCPDESCR(32H_02H, 0x32, 0x02, UCP_F_FM | UCP_F_WM), 646 UCPDESCR(32H_04H, 0x32, 0x04, UCP_F_FM | UCP_F_WM), 647 UCPDESCR(32H_07H, 0x32, 0x07, UCP_F_FM | UCP_F_WM), 648 649 UCPDESCR(33H_01H, 0x33, 0x01, UCP_F_FM | UCP_F_WM), 650 UCPDESCR(33H_02H, 0x33, 0x02, UCP_F_FM | UCP_F_WM), 651 UCPDESCR(33H_04H, 0x33, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 652 UCPDESCR(33H_07H, 0x33, 0x07, UCP_F_FM | UCP_F_WM), 653 654 UCPDESCR(34H_01H, 0x34, 0x01, UCP_F_FM | UCP_F_WM), 655 UCPDESCR(34H_02H, 0x34, 0x02, UCP_F_FM | UCP_F_WM), 656 UCPDESCR(34H_04H, 0x34, 0x04, UCP_F_FM | UCP_F_WM), 657 UCPDESCR(34H_08H, 0x34, 0x08, UCP_F_FM | UCP_F_WM), 658 UCPDESCR(34H_10H, 0x34, 0x10, UCP_F_FM | UCP_F_WM), 659 UCPDESCR(34H_20H, 0x34, 0x20, UCP_F_FM | UCP_F_WM), 660 661 UCPDESCR(35H_01H, 0x35, 0x01, UCP_F_FM | UCP_F_WM), 662 UCPDESCR(35H_02H, 0x35, 0x02, UCP_F_FM | UCP_F_WM), 663 UCPDESCR(35H_04H, 0x35, 0x04, UCP_F_FM | UCP_F_WM), 664 665 UCPDESCR(40H_01H, 0x40, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 666 UCPDESCR(40H_02H, 0x40, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 667 UCPDESCR(40H_04H, 0x40, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 668 UCPDESCR(40H_08H, 0x40, 0x08, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 669 UCPDESCR(40H_10H, 0x40, 0x10, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 670 UCPDESCR(40H_20H, 0x40, 0x20, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 671 UCPDESCR(40H_07H, 0x40, 0x07, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 672 UCPDESCR(40H_38H, 0x40, 0x38, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 673 674 UCPDESCR(41H_01H, 0x41, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 675 UCPDESCR(41H_02H, 0x41, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 676 UCPDESCR(41H_04H, 0x41, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 677 UCPDESCR(41H_08H, 0x41, 0x08, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 678 UCPDESCR(41H_10H, 0x41, 0x10, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 679 UCPDESCR(41H_20H, 0x41, 0x20, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 680 UCPDESCR(41H_07H, 0x41, 0x07, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 681 UCPDESCR(41H_38H, 0x41, 0x38, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 682 683 UCPDESCR(42H_01H, 0x42, 0x01, UCP_F_FM | UCP_F_WM), 684 UCPDESCR(42H_02H, 0x42, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 685 UCPDESCR(42H_04H, 0x42, 0x04, UCP_F_FM | UCP_F_WM), 686 UCPDESCR(42H_08H, 0x42, 0x08, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 687 688 UCPDESCR(43H_01H, 0x43, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 689 UCPDESCR(43H_02H, 0x43, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 690 691 UCPDESCR(60H_01H, 0x60, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 692 UCPDESCR(60H_02H, 0x60, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 693 UCPDESCR(60H_04H, 0x60, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 694 695 UCPDESCR(61H_01H, 0x61, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 696 UCPDESCR(61H_02H, 0x61, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 697 UCPDESCR(61H_04H, 0x61, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 698 699 UCPDESCR(62H_01H, 0x62, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 700 UCPDESCR(62H_02H, 0x62, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 701 UCPDESCR(62H_04H, 0x62, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 702 703 UCPDESCR(63H_01H, 0x63, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 704 UCPDESCR(63H_02H, 0x63, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 705 UCPDESCR(63H_04H, 0x63, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 706 UCPDESCR(63H_08H, 0x63, 0x08, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 707 UCPDESCR(63H_10H, 0x63, 0x10, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 708 UCPDESCR(63H_20H, 0x63, 0x20, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 709 710 UCPDESCR(64H_01H, 0x64, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 711 UCPDESCR(64H_02H, 0x64, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 712 UCPDESCR(64H_04H, 0x64, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 713 UCPDESCR(64H_08H, 0x64, 0x08, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 714 UCPDESCR(64H_10H, 0x64, 0x10, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 715 UCPDESCR(64H_20H, 0x64, 0x20, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 716 717 UCPDESCR(65H_01H, 0x65, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 718 UCPDESCR(65H_02H, 0x65, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 719 UCPDESCR(65H_04H, 0x65, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 720 721 UCPDESCR(66H_01H, 0x66, 0x01, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 722 UCPDESCR(66H_02H, 0x66, 0x02, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 723 UCPDESCR(66H_04H, 0x66, 0x04, UCP_F_FM | UCP_F_I7 | UCP_F_WM), 724 725 UCPDESCR(67H_01H, 0x67, 0x01, UCP_F_FM | UCP_F_WM), 726 UCPDESCR(80H_01H, 0x80, 0x01, UCP_F_FM | UCP_F_WM), 727 UCPDESCR(80H_02H, 0x80, 0x02, UCP_F_FM | UCP_F_WM), 728 UCPDESCR(80H_04H, 0x80, 0x04, UCP_F_FM | UCP_F_WM), 729 UCPDESCR(80H_08H, 0x80, 0x08, UCP_F_FM | UCP_F_WM), 730 UCPDESCR(81H_01H, 0x81, 0x01, UCP_F_FM | UCP_F_WM), 731 UCPDESCR(81H_02H, 0x81, 0x02, UCP_F_FM | UCP_F_WM), 732 UCPDESCR(81H_04H, 0x81, 0x04, UCP_F_FM | UCP_F_WM), 733 UCPDESCR(81H_08H, 0x81, 0x08, UCP_F_FM | UCP_F_WM), 734 UCPDESCR(82H_01H, 0x82, 0x01, UCP_F_FM | UCP_F_WM), 735 UCPDESCR(83H_01H, 0x83, 0x01, UCP_F_FM | UCP_F_WM), 736 UCPDESCR(83H_02H, 0x83, 0x02, UCP_F_FM | UCP_F_WM), 737 UCPDESCR(83H_04H, 0x83, 0x04, UCP_F_FM | UCP_F_WM), 738 UCPDESCR(83H_08H, 0x83, 0x08, UCP_F_FM | UCP_F_WM), 739 UCPDESCR(84H_01H, 0x84, 0x01, UCP_F_FM | UCP_F_WM), 740 UCPDESCR(84H_02H, 0x84, 0x02, UCP_F_FM | UCP_F_WM), 741 UCPDESCR(84H_04H, 0x84, 0x04, UCP_F_FM | UCP_F_WM), 742 UCPDESCR(84H_08H, 0x84, 0x08, UCP_F_FM | UCP_F_WM), 743 UCPDESCR(85H_02H, 0x85, 0x02, UCP_F_FM | UCP_F_WM), 744 UCPDESCR(86H_01H, 0x86, 0x01, UCP_F_FM | UCP_F_WM) 745 }; 746 747 static const int nucp_events = sizeof(ucp_events) / sizeof(ucp_events[0]); 748 749 static pmc_value_t 750 ucp_perfctr_value_to_reload_count(pmc_value_t v) 751 { 752 v &= (1ULL << uncore_ucp_width) - 1; 753 return (1ULL << uncore_ucp_width) - v; 754 } 755 756 static pmc_value_t 757 ucp_reload_count_to_perfctr_value(pmc_value_t rlc) 758 { 759 return (1ULL << uncore_ucp_width) - rlc; 760 } 761 762 static int 763 ucp_allocate_pmc(int cpu, int ri, struct pmc *pm, 764 const struct pmc_op_pmcallocate *a) 765 { 766 int n; 767 enum pmc_event ev; 768 struct ucp_event_descr *ie; 769 uint32_t caps, config, cpuflag, evsel; 770 771 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 772 ("[uncore,%d] illegal CPU %d", __LINE__, cpu)); 773 KASSERT(ri >= 0 && ri < uncore_ucp_npmc, 774 ("[uncore,%d] illegal row-index value %d", __LINE__, ri)); 775 776 /* check requested capabilities */ 777 caps = a->pm_caps; 778 if ((UCP_PMC_CAPS & caps) != caps) 779 return (EPERM); 780 781 ev = pm->pm_event; 782 783 /* 784 * Look for an event descriptor with matching CPU and event id 785 * fields. 786 */ 787 788 switch (uncore_cputype) { 789 case PMC_CPU_INTEL_COREI7: 790 cpuflag = UCP_F_I7; 791 break; 792 case PMC_CPU_INTEL_WESTMERE: 793 cpuflag = UCP_F_WM; 794 break; 795 default: 796 return (EINVAL); 797 } 798 799 for (n = 0, ie = ucp_events; n < nucp_events; n++, ie++) 800 if (ie->ucp_ev == ev && ie->ucp_flags & cpuflag) 801 break; 802 803 if (n == nucp_events) 804 return (EINVAL); 805 806 /* 807 * A matching event descriptor has been found, so start 808 * assembling the contents of the event select register. 809 */ 810 evsel = ie->ucp_evcode | UCP_EN; 811 812 config = a->pm_md.pm_ucp.pm_ucp_config & ~UCP_F_CMASK; 813 814 /* 815 * If the event uses a fixed umask value, reject any umask 816 * bits set by the user. 817 */ 818 if (ie->ucp_flags & UCP_F_FM) { 819 820 if (UCP_UMASK(config) != 0) 821 return (EINVAL); 822 823 evsel |= (ie->ucp_umask << 8); 824 825 } else 826 return (EINVAL); 827 828 if (caps & PMC_CAP_THRESHOLD) 829 evsel |= (a->pm_md.pm_ucp.pm_ucp_config & UCP_F_CMASK); 830 if (caps & PMC_CAP_EDGE) 831 evsel |= UCP_EDGE; 832 if (caps & PMC_CAP_INVERT) 833 evsel |= UCP_INV; 834 835 pm->pm_md.pm_ucp.pm_ucp_evsel = evsel; 836 837 return (0); 838 } 839 840 static int 841 ucp_config_pmc(int cpu, int ri, struct pmc *pm) 842 { 843 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 844 ("[uncore,%d] illegal CPU %d", __LINE__, cpu)); 845 846 KASSERT(ri >= 0 && ri < uncore_ucp_npmc, 847 ("[uncore,%d] illegal row-index %d", __LINE__, ri)); 848 849 PMCDBG(MDP,CFG,1, "ucp-config cpu=%d ri=%d pm=%p", cpu, ri, pm); 850 851 KASSERT(uncore_pcpu[cpu] != NULL, ("[uncore,%d] null per-cpu %d", __LINE__, 852 cpu)); 853 854 uncore_pcpu[cpu]->pc_uncorepmcs[ri].phw_pmc = pm; 855 856 return (0); 857 } 858 859 static int 860 ucp_describe(int cpu, int ri, struct pmc_info *pi, struct pmc **ppmc) 861 { 862 int error; 863 struct pmc_hw *phw; 864 char ucp_name[PMC_NAME_MAX]; 865 866 phw = &uncore_pcpu[cpu]->pc_uncorepmcs[ri]; 867 868 (void) snprintf(ucp_name, sizeof(ucp_name), "UCP-%d", ri); 869 if ((error = copystr(ucp_name, pi->pm_name, PMC_NAME_MAX, 870 NULL)) != 0) 871 return (error); 872 873 pi->pm_class = PMC_CLASS_UCP; 874 875 if (phw->phw_state & PMC_PHW_FLAG_IS_ENABLED) { 876 pi->pm_enabled = TRUE; 877 *ppmc = phw->phw_pmc; 878 } else { 879 pi->pm_enabled = FALSE; 880 *ppmc = NULL; 881 } 882 883 return (0); 884 } 885 886 static int 887 ucp_get_config(int cpu, int ri, struct pmc **ppm) 888 { 889 *ppm = uncore_pcpu[cpu]->pc_uncorepmcs[ri].phw_pmc; 890 891 return (0); 892 } 893 894 static int 895 ucp_read_pmc(int cpu, int ri, pmc_value_t *v) 896 { 897 struct pmc *pm; 898 pmc_value_t tmp; 899 900 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 901 ("[uncore,%d] illegal cpu value %d", __LINE__, cpu)); 902 KASSERT(ri >= 0 && ri < uncore_ucp_npmc, 903 ("[uncore,%d] illegal row-index %d", __LINE__, ri)); 904 905 pm = uncore_pcpu[cpu]->pc_uncorepmcs[ri].phw_pmc; 906 907 KASSERT(pm, 908 ("[uncore,%d] cpu %d ri %d pmc not configured", __LINE__, cpu, 909 ri)); 910 911 tmp = rdmsr(UCP_PMC0 + ri); 912 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) 913 *v = ucp_perfctr_value_to_reload_count(tmp); 914 else 915 *v = tmp; 916 917 PMCDBG(MDP,REA,1, "ucp-read cpu=%d ri=%d msr=0x%x -> v=%jx", cpu, ri, 918 ri, *v); 919 920 return (0); 921 } 922 923 static int 924 ucp_release_pmc(int cpu, int ri, struct pmc *pm) 925 { 926 (void) pm; 927 928 PMCDBG(MDP,REL,1, "ucp-release cpu=%d ri=%d pm=%p", cpu, ri, 929 pm); 930 931 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 932 ("[uncore,%d] illegal CPU value %d", __LINE__, cpu)); 933 KASSERT(ri >= 0 && ri < uncore_ucp_npmc, 934 ("[uncore,%d] illegal row-index %d", __LINE__, ri)); 935 936 KASSERT(uncore_pcpu[cpu]->pc_uncorepmcs[ri].phw_pmc 937 == NULL, ("[uncore,%d] PHW pmc non-NULL", __LINE__)); 938 939 return (0); 940 } 941 942 static int 943 ucp_start_pmc(int cpu, int ri) 944 { 945 struct pmc *pm; 946 uint32_t evsel; 947 struct uncore_cpu *cc; 948 949 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 950 ("[uncore,%d] illegal CPU value %d", __LINE__, cpu)); 951 KASSERT(ri >= 0 && ri < uncore_ucp_npmc, 952 ("[uncore,%d] illegal row-index %d", __LINE__, ri)); 953 954 cc = uncore_pcpu[cpu]; 955 pm = cc->pc_uncorepmcs[ri].phw_pmc; 956 957 KASSERT(pm, 958 ("[uncore,%d] starting cpu%d,ri%d with no pmc configured", 959 __LINE__, cpu, ri)); 960 961 PMCDBG(MDP,STA,1, "ucp-start cpu=%d ri=%d", cpu, ri); 962 963 evsel = pm->pm_md.pm_ucp.pm_ucp_evsel; 964 965 PMCDBG(MDP,STA,2, "ucp-start/2 cpu=%d ri=%d evselmsr=0x%x evsel=0x%x", 966 cpu, ri, UCP_EVSEL0 + ri, evsel); 967 968 wrmsr(UCP_EVSEL0 + ri, evsel); 969 970 do { 971 cc->pc_resync = 0; 972 cc->pc_globalctrl |= (1ULL << ri); 973 wrmsr(UC_GLOBAL_CTRL, cc->pc_globalctrl); 974 } while (cc->pc_resync != 0); 975 976 return (0); 977 } 978 979 static int 980 ucp_stop_pmc(int cpu, int ri) 981 { 982 struct pmc *pm; 983 struct uncore_cpu *cc; 984 985 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 986 ("[uncore,%d] illegal cpu value %d", __LINE__, cpu)); 987 KASSERT(ri >= 0 && ri < uncore_ucp_npmc, 988 ("[uncore,%d] illegal row index %d", __LINE__, ri)); 989 990 cc = uncore_pcpu[cpu]; 991 pm = cc->pc_uncorepmcs[ri].phw_pmc; 992 993 KASSERT(pm, 994 ("[uncore,%d] cpu%d ri%d no configured PMC to stop", __LINE__, 995 cpu, ri)); 996 997 PMCDBG(MDP,STO,1, "ucp-stop cpu=%d ri=%d", cpu, ri); 998 999 wrmsr(UCP_EVSEL0 + ri, 0); /* stop hw */ 1000 1001 do { 1002 cc->pc_resync = 0; 1003 cc->pc_globalctrl &= ~(1ULL << ri); 1004 wrmsr(UC_GLOBAL_CTRL, cc->pc_globalctrl); 1005 } while (cc->pc_resync != 0); 1006 1007 return (0); 1008 } 1009 1010 static int 1011 ucp_write_pmc(int cpu, int ri, pmc_value_t v) 1012 { 1013 struct pmc *pm; 1014 struct uncore_cpu *cc; 1015 1016 KASSERT(cpu >= 0 && cpu < pmc_cpu_max(), 1017 ("[uncore,%d] illegal cpu value %d", __LINE__, cpu)); 1018 KASSERT(ri >= 0 && ri < uncore_ucp_npmc, 1019 ("[uncore,%d] illegal row index %d", __LINE__, ri)); 1020 1021 cc = uncore_pcpu[cpu]; 1022 pm = cc->pc_uncorepmcs[ri].phw_pmc; 1023 1024 KASSERT(pm, 1025 ("[uncore,%d] cpu%d ri%d no configured PMC to stop", __LINE__, 1026 cpu, ri)); 1027 1028 PMCDBG(MDP,WRI,1, "ucp-write cpu=%d ri=%d msr=0x%x v=%jx", cpu, ri, 1029 UCP_PMC0 + ri, v); 1030 1031 if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) 1032 v = ucp_reload_count_to_perfctr_value(v); 1033 1034 /* 1035 * Write the new value to the counter. The counter will be in 1036 * a stopped state when the pcd_write() entry point is called. 1037 */ 1038 1039 wrmsr(UCP_PMC0 + ri, v); 1040 1041 return (0); 1042 } 1043 1044 1045 static void 1046 ucp_initialize(struct pmc_mdep *md, int maxcpu, int npmc, int pmcwidth) 1047 { 1048 struct pmc_classdep *pcd; 1049 1050 KASSERT(md != NULL, ("[ucp,%d] md is NULL", __LINE__)); 1051 1052 PMCDBG(MDP,INI,1, "%s", "ucp-initialize"); 1053 1054 pcd = &md->pmd_classdep[PMC_MDEP_CLASS_INDEX_UCP]; 1055 1056 pcd->pcd_caps = UCP_PMC_CAPS; 1057 pcd->pcd_class = PMC_CLASS_UCP; 1058 pcd->pcd_num = npmc; 1059 pcd->pcd_ri = md->pmd_npmc; 1060 pcd->pcd_width = pmcwidth; 1061 1062 pcd->pcd_allocate_pmc = ucp_allocate_pmc; 1063 pcd->pcd_config_pmc = ucp_config_pmc; 1064 pcd->pcd_describe = ucp_describe; 1065 pcd->pcd_get_config = ucp_get_config; 1066 pcd->pcd_get_msr = NULL; 1067 pcd->pcd_pcpu_fini = uncore_pcpu_fini; 1068 pcd->pcd_pcpu_init = uncore_pcpu_init; 1069 pcd->pcd_read_pmc = ucp_read_pmc; 1070 pcd->pcd_release_pmc = ucp_release_pmc; 1071 pcd->pcd_start_pmc = ucp_start_pmc; 1072 pcd->pcd_stop_pmc = ucp_stop_pmc; 1073 pcd->pcd_write_pmc = ucp_write_pmc; 1074 1075 md->pmd_npmc += npmc; 1076 } 1077 1078 int 1079 pmc_uncore_initialize(struct pmc_mdep *md, int maxcpu) 1080 { 1081 uncore_cputype = md->pmd_cputype; 1082 uncore_pmcmask = 0; 1083 1084 /* 1085 * Initialize programmable counters. 1086 */ 1087 1088 uncore_ucp_npmc = 8; 1089 uncore_ucp_width = 48; 1090 1091 uncore_pmcmask |= ((1ULL << uncore_ucp_npmc) - 1); 1092 1093 ucp_initialize(md, maxcpu, uncore_ucp_npmc, uncore_ucp_width); 1094 1095 /* 1096 * Initialize fixed function counters, if present. 1097 */ 1098 uncore_ucf_ri = uncore_ucp_npmc; 1099 uncore_ucf_npmc = 1; 1100 uncore_ucf_width = 48; 1101 1102 ucf_initialize(md, maxcpu, uncore_ucf_npmc, uncore_ucf_width); 1103 uncore_pmcmask |= ((1ULL << uncore_ucf_npmc) - 1) << UCF_OFFSET; 1104 1105 PMCDBG(MDP,INI,1,"uncore-init pmcmask=0x%jx ucfri=%d", uncore_pmcmask, 1106 uncore_ucf_ri); 1107 1108 uncore_pcpu = malloc(sizeof(struct uncore_cpu **) * maxcpu, M_PMC, 1109 M_ZERO | M_WAITOK); 1110 1111 return (0); 1112 } 1113 1114 void 1115 pmc_uncore_finalize(struct pmc_mdep *md) 1116 { 1117 PMCDBG(MDP,INI,1, "%s", "uncore-finalize"); 1118 1119 free(uncore_pcpu, M_PMC); 1120 uncore_pcpu = NULL; 1121 } 1122