1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2014, The Linux Foundation. All rights reserved. 4 */ 5 6 #include <linux/acpi.h> 7 #include <linux/bitops.h> 8 #include <linux/kernel.h> 9 #include <linux/kvm_host.h> 10 #include <linux/moduleparam.h> 11 #include <linux/init.h> 12 #include <linux/types.h> 13 #include <linux/device.h> 14 #include <linux/io.h> 15 #include <linux/err.h> 16 #include <linux/fs.h> 17 #include <linux/slab.h> 18 #include <linux/delay.h> 19 #include <linux/smp.h> 20 #include <linux/sysfs.h> 21 #include <linux/stat.h> 22 #include <linux/clk.h> 23 #include <linux/cpu.h> 24 #include <linux/cpu_pm.h> 25 #include <linux/coresight.h> 26 #include <linux/coresight-pmu.h> 27 #include <linux/amba/bus.h> 28 #include <linux/seq_file.h> 29 #include <linux/uaccess.h> 30 #include <linux/perf_event.h> 31 #include <linux/platform_device.h> 32 #include <linux/pm_runtime.h> 33 #include <linux/property.h> 34 #include <linux/clk/clk-conf.h> 35 36 #include <asm/barrier.h> 37 #include <asm/sections.h> 38 #include <asm/sysreg.h> 39 #include <asm/local.h> 40 #include <asm/virt.h> 41 42 #include "coresight-etm4x.h" 43 #include "coresight-etm-perf.h" 44 #include "coresight-etm4x-cfg.h" 45 #include "coresight-self-hosted-trace.h" 46 #include "coresight-syscfg.h" 47 #include "coresight-trace-id.h" 48 49 static int boot_enable; 50 module_param(boot_enable, int, 0444); 51 MODULE_PARM_DESC(boot_enable, "Enable tracing on boot"); 52 53 #define PARAM_PM_SAVE_FIRMWARE 0 /* save self-hosted state as per firmware */ 54 #define PARAM_PM_SAVE_NEVER 1 /* never save any state */ 55 #define PARAM_PM_SAVE_SELF_HOSTED 2 /* save self-hosted state only */ 56 57 static int pm_save_enable = PARAM_PM_SAVE_FIRMWARE; 58 module_param(pm_save_enable, int, 0444); 59 MODULE_PARM_DESC(pm_save_enable, 60 "Save/restore state on power down: 1 = never, 2 = self-hosted"); 61 62 static struct etmv4_drvdata *etmdrvdata[NR_CPUS]; 63 static void etm4_set_default_config(struct etmv4_config *config); 64 static int etm4_set_event_filters(struct etmv4_drvdata *drvdata, 65 struct perf_event *event); 66 static u64 etm4_get_access_type(struct etmv4_config *config); 67 68 static enum cpuhp_state hp_online; 69 70 struct etm4_init_arg { 71 struct device *dev; 72 struct csdev_access *csa; 73 }; 74 75 static DEFINE_PER_CPU(struct etm4_init_arg *, delayed_probe); 76 static int etm4_probe_cpu(unsigned int cpu); 77 78 /* 79 * Check if TRCSSPCICRn(i) is implemented for a given instance. 80 * 81 * TRCSSPCICRn is implemented only if : 82 * TRCSSPCICR<n> is present only if all of the following are true: 83 * TRCIDR4.NUMSSCC > n. 84 * TRCIDR4.NUMPC > 0b0000 . 85 * TRCSSCSR<n>.PC == 0b1 86 */ 87 static bool etm4x_sspcicrn_present(struct etmv4_drvdata *drvdata, int n) 88 { 89 return (n < drvdata->nr_ss_cmp) && 90 drvdata->nr_pe && 91 (drvdata->config.ss_status[n] & TRCSSCSRn_PC); 92 } 93 94 u64 etm4x_sysreg_read(u32 offset, bool _relaxed, bool _64bit) 95 { 96 u64 res = 0; 97 98 switch (offset) { 99 ETM4x_READ_SYSREG_CASES(res) 100 default : 101 pr_warn_ratelimited("etm4x: trying to read unsupported register @%x\n", 102 offset); 103 } 104 105 if (!_relaxed) 106 __io_ar(res); /* Imitate the !relaxed I/O helpers */ 107 108 return res; 109 } 110 111 void etm4x_sysreg_write(u64 val, u32 offset, bool _relaxed, bool _64bit) 112 { 113 if (!_relaxed) 114 __io_bw(); /* Imitate the !relaxed I/O helpers */ 115 if (!_64bit) 116 val &= GENMASK(31, 0); 117 118 switch (offset) { 119 ETM4x_WRITE_SYSREG_CASES(val) 120 default : 121 pr_warn_ratelimited("etm4x: trying to write to unsupported register @%x\n", 122 offset); 123 } 124 } 125 126 static u64 ete_sysreg_read(u32 offset, bool _relaxed, bool _64bit) 127 { 128 u64 res = 0; 129 130 switch (offset) { 131 ETE_READ_CASES(res) 132 default : 133 pr_warn_ratelimited("ete: trying to read unsupported register @%x\n", 134 offset); 135 } 136 137 if (!_relaxed) 138 __io_ar(res); /* Imitate the !relaxed I/O helpers */ 139 140 return res; 141 } 142 143 static void ete_sysreg_write(u64 val, u32 offset, bool _relaxed, bool _64bit) 144 { 145 if (!_relaxed) 146 __io_bw(); /* Imitate the !relaxed I/O helpers */ 147 if (!_64bit) 148 val &= GENMASK(31, 0); 149 150 switch (offset) { 151 ETE_WRITE_CASES(val) 152 default : 153 pr_warn_ratelimited("ete: trying to write to unsupported register @%x\n", 154 offset); 155 } 156 } 157 158 static void etm_detect_os_lock(struct etmv4_drvdata *drvdata, 159 struct csdev_access *csa) 160 { 161 u32 oslsr = etm4x_relaxed_read32(csa, TRCOSLSR); 162 163 drvdata->os_lock_model = ETM_OSLSR_OSLM(oslsr); 164 } 165 166 static void etm_write_os_lock(struct etmv4_drvdata *drvdata, 167 struct csdev_access *csa, u32 val) 168 { 169 val = !!val; 170 171 switch (drvdata->os_lock_model) { 172 case ETM_OSLOCK_PRESENT: 173 etm4x_relaxed_write32(csa, val, TRCOSLAR); 174 break; 175 case ETM_OSLOCK_PE: 176 write_sysreg_s(val, SYS_OSLAR_EL1); 177 break; 178 default: 179 pr_warn_once("CPU%d: Unsupported Trace OSLock model: %x\n", 180 smp_processor_id(), drvdata->os_lock_model); 181 fallthrough; 182 case ETM_OSLOCK_NI: 183 return; 184 } 185 isb(); 186 } 187 188 static void etm4_os_unlock_csa(struct etmv4_drvdata *drvdata, 189 struct csdev_access *csa) 190 { 191 WARN_ON(drvdata->cpu != smp_processor_id()); 192 193 /* Writing 0 to OS Lock unlocks the trace unit registers */ 194 etm_write_os_lock(drvdata, csa, 0x0); 195 drvdata->os_unlock = true; 196 } 197 198 static void etm4_os_unlock(struct etmv4_drvdata *drvdata) 199 { 200 if (!WARN_ON(!drvdata->csdev)) 201 etm4_os_unlock_csa(drvdata, &drvdata->csdev->access); 202 } 203 204 static void etm4_os_lock(struct etmv4_drvdata *drvdata) 205 { 206 if (WARN_ON(!drvdata->csdev)) 207 return; 208 /* Writing 0x1 to OS Lock locks the trace registers */ 209 etm_write_os_lock(drvdata, &drvdata->csdev->access, 0x1); 210 drvdata->os_unlock = false; 211 } 212 213 static void etm4_cs_lock(struct etmv4_drvdata *drvdata, 214 struct csdev_access *csa) 215 { 216 /* Software Lock is only accessible via memory mapped interface */ 217 if (csa->io_mem) 218 CS_LOCK(csa->base); 219 } 220 221 static void etm4_cs_unlock(struct etmv4_drvdata *drvdata, 222 struct csdev_access *csa) 223 { 224 if (csa->io_mem) 225 CS_UNLOCK(csa->base); 226 } 227 228 static int etm4_cpu_id(struct coresight_device *csdev) 229 { 230 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); 231 232 return drvdata->cpu; 233 } 234 235 void etm4_release_trace_id(struct etmv4_drvdata *drvdata) 236 { 237 coresight_trace_id_put_cpu_id(drvdata->cpu); 238 } 239 240 struct etm4_enable_arg { 241 struct etmv4_drvdata *drvdata; 242 int rc; 243 }; 244 245 /* 246 * etm4x_prohibit_trace - Prohibit the CPU from tracing at all ELs. 247 * When the CPU supports FEAT_TRF, we could move the ETM to a trace 248 * prohibited state by filtering the Exception levels via TRFCR_EL1. 249 */ 250 static void etm4x_prohibit_trace(struct etmv4_drvdata *drvdata) 251 { 252 u64 trfcr; 253 254 /* If the CPU doesn't support FEAT_TRF, nothing to do */ 255 if (!drvdata->trfcr) 256 return; 257 258 trfcr = drvdata->trfcr & ~(TRFCR_EL1_ExTRE | TRFCR_EL1_E0TRE); 259 260 write_trfcr(trfcr); 261 kvm_tracing_set_el1_configuration(trfcr); 262 } 263 264 static u64 etm4x_get_kern_user_filter(struct etmv4_drvdata *drvdata) 265 { 266 u64 trfcr = drvdata->trfcr; 267 268 if (drvdata->config.mode & ETM_MODE_EXCL_KERN) 269 trfcr &= ~TRFCR_EL1_ExTRE; 270 if (drvdata->config.mode & ETM_MODE_EXCL_USER) 271 trfcr &= ~TRFCR_EL1_E0TRE; 272 273 return trfcr; 274 } 275 276 /* 277 * etm4x_allow_trace - Allow CPU tracing in the respective ELs, 278 * as configured by the drvdata->config.mode for the current 279 * session. Even though we have TRCVICTLR bits to filter the 280 * trace in the ELs, it doesn't prevent the ETM from generating 281 * a packet (e.g, TraceInfo) that might contain the addresses from 282 * the excluded levels. Thus we use the additional controls provided 283 * via the Trace Filtering controls (FEAT_TRF) to make sure no trace 284 * is generated for the excluded ELs. 285 */ 286 static void etm4x_allow_trace(struct etmv4_drvdata *drvdata) 287 { 288 u64 trfcr, guest_trfcr; 289 290 /* If the CPU doesn't support FEAT_TRF, nothing to do */ 291 if (!drvdata->trfcr) 292 return; 293 294 if (drvdata->config.mode & ETM_MODE_EXCL_HOST) 295 trfcr = drvdata->trfcr & ~(TRFCR_EL1_ExTRE | TRFCR_EL1_E0TRE); 296 else 297 trfcr = etm4x_get_kern_user_filter(drvdata); 298 299 write_trfcr(trfcr); 300 301 /* Set filters for guests and pass to KVM */ 302 if (drvdata->config.mode & ETM_MODE_EXCL_GUEST) 303 guest_trfcr = drvdata->trfcr & ~(TRFCR_EL1_ExTRE | TRFCR_EL1_E0TRE); 304 else 305 guest_trfcr = etm4x_get_kern_user_filter(drvdata); 306 307 /* TRFCR_EL1 doesn't have CX so mask it out. */ 308 guest_trfcr &= ~TRFCR_EL2_CX; 309 kvm_tracing_set_el1_configuration(guest_trfcr); 310 } 311 312 #ifdef CONFIG_ETM4X_IMPDEF_FEATURE 313 314 #define HISI_HIP08_AMBA_ID 0x000b6d01 315 #define ETM4_AMBA_MASK 0xfffff 316 #define HISI_HIP08_CORE_COMMIT_MASK 0x3000 317 #define HISI_HIP08_CORE_COMMIT_SHIFT 12 318 #define HISI_HIP08_CORE_COMMIT_FULL 0b00 319 #define HISI_HIP08_CORE_COMMIT_LVL_1 0b01 320 #define HISI_HIP08_CORE_COMMIT_REG sys_reg(3, 1, 15, 2, 5) 321 322 struct etm4_arch_features { 323 void (*arch_callback)(bool enable); 324 }; 325 326 static bool etm4_hisi_match_pid(unsigned int id) 327 { 328 return (id & ETM4_AMBA_MASK) == HISI_HIP08_AMBA_ID; 329 } 330 331 static void etm4_hisi_config_core_commit(bool enable) 332 { 333 u8 commit = enable ? HISI_HIP08_CORE_COMMIT_LVL_1 : 334 HISI_HIP08_CORE_COMMIT_FULL; 335 u64 val; 336 337 /* 338 * bit 12 and 13 of HISI_HIP08_CORE_COMMIT_REG are used together 339 * to set core-commit, 2'b00 means cpu is at full speed, 2'b01, 340 * 2'b10, 2'b11 mean reduce pipeline speed, and 2'b01 means level-1 341 * speed(minimun value). So bit 12 and 13 should be cleared together. 342 */ 343 val = read_sysreg_s(HISI_HIP08_CORE_COMMIT_REG); 344 val &= ~HISI_HIP08_CORE_COMMIT_MASK; 345 val |= commit << HISI_HIP08_CORE_COMMIT_SHIFT; 346 write_sysreg_s(val, HISI_HIP08_CORE_COMMIT_REG); 347 } 348 349 static struct etm4_arch_features etm4_features[] = { 350 [ETM4_IMPDEF_HISI_CORE_COMMIT] = { 351 .arch_callback = etm4_hisi_config_core_commit, 352 }, 353 {}, 354 }; 355 356 static void etm4_enable_arch_specific(struct etmv4_drvdata *drvdata) 357 { 358 struct etm4_arch_features *ftr; 359 int bit; 360 361 for_each_set_bit(bit, drvdata->arch_features, ETM4_IMPDEF_FEATURE_MAX) { 362 ftr = &etm4_features[bit]; 363 364 if (ftr->arch_callback) 365 ftr->arch_callback(true); 366 } 367 } 368 369 static void etm4_disable_arch_specific(struct etmv4_drvdata *drvdata) 370 { 371 struct etm4_arch_features *ftr; 372 int bit; 373 374 for_each_set_bit(bit, drvdata->arch_features, ETM4_IMPDEF_FEATURE_MAX) { 375 ftr = &etm4_features[bit]; 376 377 if (ftr->arch_callback) 378 ftr->arch_callback(false); 379 } 380 } 381 382 static void etm4_check_arch_features(struct etmv4_drvdata *drvdata, 383 struct csdev_access *csa) 384 { 385 /* 386 * TRCPIDR* registers are not required for ETMs with system 387 * instructions. They must be identified by the MIDR+REVIDRs. 388 * Skip the TRCPID checks for now. 389 */ 390 if (!csa->io_mem) 391 return; 392 393 if (etm4_hisi_match_pid(coresight_get_pid(csa))) 394 set_bit(ETM4_IMPDEF_HISI_CORE_COMMIT, drvdata->arch_features); 395 } 396 #else 397 static void etm4_enable_arch_specific(struct etmv4_drvdata *drvdata) 398 { 399 } 400 401 static void etm4_disable_arch_specific(struct etmv4_drvdata *drvdata) 402 { 403 } 404 405 static void etm4_check_arch_features(struct etmv4_drvdata *drvdata, 406 struct csdev_access *csa) 407 { 408 } 409 #endif /* CONFIG_ETM4X_IMPDEF_FEATURE */ 410 411 static void etm4x_sys_ins_barrier(struct csdev_access *csa, u32 offset, int pos, int val) 412 { 413 if (!csa->io_mem) 414 isb(); 415 } 416 417 /* 418 * etm4x_wait_status: Poll for TRCSTATR.<pos> == <val>. While using system 419 * instruction to access the trace unit, each access must be separated by a 420 * synchronization barrier. See ARM IHI0064H.b section "4.3.7 Synchronization of 421 * register updates", for system instructions section, in "Notes": 422 * 423 * "In particular, whenever disabling or enabling the trace unit, a poll of 424 * TRCSTATR needs explicit synchronization between each read of TRCSTATR" 425 */ 426 static int etm4x_wait_status(struct csdev_access *csa, int pos, int val) 427 { 428 if (!csa->io_mem) 429 return coresight_timeout_action(csa, TRCSTATR, pos, val, 430 etm4x_sys_ins_barrier); 431 return coresight_timeout(csa, TRCSTATR, pos, val); 432 } 433 434 static int etm4_enable_trace_unit(struct etmv4_drvdata *drvdata) 435 { 436 struct coresight_device *csdev = drvdata->csdev; 437 struct device *etm_dev = &csdev->dev; 438 struct csdev_access *csa = &csdev->access; 439 440 /* 441 * ETE mandates that the TRCRSR is written to before 442 * enabling it. 443 */ 444 if (etm4x_is_ete(drvdata)) 445 etm4x_relaxed_write32(csa, TRCRSR_TA, TRCRSR); 446 447 etm4x_allow_trace(drvdata); 448 /* Enable the trace unit */ 449 etm4x_relaxed_write32(csa, 1, TRCPRGCTLR); 450 451 /* Synchronize the register updates for sysreg access */ 452 if (!csa->io_mem) 453 isb(); 454 455 /* wait for TRCSTATR.IDLE to go back down to '0' */ 456 if (etm4x_wait_status(csa, TRCSTATR_IDLE_BIT, 0)) { 457 dev_err(etm_dev, 458 "timeout while waiting for Idle Trace Status\n"); 459 return -ETIME; 460 } 461 462 /* 463 * As recommended by section 4.3.7 ("Synchronization when using the 464 * memory-mapped interface") of ARM IHI 0064D 465 */ 466 dsb(sy); 467 isb(); 468 469 return 0; 470 } 471 472 static int etm4_enable_hw(struct etmv4_drvdata *drvdata) 473 { 474 int i, rc; 475 struct etmv4_config *config = &drvdata->config; 476 struct coresight_device *csdev = drvdata->csdev; 477 struct device *etm_dev = &csdev->dev; 478 struct csdev_access *csa = &csdev->access; 479 480 481 etm4_cs_unlock(drvdata, csa); 482 etm4_enable_arch_specific(drvdata); 483 484 etm4_os_unlock(drvdata); 485 486 rc = coresight_claim_device_unlocked(csdev); 487 if (rc) 488 goto done; 489 490 /* Disable the trace unit before programming trace registers */ 491 etm4x_relaxed_write32(csa, 0, TRCPRGCTLR); 492 493 /* 494 * If we use system instructions, we need to synchronize the 495 * write to the TRCPRGCTLR, before accessing the TRCSTATR. 496 * See ARM IHI0064F, section 497 * "4.3.7 Synchronization of register updates" 498 */ 499 if (!csa->io_mem) 500 isb(); 501 502 /* wait for TRCSTATR.IDLE to go up */ 503 if (etm4x_wait_status(csa, TRCSTATR_IDLE_BIT, 1)) 504 dev_err(etm_dev, 505 "timeout while waiting for Idle Trace Status\n"); 506 if (drvdata->nr_pe) 507 etm4x_relaxed_write32(csa, config->pe_sel, TRCPROCSELR); 508 etm4x_relaxed_write32(csa, config->cfg, TRCCONFIGR); 509 /* nothing specific implemented */ 510 etm4x_relaxed_write32(csa, 0x0, TRCAUXCTLR); 511 etm4x_relaxed_write32(csa, config->eventctrl0, TRCEVENTCTL0R); 512 etm4x_relaxed_write32(csa, config->eventctrl1, TRCEVENTCTL1R); 513 if (drvdata->stallctl) 514 etm4x_relaxed_write32(csa, config->stall_ctrl, TRCSTALLCTLR); 515 etm4x_relaxed_write32(csa, config->ts_ctrl, TRCTSCTLR); 516 etm4x_relaxed_write32(csa, config->syncfreq, TRCSYNCPR); 517 etm4x_relaxed_write32(csa, config->ccctlr, TRCCCCTLR); 518 etm4x_relaxed_write32(csa, config->bb_ctrl, TRCBBCTLR); 519 etm4x_relaxed_write32(csa, drvdata->trcid, TRCTRACEIDR); 520 etm4x_relaxed_write32(csa, config->vinst_ctrl, TRCVICTLR); 521 etm4x_relaxed_write32(csa, config->viiectlr, TRCVIIECTLR); 522 etm4x_relaxed_write32(csa, config->vissctlr, TRCVISSCTLR); 523 if (drvdata->nr_pe_cmp) 524 etm4x_relaxed_write32(csa, config->vipcssctlr, TRCVIPCSSCTLR); 525 for (i = 0; i < drvdata->nrseqstate - 1; i++) 526 etm4x_relaxed_write32(csa, config->seq_ctrl[i], TRCSEQEVRn(i)); 527 if (drvdata->nrseqstate) { 528 etm4x_relaxed_write32(csa, config->seq_rst, TRCSEQRSTEVR); 529 etm4x_relaxed_write32(csa, config->seq_state, TRCSEQSTR); 530 } 531 etm4x_relaxed_write32(csa, config->ext_inp, TRCEXTINSELR); 532 for (i = 0; i < drvdata->nr_cntr; i++) { 533 etm4x_relaxed_write32(csa, config->cntrldvr[i], TRCCNTRLDVRn(i)); 534 etm4x_relaxed_write32(csa, config->cntr_ctrl[i], TRCCNTCTLRn(i)); 535 etm4x_relaxed_write32(csa, config->cntr_val[i], TRCCNTVRn(i)); 536 } 537 538 /* 539 * Resource selector pair 0 is always implemented and reserved. As 540 * such start at 2. 541 */ 542 for (i = 2; i < drvdata->nr_resource * 2; i++) 543 etm4x_relaxed_write32(csa, config->res_ctrl[i], TRCRSCTLRn(i)); 544 545 for (i = 0; i < drvdata->nr_ss_cmp; i++) { 546 /* always clear status bit on restart if using single-shot */ 547 if (config->ss_ctrl[i] || config->ss_pe_cmp[i]) 548 config->ss_status[i] &= ~TRCSSCSRn_STATUS; 549 etm4x_relaxed_write32(csa, config->ss_ctrl[i], TRCSSCCRn(i)); 550 etm4x_relaxed_write32(csa, config->ss_status[i], TRCSSCSRn(i)); 551 if (etm4x_sspcicrn_present(drvdata, i)) 552 etm4x_relaxed_write32(csa, config->ss_pe_cmp[i], TRCSSPCICRn(i)); 553 } 554 for (i = 0; i < drvdata->nr_addr_cmp * 2; i++) { 555 etm4x_relaxed_write64(csa, config->addr_val[i], TRCACVRn(i)); 556 etm4x_relaxed_write64(csa, config->addr_acc[i], TRCACATRn(i)); 557 } 558 for (i = 0; i < drvdata->numcidc; i++) 559 etm4x_relaxed_write64(csa, config->ctxid_pid[i], TRCCIDCVRn(i)); 560 etm4x_relaxed_write32(csa, config->ctxid_mask0, TRCCIDCCTLR0); 561 if (drvdata->numcidc > 4) 562 etm4x_relaxed_write32(csa, config->ctxid_mask1, TRCCIDCCTLR1); 563 564 for (i = 0; i < drvdata->numvmidc; i++) 565 etm4x_relaxed_write64(csa, config->vmid_val[i], TRCVMIDCVRn(i)); 566 etm4x_relaxed_write32(csa, config->vmid_mask0, TRCVMIDCCTLR0); 567 if (drvdata->numvmidc > 4) 568 etm4x_relaxed_write32(csa, config->vmid_mask1, TRCVMIDCCTLR1); 569 570 if (!drvdata->skip_power_up) { 571 u32 trcpdcr = etm4x_relaxed_read32(csa, TRCPDCR); 572 573 /* 574 * Request to keep the trace unit powered and also 575 * emulation of powerdown 576 */ 577 etm4x_relaxed_write32(csa, trcpdcr | TRCPDCR_PU, TRCPDCR); 578 } 579 580 if (!drvdata->paused) 581 rc = etm4_enable_trace_unit(drvdata); 582 done: 583 etm4_cs_lock(drvdata, csa); 584 585 dev_dbg(etm_dev, "cpu: %d enable smp call done: %d\n", 586 drvdata->cpu, rc); 587 return rc; 588 } 589 590 static void etm4_enable_hw_smp_call(void *info) 591 { 592 struct etm4_enable_arg *arg = info; 593 594 if (WARN_ON(!arg)) 595 return; 596 arg->rc = etm4_enable_hw(arg->drvdata); 597 } 598 599 /* 600 * The goal of function etm4_config_timestamp_event() is to configure a 601 * counter that will tell the tracer to emit a timestamp packet when it 602 * reaches zero. This is done in order to get a more fine grained idea 603 * of when instructions are executed so that they can be correlated 604 * with execution on other CPUs. 605 * 606 * To do this the counter itself is configured to self reload and 607 * TRCRSCTLR1 (always true) used to get the counter to decrement. From 608 * there a resource selector is configured with the counter and the 609 * timestamp control register to use the resource selector to trigger the 610 * event that will insert a timestamp packet in the stream. 611 */ 612 static int etm4_config_timestamp_event(struct etmv4_drvdata *drvdata) 613 { 614 int ctridx, ret = -EINVAL; 615 int counter, rselector; 616 u32 val = 0; 617 struct etmv4_config *config = &drvdata->config; 618 619 /* No point in trying if we don't have at least one counter */ 620 if (!drvdata->nr_cntr) 621 goto out; 622 623 /* Find a counter that hasn't been initialised */ 624 for (ctridx = 0; ctridx < drvdata->nr_cntr; ctridx++) 625 if (config->cntr_val[ctridx] == 0) 626 break; 627 628 /* All the counters have been configured already, bail out */ 629 if (ctridx == drvdata->nr_cntr) { 630 pr_debug("%s: no available counter found\n", __func__); 631 ret = -ENOSPC; 632 goto out; 633 } 634 635 /* 636 * Searching for an available resource selector to use, starting at 637 * '2' since every implementation has at least 2 resource selector. 638 * ETMIDR4 gives the number of resource selector _pairs_, 639 * hence multiply by 2. 640 */ 641 for (rselector = 2; rselector < drvdata->nr_resource * 2; rselector++) 642 if (!config->res_ctrl[rselector]) 643 break; 644 645 if (rselector == drvdata->nr_resource * 2) { 646 pr_debug("%s: no available resource selector found\n", 647 __func__); 648 ret = -ENOSPC; 649 goto out; 650 } 651 652 /* Remember what counter we used */ 653 counter = 1 << ctridx; 654 655 /* 656 * Initialise original and reload counter value to the smallest 657 * possible value in order to get as much precision as we can. 658 */ 659 config->cntr_val[ctridx] = 1; 660 config->cntrldvr[ctridx] = 1; 661 662 /* Set the trace counter control register */ 663 val = 0x1 << 16 | /* Bit 16, reload counter automatically */ 664 0x0 << 7 | /* Select single resource selector */ 665 0x1; /* Resource selector 1, i.e always true */ 666 667 config->cntr_ctrl[ctridx] = val; 668 669 val = 0x2 << 16 | /* Group 0b0010 - Counter and sequencers */ 670 counter << 0; /* Counter to use */ 671 672 config->res_ctrl[rselector] = val; 673 674 val = 0x0 << 7 | /* Select single resource selector */ 675 rselector; /* Resource selector */ 676 677 config->ts_ctrl = val; 678 679 ret = 0; 680 out: 681 return ret; 682 } 683 684 static int etm4_parse_event_config(struct coresight_device *csdev, 685 struct perf_event *event) 686 { 687 int ret = 0; 688 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); 689 struct etmv4_config *config = &drvdata->config; 690 struct perf_event_attr *attr = &event->attr; 691 unsigned long cfg_hash; 692 int preset, cc_threshold; 693 694 /* Clear configuration from previous run */ 695 memset(config, 0, sizeof(struct etmv4_config)); 696 697 if (attr->exclude_kernel) 698 config->mode = ETM_MODE_EXCL_KERN; 699 700 if (attr->exclude_user) 701 config->mode = ETM_MODE_EXCL_USER; 702 703 if (attr->exclude_host) 704 config->mode |= ETM_MODE_EXCL_HOST; 705 706 if (attr->exclude_guest) 707 config->mode |= ETM_MODE_EXCL_GUEST; 708 709 /* Always start from the default config */ 710 etm4_set_default_config(config); 711 712 /* Configure filters specified on the perf cmd line, if any. */ 713 ret = etm4_set_event_filters(drvdata, event); 714 if (ret) 715 goto out; 716 717 /* Go from generic option to ETMv4 specifics */ 718 if (attr->config & BIT(ETM_OPT_CYCACC)) { 719 config->cfg |= TRCCONFIGR_CCI; 720 /* TRM: Must program this for cycacc to work */ 721 cc_threshold = attr->config3 & ETM_CYC_THRESHOLD_MASK; 722 if (!cc_threshold) 723 cc_threshold = ETM_CYC_THRESHOLD_DEFAULT; 724 if (cc_threshold < drvdata->ccitmin) 725 cc_threshold = drvdata->ccitmin; 726 config->ccctlr = cc_threshold; 727 } 728 if (attr->config & BIT(ETM_OPT_TS)) { 729 /* 730 * Configure timestamps to be emitted at regular intervals in 731 * order to correlate instructions executed on different CPUs 732 * (CPU-wide trace scenarios). 733 */ 734 ret = etm4_config_timestamp_event(drvdata); 735 736 /* 737 * No need to go further if timestamp intervals can't 738 * be configured. 739 */ 740 if (ret) 741 goto out; 742 743 /* bit[11], Global timestamp tracing bit */ 744 config->cfg |= TRCCONFIGR_TS; 745 } 746 747 /* Only trace contextID when runs in root PID namespace */ 748 if ((attr->config & BIT(ETM_OPT_CTXTID)) && 749 task_is_in_init_pid_ns(current)) 750 /* bit[6], Context ID tracing bit */ 751 config->cfg |= TRCCONFIGR_CID; 752 753 /* 754 * If set bit ETM_OPT_CTXTID2 in perf config, this asks to trace VMID 755 * for recording CONTEXTIDR_EL2. Do not enable VMID tracing if the 756 * kernel is not running in EL2. 757 */ 758 if (attr->config & BIT(ETM_OPT_CTXTID2)) { 759 if (!is_kernel_in_hyp_mode()) { 760 ret = -EINVAL; 761 goto out; 762 } 763 /* Only trace virtual contextID when runs in root PID namespace */ 764 if (task_is_in_init_pid_ns(current)) 765 config->cfg |= TRCCONFIGR_VMID | TRCCONFIGR_VMIDOPT; 766 } 767 768 /* return stack - enable if selected and supported */ 769 if ((attr->config & BIT(ETM_OPT_RETSTK)) && drvdata->retstack) 770 /* bit[12], Return stack enable bit */ 771 config->cfg |= TRCCONFIGR_RS; 772 773 /* 774 * Set any selected configuration and preset. 775 * 776 * This extracts the values of PMU_FORMAT_ATTR(configid) and PMU_FORMAT_ATTR(preset) 777 * in the perf attributes defined in coresight-etm-perf.c. 778 * configid uses bits 63:32 of attr->config2, preset uses bits 3:0 of attr->config. 779 * A zero configid means no configuration active, preset = 0 means no preset selected. 780 */ 781 if (attr->config2 & GENMASK_ULL(63, 32)) { 782 cfg_hash = (u32)(attr->config2 >> 32); 783 preset = attr->config & 0xF; 784 ret = cscfg_csdev_enable_active_config(csdev, cfg_hash, preset); 785 } 786 787 /* branch broadcast - enable if selected and supported */ 788 if (attr->config & BIT(ETM_OPT_BRANCH_BROADCAST)) { 789 if (!drvdata->trcbb) { 790 /* 791 * Missing BB support could cause silent decode errors 792 * so fail to open if it's not supported. 793 */ 794 ret = -EINVAL; 795 goto out; 796 } else { 797 config->cfg |= BIT(ETM4_CFG_BIT_BB); 798 } 799 } 800 801 out: 802 return ret; 803 } 804 805 static int etm4_enable_perf(struct coresight_device *csdev, 806 struct perf_event *event, 807 struct coresight_path *path) 808 { 809 int ret = 0; 810 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); 811 812 if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id())) { 813 ret = -EINVAL; 814 goto out; 815 } 816 817 /* Configure the tracer based on the session's specifics */ 818 ret = etm4_parse_event_config(csdev, event); 819 if (ret) 820 goto out; 821 822 drvdata->trcid = path->trace_id; 823 824 /* Populate pause state */ 825 drvdata->paused = !!READ_ONCE(event->hw.aux_paused); 826 827 /* And enable it */ 828 ret = etm4_enable_hw(drvdata); 829 830 out: 831 return ret; 832 } 833 834 static int etm4_enable_sysfs(struct coresight_device *csdev, struct coresight_path *path) 835 { 836 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); 837 struct etm4_enable_arg arg = { }; 838 unsigned long cfg_hash; 839 int ret, preset; 840 841 /* enable any config activated by configfs */ 842 cscfg_config_sysfs_get_active_cfg(&cfg_hash, &preset); 843 if (cfg_hash) { 844 ret = cscfg_csdev_enable_active_config(csdev, cfg_hash, preset); 845 if (ret) 846 return ret; 847 } 848 849 raw_spin_lock(&drvdata->spinlock); 850 851 drvdata->trcid = path->trace_id; 852 853 /* Tracer will never be paused in sysfs mode */ 854 drvdata->paused = false; 855 856 /* 857 * Executing etm4_enable_hw on the cpu whose ETM is being enabled 858 * ensures that register writes occur when cpu is powered. 859 */ 860 arg.drvdata = drvdata; 861 ret = smp_call_function_single(drvdata->cpu, 862 etm4_enable_hw_smp_call, &arg, 1); 863 if (!ret) 864 ret = arg.rc; 865 if (!ret) 866 drvdata->sticky_enable = true; 867 868 if (ret) 869 etm4_release_trace_id(drvdata); 870 871 raw_spin_unlock(&drvdata->spinlock); 872 873 if (!ret) 874 dev_dbg(&csdev->dev, "ETM tracing enabled\n"); 875 return ret; 876 } 877 878 static int etm4_enable(struct coresight_device *csdev, struct perf_event *event, 879 enum cs_mode mode, struct coresight_path *path) 880 { 881 int ret; 882 883 if (!coresight_take_mode(csdev, mode)) { 884 /* Someone is already using the tracer */ 885 return -EBUSY; 886 } 887 888 switch (mode) { 889 case CS_MODE_SYSFS: 890 ret = etm4_enable_sysfs(csdev, path); 891 break; 892 case CS_MODE_PERF: 893 ret = etm4_enable_perf(csdev, event, path); 894 break; 895 default: 896 ret = -EINVAL; 897 } 898 899 /* The tracer didn't start */ 900 if (ret) 901 coresight_set_mode(csdev, CS_MODE_DISABLED); 902 903 return ret; 904 } 905 906 static void etm4_disable_trace_unit(struct etmv4_drvdata *drvdata) 907 { 908 u32 control; 909 struct coresight_device *csdev = drvdata->csdev; 910 struct device *etm_dev = &csdev->dev; 911 struct csdev_access *csa = &csdev->access; 912 913 control = etm4x_relaxed_read32(csa, TRCPRGCTLR); 914 915 /* EN, bit[0] Trace unit enable bit */ 916 control &= ~0x1; 917 918 /* 919 * If the CPU supports v8.4 Trace filter Control, 920 * set the ETM to trace prohibited region. 921 */ 922 etm4x_prohibit_trace(drvdata); 923 /* 924 * Make sure everything completes before disabling, as recommended 925 * by section 7.3.77 ("TRCVICTLR, ViewInst Main Control Register, 926 * SSTATUS") of ARM IHI 0064D 927 */ 928 dsb(sy); 929 isb(); 930 /* Trace synchronization barrier, is a nop if not supported */ 931 tsb_csync(); 932 etm4x_relaxed_write32(csa, control, TRCPRGCTLR); 933 934 /* 935 * As recommended by section 4.3.7 ("Synchronization when using system 936 * instructions to progrom the trace unit") of ARM IHI 0064H.b, the 937 * self-hosted trace analyzer must perform a Context synchronization 938 * event between writing to the TRCPRGCTLR and reading the TRCSTATR. 939 */ 940 if (!csa->io_mem) 941 isb(); 942 943 /* wait for TRCSTATR.PMSTABLE to go to '1' */ 944 if (etm4x_wait_status(csa, TRCSTATR_PMSTABLE_BIT, 1)) 945 dev_err(etm_dev, 946 "timeout while waiting for PM stable Trace Status\n"); 947 /* 948 * As recommended by section 4.3.7 (Synchronization of register updates) 949 * of ARM IHI 0064H.b. 950 */ 951 isb(); 952 } 953 954 static void etm4_disable_hw(void *info) 955 { 956 u32 control; 957 struct etmv4_drvdata *drvdata = info; 958 struct etmv4_config *config = &drvdata->config; 959 struct coresight_device *csdev = drvdata->csdev; 960 struct csdev_access *csa = &csdev->access; 961 int i; 962 963 etm4_cs_unlock(drvdata, csa); 964 etm4_disable_arch_specific(drvdata); 965 966 if (!drvdata->skip_power_up) { 967 /* power can be removed from the trace unit now */ 968 control = etm4x_relaxed_read32(csa, TRCPDCR); 969 control &= ~TRCPDCR_PU; 970 etm4x_relaxed_write32(csa, control, TRCPDCR); 971 } 972 973 etm4_disable_trace_unit(drvdata); 974 975 /* read the status of the single shot comparators */ 976 for (i = 0; i < drvdata->nr_ss_cmp; i++) { 977 config->ss_status[i] = 978 etm4x_relaxed_read32(csa, TRCSSCSRn(i)); 979 } 980 981 /* read back the current counter values */ 982 for (i = 0; i < drvdata->nr_cntr; i++) { 983 config->cntr_val[i] = 984 etm4x_relaxed_read32(csa, TRCCNTVRn(i)); 985 } 986 987 coresight_disclaim_device_unlocked(csdev); 988 etm4_cs_lock(drvdata, csa); 989 990 dev_dbg(&drvdata->csdev->dev, 991 "cpu: %d disable smp call done\n", drvdata->cpu); 992 } 993 994 static int etm4_disable_perf(struct coresight_device *csdev, 995 struct perf_event *event) 996 { 997 u32 control; 998 struct etm_filters *filters = event->hw.addr_filters; 999 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); 1000 struct perf_event_attr *attr = &event->attr; 1001 1002 if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id())) 1003 return -EINVAL; 1004 1005 etm4_disable_hw(drvdata); 1006 /* 1007 * The config_id occupies bits 63:32 of the config2 perf event attr 1008 * field. If this is non-zero then we will have enabled a config. 1009 */ 1010 if (attr->config2 & GENMASK_ULL(63, 32)) 1011 cscfg_csdev_disable_active_config(csdev); 1012 1013 /* 1014 * Check if the start/stop logic was active when the unit was stopped. 1015 * That way we can re-enable the start/stop logic when the process is 1016 * scheduled again. Configuration of the start/stop logic happens in 1017 * function etm4_set_event_filters(). 1018 */ 1019 control = etm4x_relaxed_read32(&csdev->access, TRCVICTLR); 1020 /* TRCVICTLR::SSSTATUS, bit[9] */ 1021 filters->ssstatus = (control & BIT(9)); 1022 1023 /* 1024 * perf will release trace ids when _free_aux() is 1025 * called at the end of the session. 1026 */ 1027 1028 return 0; 1029 } 1030 1031 static void etm4_disable_sysfs(struct coresight_device *csdev) 1032 { 1033 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); 1034 1035 /* 1036 * Taking hotplug lock here protects from clocks getting disabled 1037 * with tracing being left on (crash scenario) if user disable occurs 1038 * after cpu online mask indicates the cpu is offline but before the 1039 * DYING hotplug callback is serviced by the ETM driver. 1040 */ 1041 cpus_read_lock(); 1042 raw_spin_lock(&drvdata->spinlock); 1043 1044 /* 1045 * Executing etm4_disable_hw on the cpu whose ETM is being disabled 1046 * ensures that register writes occur when cpu is powered. 1047 */ 1048 smp_call_function_single(drvdata->cpu, etm4_disable_hw, drvdata, 1); 1049 1050 raw_spin_unlock(&drvdata->spinlock); 1051 1052 cscfg_csdev_disable_active_config(csdev); 1053 1054 cpus_read_unlock(); 1055 1056 /* 1057 * we only release trace IDs when resetting sysfs. 1058 * This permits sysfs users to read the trace ID after the trace 1059 * session has completed. This maintains operational behaviour with 1060 * prior trace id allocation method 1061 */ 1062 1063 dev_dbg(&csdev->dev, "ETM tracing disabled\n"); 1064 } 1065 1066 static void etm4_disable(struct coresight_device *csdev, 1067 struct perf_event *event) 1068 { 1069 enum cs_mode mode; 1070 1071 /* 1072 * For as long as the tracer isn't disabled another entity can't 1073 * change its status. As such we can read the status here without 1074 * fearing it will change under us. 1075 */ 1076 mode = coresight_get_mode(csdev); 1077 1078 switch (mode) { 1079 case CS_MODE_DISABLED: 1080 break; 1081 case CS_MODE_SYSFS: 1082 etm4_disable_sysfs(csdev); 1083 break; 1084 case CS_MODE_PERF: 1085 etm4_disable_perf(csdev, event); 1086 break; 1087 } 1088 1089 if (mode) 1090 coresight_set_mode(csdev, CS_MODE_DISABLED); 1091 } 1092 1093 static int etm4_resume_perf(struct coresight_device *csdev) 1094 { 1095 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); 1096 struct csdev_access *csa = &csdev->access; 1097 1098 if (coresight_get_mode(csdev) != CS_MODE_PERF) 1099 return -EINVAL; 1100 1101 etm4_cs_unlock(drvdata, csa); 1102 etm4_enable_trace_unit(drvdata); 1103 etm4_cs_lock(drvdata, csa); 1104 1105 drvdata->paused = false; 1106 return 0; 1107 } 1108 1109 static void etm4_pause_perf(struct coresight_device *csdev) 1110 { 1111 struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent); 1112 struct csdev_access *csa = &csdev->access; 1113 1114 if (coresight_get_mode(csdev) != CS_MODE_PERF) 1115 return; 1116 1117 etm4_cs_unlock(drvdata, csa); 1118 etm4_disable_trace_unit(drvdata); 1119 etm4_cs_lock(drvdata, csa); 1120 1121 drvdata->paused = true; 1122 } 1123 1124 static const struct coresight_ops_source etm4_source_ops = { 1125 .cpu_id = etm4_cpu_id, 1126 .enable = etm4_enable, 1127 .disable = etm4_disable, 1128 .resume_perf = etm4_resume_perf, 1129 .pause_perf = etm4_pause_perf, 1130 }; 1131 1132 static const struct coresight_ops etm4_cs_ops = { 1133 .trace_id = coresight_etm_get_trace_id, 1134 .source_ops = &etm4_source_ops, 1135 }; 1136 1137 static bool cpu_supports_sysreg_trace(void) 1138 { 1139 u64 dfr0 = read_sysreg_s(SYS_ID_AA64DFR0_EL1); 1140 1141 return ((dfr0 >> ID_AA64DFR0_EL1_TraceVer_SHIFT) & 0xfUL) > 0; 1142 } 1143 1144 static bool etm4_init_sysreg_access(struct etmv4_drvdata *drvdata, 1145 struct csdev_access *csa) 1146 { 1147 u32 devarch; 1148 1149 if (!cpu_supports_sysreg_trace()) 1150 return false; 1151 1152 /* 1153 * ETMs implementing sysreg access must implement TRCDEVARCH. 1154 */ 1155 devarch = read_etm4x_sysreg_const_offset(TRCDEVARCH); 1156 switch (devarch & ETM_DEVARCH_ID_MASK) { 1157 case ETM_DEVARCH_ETMv4x_ARCH: 1158 *csa = (struct csdev_access) { 1159 .io_mem = false, 1160 .read = etm4x_sysreg_read, 1161 .write = etm4x_sysreg_write, 1162 }; 1163 break; 1164 case ETM_DEVARCH_ETE_ARCH: 1165 *csa = (struct csdev_access) { 1166 .io_mem = false, 1167 .read = ete_sysreg_read, 1168 .write = ete_sysreg_write, 1169 }; 1170 break; 1171 default: 1172 return false; 1173 } 1174 1175 drvdata->arch = etm_devarch_to_arch(devarch); 1176 return true; 1177 } 1178 1179 static bool is_devtype_cpu_trace(void __iomem *base) 1180 { 1181 u32 devtype = readl(base + TRCDEVTYPE); 1182 1183 return (devtype == CS_DEVTYPE_PE_TRACE); 1184 } 1185 1186 static bool etm4_init_iomem_access(struct etmv4_drvdata *drvdata, 1187 struct csdev_access *csa) 1188 { 1189 u32 devarch = readl_relaxed(drvdata->base + TRCDEVARCH); 1190 1191 if (!is_coresight_device(drvdata->base) || !is_devtype_cpu_trace(drvdata->base)) 1192 return false; 1193 1194 /* 1195 * All ETMs must implement TRCDEVARCH to indicate that 1196 * the component is an ETMv4. Even though TRCIDR1 also 1197 * contains the information, it is part of the "Trace" 1198 * register and must be accessed with the OSLK cleared, 1199 * with MMIO. But we cannot touch the OSLK until we are 1200 * sure this is an ETM. So rely only on the TRCDEVARCH. 1201 */ 1202 if ((devarch & ETM_DEVARCH_ID_MASK) != ETM_DEVARCH_ETMv4x_ARCH) { 1203 pr_warn_once("TRCDEVARCH doesn't match ETMv4 architecture\n"); 1204 return false; 1205 } 1206 1207 drvdata->arch = etm_devarch_to_arch(devarch); 1208 *csa = CSDEV_ACCESS_IOMEM(drvdata->base); 1209 return true; 1210 } 1211 1212 static bool etm4_init_csdev_access(struct etmv4_drvdata *drvdata, 1213 struct csdev_access *csa) 1214 { 1215 /* 1216 * Always choose the memory mapped io, if there is 1217 * a memory map to prevent sysreg access on broken 1218 * systems. 1219 */ 1220 if (drvdata->base) 1221 return etm4_init_iomem_access(drvdata, csa); 1222 1223 if (etm4_init_sysreg_access(drvdata, csa)) 1224 return true; 1225 1226 return false; 1227 } 1228 1229 static void cpu_detect_trace_filtering(struct etmv4_drvdata *drvdata) 1230 { 1231 u64 dfr0 = read_sysreg(id_aa64dfr0_el1); 1232 u64 trfcr; 1233 1234 drvdata->trfcr = 0; 1235 if (!cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceFilt_SHIFT)) 1236 return; 1237 1238 /* 1239 * If the CPU supports v8.4 SelfHosted Tracing, enable 1240 * tracing at the kernel EL and EL0, forcing to use the 1241 * virtual time as the timestamp. 1242 */ 1243 trfcr = (FIELD_PREP(TRFCR_EL1_TS_MASK, TRFCR_EL1_TS_VIRTUAL) | 1244 TRFCR_EL1_ExTRE | 1245 TRFCR_EL1_E0TRE); 1246 1247 /* If we are running at EL2, allow tracing the CONTEXTIDR_EL2. */ 1248 if (is_kernel_in_hyp_mode()) 1249 trfcr |= TRFCR_EL2_CX; 1250 1251 drvdata->trfcr = trfcr; 1252 } 1253 1254 /* 1255 * The following errata on applicable cpu ranges, affect the CCITMIN filed 1256 * in TCRIDR3 register. Software read for the field returns 0x100 limiting 1257 * the cycle threshold granularity, whereas the right value should have 1258 * been 0x4, which is well supported in the hardware. 1259 */ 1260 static struct midr_range etm_wrong_ccitmin_cpus[] = { 1261 /* Erratum #1490853 - Cortex-A76 */ 1262 MIDR_RANGE(MIDR_CORTEX_A76, 0, 0, 4, 0), 1263 /* Erratum #1490853 - Neoverse-N1 */ 1264 MIDR_RANGE(MIDR_NEOVERSE_N1, 0, 0, 4, 0), 1265 /* Erratum #1491015 - Cortex-A77 */ 1266 MIDR_RANGE(MIDR_CORTEX_A77, 0, 0, 1, 0), 1267 /* Erratum #1502854 - Cortex-X1 */ 1268 MIDR_REV(MIDR_CORTEX_X1, 0, 0), 1269 /* Erratum #1619801 - Neoverse-V1 */ 1270 MIDR_REV(MIDR_NEOVERSE_V1, 0, 0), 1271 {}, 1272 }; 1273 1274 static void etm4_fixup_wrong_ccitmin(struct etmv4_drvdata *drvdata) 1275 { 1276 /* 1277 * Erratum affected cpus will read 256 as the minimum 1278 * instruction trace cycle counting threshold whereas 1279 * the correct value should be 4 instead. Override the 1280 * recorded value for 'drvdata->ccitmin' to workaround 1281 * this problem. 1282 */ 1283 if (is_midr_in_range_list(etm_wrong_ccitmin_cpus)) { 1284 if (drvdata->ccitmin == 256) 1285 drvdata->ccitmin = 4; 1286 } 1287 } 1288 1289 static void etm4_init_arch_data(void *info) 1290 { 1291 u32 etmidr0; 1292 u32 etmidr2; 1293 u32 etmidr3; 1294 u32 etmidr4; 1295 u32 etmidr5; 1296 struct etm4_init_arg *init_arg = info; 1297 struct etmv4_drvdata *drvdata; 1298 struct csdev_access *csa; 1299 struct device *dev = init_arg->dev; 1300 int i; 1301 1302 drvdata = dev_get_drvdata(init_arg->dev); 1303 csa = init_arg->csa; 1304 1305 /* 1306 * If we are unable to detect the access mechanism, 1307 * or unable to detect the trace unit type, fail 1308 * early. 1309 */ 1310 if (!etm4_init_csdev_access(drvdata, csa)) 1311 return; 1312 1313 if (!csa->io_mem || 1314 fwnode_property_present(dev_fwnode(dev), "qcom,skip-power-up")) 1315 drvdata->skip_power_up = true; 1316 1317 /* Detect the support for OS Lock before we actually use it */ 1318 etm_detect_os_lock(drvdata, csa); 1319 1320 /* Make sure all registers are accessible */ 1321 etm4_os_unlock_csa(drvdata, csa); 1322 etm4_cs_unlock(drvdata, csa); 1323 1324 etm4_check_arch_features(drvdata, csa); 1325 1326 /* find all capabilities of the tracing unit */ 1327 etmidr0 = etm4x_relaxed_read32(csa, TRCIDR0); 1328 1329 /* INSTP0, bits[2:1] P0 tracing support field */ 1330 drvdata->instrp0 = !!(FIELD_GET(TRCIDR0_INSTP0_MASK, etmidr0) == 0b11); 1331 /* TRCBB, bit[5] Branch broadcast tracing support bit */ 1332 drvdata->trcbb = !!(etmidr0 & TRCIDR0_TRCBB); 1333 /* TRCCOND, bit[6] Conditional instruction tracing support bit */ 1334 drvdata->trccond = !!(etmidr0 & TRCIDR0_TRCCOND); 1335 /* TRCCCI, bit[7] Cycle counting instruction bit */ 1336 drvdata->trccci = !!(etmidr0 & TRCIDR0_TRCCCI); 1337 /* RETSTACK, bit[9] Return stack bit */ 1338 drvdata->retstack = !!(etmidr0 & TRCIDR0_RETSTACK); 1339 /* NUMEVENT, bits[11:10] Number of events field */ 1340 drvdata->nr_event = FIELD_GET(TRCIDR0_NUMEVENT_MASK, etmidr0); 1341 /* QSUPP, bits[16:15] Q element support field */ 1342 drvdata->q_support = FIELD_GET(TRCIDR0_QSUPP_MASK, etmidr0); 1343 if (drvdata->q_support) 1344 drvdata->q_filt = !!(etmidr0 & TRCIDR0_QFILT); 1345 /* TSSIZE, bits[28:24] Global timestamp size field */ 1346 drvdata->ts_size = FIELD_GET(TRCIDR0_TSSIZE_MASK, etmidr0); 1347 1348 /* maximum size of resources */ 1349 etmidr2 = etm4x_relaxed_read32(csa, TRCIDR2); 1350 /* CIDSIZE, bits[9:5] Indicates the Context ID size */ 1351 drvdata->ctxid_size = FIELD_GET(TRCIDR2_CIDSIZE_MASK, etmidr2); 1352 /* VMIDSIZE, bits[14:10] Indicates the VMID size */ 1353 drvdata->vmid_size = FIELD_GET(TRCIDR2_VMIDSIZE_MASK, etmidr2); 1354 /* CCSIZE, bits[28:25] size of the cycle counter in bits minus 12 */ 1355 drvdata->ccsize = FIELD_GET(TRCIDR2_CCSIZE_MASK, etmidr2); 1356 1357 etmidr3 = etm4x_relaxed_read32(csa, TRCIDR3); 1358 /* CCITMIN, bits[11:0] minimum threshold value that can be programmed */ 1359 drvdata->ccitmin = FIELD_GET(TRCIDR3_CCITMIN_MASK, etmidr3); 1360 etm4_fixup_wrong_ccitmin(drvdata); 1361 1362 /* EXLEVEL_S, bits[19:16] Secure state instruction tracing */ 1363 drvdata->s_ex_level = FIELD_GET(TRCIDR3_EXLEVEL_S_MASK, etmidr3); 1364 drvdata->config.s_ex_level = drvdata->s_ex_level; 1365 /* EXLEVEL_NS, bits[23:20] Non-secure state instruction tracing */ 1366 drvdata->ns_ex_level = FIELD_GET(TRCIDR3_EXLEVEL_NS_MASK, etmidr3); 1367 /* 1368 * TRCERR, bit[24] whether a trace unit can trace a 1369 * system error exception. 1370 */ 1371 drvdata->trc_error = !!(etmidr3 & TRCIDR3_TRCERR); 1372 /* SYNCPR, bit[25] implementation has a fixed synchronization period? */ 1373 drvdata->syncpr = !!(etmidr3 & TRCIDR3_SYNCPR); 1374 /* STALLCTL, bit[26] is stall control implemented? */ 1375 drvdata->stallctl = !!(etmidr3 & TRCIDR3_STALLCTL); 1376 /* SYSSTALL, bit[27] implementation can support stall control? */ 1377 drvdata->sysstall = !!(etmidr3 & TRCIDR3_SYSSTALL); 1378 /* 1379 * NUMPROC - the number of PEs available for tracing, 5bits 1380 * = TRCIDR3.bits[13:12]bits[30:28] 1381 * bits[4:3] = TRCIDR3.bits[13:12] (since etm-v4.2, otherwise RES0) 1382 * bits[3:0] = TRCIDR3.bits[30:28] 1383 */ 1384 drvdata->nr_pe = (FIELD_GET(TRCIDR3_NUMPROC_HI_MASK, etmidr3) << 3) | 1385 FIELD_GET(TRCIDR3_NUMPROC_LO_MASK, etmidr3); 1386 /* NOOVERFLOW, bit[31] is trace overflow prevention supported */ 1387 drvdata->nooverflow = !!(etmidr3 & TRCIDR3_NOOVERFLOW); 1388 1389 /* number of resources trace unit supports */ 1390 etmidr4 = etm4x_relaxed_read32(csa, TRCIDR4); 1391 /* NUMACPAIRS, bits[0:3] number of addr comparator pairs for tracing */ 1392 drvdata->nr_addr_cmp = FIELD_GET(TRCIDR4_NUMACPAIRS_MASK, etmidr4); 1393 /* NUMPC, bits[15:12] number of PE comparator inputs for tracing */ 1394 drvdata->nr_pe_cmp = FIELD_GET(TRCIDR4_NUMPC_MASK, etmidr4); 1395 /* 1396 * NUMRSPAIR, bits[19:16] 1397 * The number of resource pairs conveyed by the HW starts at 0, i.e a 1398 * value of 0x0 indicate 1 resource pair, 0x1 indicate two and so on. 1399 * As such add 1 to the value of NUMRSPAIR for a better representation. 1400 * 1401 * For ETM v4.3 and later, 0x0 means 0, and no pairs are available - 1402 * the default TRUE and FALSE resource selectors are omitted. 1403 * Otherwise for values 0x1 and above the number is N + 1 as per v4.2. 1404 */ 1405 drvdata->nr_resource = FIELD_GET(TRCIDR4_NUMRSPAIR_MASK, etmidr4); 1406 if ((drvdata->arch < ETM_ARCH_V4_3) || (drvdata->nr_resource > 0)) 1407 drvdata->nr_resource += 1; 1408 /* 1409 * NUMSSCC, bits[23:20] the number of single-shot 1410 * comparator control for tracing. Read any status regs as these 1411 * also contain RO capability data. 1412 */ 1413 drvdata->nr_ss_cmp = FIELD_GET(TRCIDR4_NUMSSCC_MASK, etmidr4); 1414 for (i = 0; i < drvdata->nr_ss_cmp; i++) { 1415 drvdata->config.ss_status[i] = 1416 etm4x_relaxed_read32(csa, TRCSSCSRn(i)); 1417 } 1418 /* NUMCIDC, bits[27:24] number of Context ID comparators for tracing */ 1419 drvdata->numcidc = FIELD_GET(TRCIDR4_NUMCIDC_MASK, etmidr4); 1420 /* NUMVMIDC, bits[31:28] number of VMID comparators for tracing */ 1421 drvdata->numvmidc = FIELD_GET(TRCIDR4_NUMVMIDC_MASK, etmidr4); 1422 1423 etmidr5 = etm4x_relaxed_read32(csa, TRCIDR5); 1424 /* NUMEXTIN, bits[8:0] number of external inputs implemented */ 1425 drvdata->nr_ext_inp = FIELD_GET(TRCIDR5_NUMEXTIN_MASK, etmidr5); 1426 /* TRACEIDSIZE, bits[21:16] indicates the trace ID width */ 1427 drvdata->trcid_size = FIELD_GET(TRCIDR5_TRACEIDSIZE_MASK, etmidr5); 1428 /* ATBTRIG, bit[22] implementation can support ATB triggers? */ 1429 drvdata->atbtrig = !!(etmidr5 & TRCIDR5_ATBTRIG); 1430 /* 1431 * LPOVERRIDE, bit[23] implementation supports 1432 * low-power state override 1433 */ 1434 drvdata->lpoverride = (etmidr5 & TRCIDR5_LPOVERRIDE) && (!drvdata->skip_power_up); 1435 /* NUMSEQSTATE, bits[27:25] number of sequencer states implemented */ 1436 drvdata->nrseqstate = FIELD_GET(TRCIDR5_NUMSEQSTATE_MASK, etmidr5); 1437 /* NUMCNTR, bits[30:28] number of counters available for tracing */ 1438 drvdata->nr_cntr = FIELD_GET(TRCIDR5_NUMCNTR_MASK, etmidr5); 1439 1440 coresight_clear_self_claim_tag_unlocked(csa); 1441 etm4_cs_lock(drvdata, csa); 1442 cpu_detect_trace_filtering(drvdata); 1443 } 1444 1445 static u32 etm4_get_victlr_access_type(struct etmv4_config *config) 1446 { 1447 return etm4_get_access_type(config) << __bf_shf(TRCVICTLR_EXLEVEL_MASK); 1448 } 1449 1450 /* Set ELx trace filter access in the TRCVICTLR register */ 1451 static void etm4_set_victlr_access(struct etmv4_config *config) 1452 { 1453 config->vinst_ctrl &= ~TRCVICTLR_EXLEVEL_MASK; 1454 config->vinst_ctrl |= etm4_get_victlr_access_type(config); 1455 } 1456 1457 static void etm4_set_default_config(struct etmv4_config *config) 1458 { 1459 /* disable all events tracing */ 1460 config->eventctrl0 = 0x0; 1461 config->eventctrl1 = 0x0; 1462 1463 /* disable stalling */ 1464 config->stall_ctrl = 0x0; 1465 1466 /* enable trace synchronization every 4096 bytes, if available */ 1467 config->syncfreq = 0xC; 1468 1469 /* disable timestamp event */ 1470 config->ts_ctrl = 0x0; 1471 1472 /* TRCVICTLR::EVENT = 0x01, select the always on logic */ 1473 config->vinst_ctrl = FIELD_PREP(TRCVICTLR_EVENT_MASK, 0x01); 1474 1475 /* TRCVICTLR::EXLEVEL_NS:EXLEVELS: Set kernel / user filtering */ 1476 etm4_set_victlr_access(config); 1477 } 1478 1479 static u64 etm4_get_ns_access_type(struct etmv4_config *config) 1480 { 1481 u64 access_type = 0; 1482 1483 /* 1484 * EXLEVEL_NS, for NonSecure Exception levels. 1485 * The mask here is a generic value and must be 1486 * shifted to the corresponding field for the registers 1487 */ 1488 if (!is_kernel_in_hyp_mode()) { 1489 /* Stay away from hypervisor mode for non-VHE */ 1490 access_type = ETM_EXLEVEL_NS_HYP; 1491 if (config->mode & ETM_MODE_EXCL_KERN) 1492 access_type |= ETM_EXLEVEL_NS_OS; 1493 } else if (config->mode & ETM_MODE_EXCL_KERN) { 1494 access_type = ETM_EXLEVEL_NS_HYP; 1495 } 1496 1497 if (config->mode & ETM_MODE_EXCL_USER) 1498 access_type |= ETM_EXLEVEL_NS_APP; 1499 1500 return access_type; 1501 } 1502 1503 /* 1504 * Construct the exception level masks for a given config. 1505 * This must be shifted to the corresponding register field 1506 * for usage. 1507 */ 1508 static u64 etm4_get_access_type(struct etmv4_config *config) 1509 { 1510 /* All Secure exception levels are excluded from the trace */ 1511 return etm4_get_ns_access_type(config) | (u64)config->s_ex_level; 1512 } 1513 1514 static u64 etm4_get_comparator_access_type(struct etmv4_config *config) 1515 { 1516 return etm4_get_access_type(config) << TRCACATR_EXLEVEL_SHIFT; 1517 } 1518 1519 static void etm4_set_comparator_filter(struct etmv4_config *config, 1520 u64 start, u64 stop, int comparator) 1521 { 1522 u64 access_type = etm4_get_comparator_access_type(config); 1523 1524 /* First half of default address comparator */ 1525 config->addr_val[comparator] = start; 1526 config->addr_acc[comparator] = access_type; 1527 config->addr_type[comparator] = ETM_ADDR_TYPE_RANGE; 1528 1529 /* Second half of default address comparator */ 1530 config->addr_val[comparator + 1] = stop; 1531 config->addr_acc[comparator + 1] = access_type; 1532 config->addr_type[comparator + 1] = ETM_ADDR_TYPE_RANGE; 1533 1534 /* 1535 * Configure the ViewInst function to include this address range 1536 * comparator. 1537 * 1538 * @comparator is divided by two since it is the index in the 1539 * etmv4_config::addr_val array but register TRCVIIECTLR deals with 1540 * address range comparator _pairs_. 1541 * 1542 * Therefore: 1543 * index 0 -> compatator pair 0 1544 * index 2 -> comparator pair 1 1545 * index 4 -> comparator pair 2 1546 * ... 1547 * index 14 -> comparator pair 7 1548 */ 1549 config->viiectlr |= BIT(comparator / 2); 1550 } 1551 1552 static void etm4_set_start_stop_filter(struct etmv4_config *config, 1553 u64 address, int comparator, 1554 enum etm_addr_type type) 1555 { 1556 int shift; 1557 u64 access_type = etm4_get_comparator_access_type(config); 1558 1559 /* Configure the comparator */ 1560 config->addr_val[comparator] = address; 1561 config->addr_acc[comparator] = access_type; 1562 config->addr_type[comparator] = type; 1563 1564 /* 1565 * Configure ViewInst Start-Stop control register. 1566 * Addresses configured to start tracing go from bit 0 to n-1, 1567 * while those configured to stop tracing from 16 to 16 + n-1. 1568 */ 1569 shift = (type == ETM_ADDR_TYPE_START ? 0 : 16); 1570 config->vissctlr |= BIT(shift + comparator); 1571 } 1572 1573 static void etm4_set_default_filter(struct etmv4_config *config) 1574 { 1575 /* Trace everything 'default' filter achieved by no filtering */ 1576 config->viiectlr = 0x0; 1577 1578 /* 1579 * TRCVICTLR::SSSTATUS == 1, the start-stop logic is 1580 * in the started state 1581 */ 1582 config->vinst_ctrl |= TRCVICTLR_SSSTATUS; 1583 config->mode |= ETM_MODE_VIEWINST_STARTSTOP; 1584 1585 /* No start-stop filtering for ViewInst */ 1586 config->vissctlr = 0x0; 1587 } 1588 1589 static void etm4_set_default(struct etmv4_config *config) 1590 { 1591 if (WARN_ON_ONCE(!config)) 1592 return; 1593 1594 /* 1595 * Make default initialisation trace everything 1596 * 1597 * This is done by a minimum default config sufficient to enable 1598 * full instruction trace - with a default filter for trace all 1599 * achieved by having no filtering. 1600 */ 1601 etm4_set_default_config(config); 1602 etm4_set_default_filter(config); 1603 } 1604 1605 static int etm4_get_next_comparator(struct etmv4_drvdata *drvdata, u32 type) 1606 { 1607 int nr_comparator, index = 0; 1608 struct etmv4_config *config = &drvdata->config; 1609 1610 /* 1611 * nr_addr_cmp holds the number of comparator _pair_, so time 2 1612 * for the total number of comparators. 1613 */ 1614 nr_comparator = drvdata->nr_addr_cmp * 2; 1615 1616 /* Go through the tally of comparators looking for a free one. */ 1617 while (index < nr_comparator) { 1618 switch (type) { 1619 case ETM_ADDR_TYPE_RANGE: 1620 if (config->addr_type[index] == ETM_ADDR_TYPE_NONE && 1621 config->addr_type[index + 1] == ETM_ADDR_TYPE_NONE) 1622 return index; 1623 1624 /* Address range comparators go in pairs */ 1625 index += 2; 1626 break; 1627 case ETM_ADDR_TYPE_START: 1628 case ETM_ADDR_TYPE_STOP: 1629 if (config->addr_type[index] == ETM_ADDR_TYPE_NONE) 1630 return index; 1631 1632 /* Start/stop address can have odd indexes */ 1633 index += 1; 1634 break; 1635 default: 1636 return -EINVAL; 1637 } 1638 } 1639 1640 /* If we are here all the comparators have been used. */ 1641 return -ENOSPC; 1642 } 1643 1644 static int etm4_set_event_filters(struct etmv4_drvdata *drvdata, 1645 struct perf_event *event) 1646 { 1647 int i, comparator, ret = 0; 1648 u64 address; 1649 struct etmv4_config *config = &drvdata->config; 1650 struct etm_filters *filters = event->hw.addr_filters; 1651 1652 if (!filters) 1653 goto default_filter; 1654 1655 /* Sync events with what Perf got */ 1656 perf_event_addr_filters_sync(event); 1657 1658 /* 1659 * If there are no filters to deal with simply go ahead with 1660 * the default filter, i.e the entire address range. 1661 */ 1662 if (!filters->nr_filters) 1663 goto default_filter; 1664 1665 for (i = 0; i < filters->nr_filters; i++) { 1666 struct etm_filter *filter = &filters->etm_filter[i]; 1667 enum etm_addr_type type = filter->type; 1668 1669 /* See if a comparator is free. */ 1670 comparator = etm4_get_next_comparator(drvdata, type); 1671 if (comparator < 0) { 1672 ret = comparator; 1673 goto out; 1674 } 1675 1676 switch (type) { 1677 case ETM_ADDR_TYPE_RANGE: 1678 etm4_set_comparator_filter(config, 1679 filter->start_addr, 1680 filter->stop_addr, 1681 comparator); 1682 /* 1683 * TRCVICTLR::SSSTATUS == 1, the start-stop logic is 1684 * in the started state 1685 */ 1686 config->vinst_ctrl |= TRCVICTLR_SSSTATUS; 1687 1688 /* No start-stop filtering for ViewInst */ 1689 config->vissctlr = 0x0; 1690 break; 1691 case ETM_ADDR_TYPE_START: 1692 case ETM_ADDR_TYPE_STOP: 1693 /* Get the right start or stop address */ 1694 address = (type == ETM_ADDR_TYPE_START ? 1695 filter->start_addr : 1696 filter->stop_addr); 1697 1698 /* Configure comparator */ 1699 etm4_set_start_stop_filter(config, address, 1700 comparator, type); 1701 1702 /* 1703 * If filters::ssstatus == 1, trace acquisition was 1704 * started but the process was yanked away before the 1705 * stop address was hit. As such the start/stop 1706 * logic needs to be re-started so that tracing can 1707 * resume where it left. 1708 * 1709 * The start/stop logic status when a process is 1710 * scheduled out is checked in function 1711 * etm4_disable_perf(). 1712 */ 1713 if (filters->ssstatus) 1714 config->vinst_ctrl |= TRCVICTLR_SSSTATUS; 1715 1716 /* No include/exclude filtering for ViewInst */ 1717 config->viiectlr = 0x0; 1718 break; 1719 default: 1720 ret = -EINVAL; 1721 goto out; 1722 } 1723 } 1724 1725 goto out; 1726 1727 1728 default_filter: 1729 etm4_set_default_filter(config); 1730 1731 out: 1732 return ret; 1733 } 1734 1735 void etm4_config_trace_mode(struct etmv4_config *config) 1736 { 1737 u32 mode; 1738 1739 mode = config->mode; 1740 mode &= (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER); 1741 1742 /* excluding kernel AND user space doesn't make sense */ 1743 WARN_ON_ONCE(mode == (ETM_MODE_EXCL_KERN | ETM_MODE_EXCL_USER)); 1744 1745 /* nothing to do if neither flags are set */ 1746 if (!(mode & ETM_MODE_EXCL_KERN) && !(mode & ETM_MODE_EXCL_USER)) 1747 return; 1748 1749 etm4_set_victlr_access(config); 1750 } 1751 1752 static int etm4_online_cpu(unsigned int cpu) 1753 { 1754 if (!etmdrvdata[cpu]) 1755 return etm4_probe_cpu(cpu); 1756 1757 if (etmdrvdata[cpu]->boot_enable && !etmdrvdata[cpu]->sticky_enable) 1758 coresight_enable_sysfs(etmdrvdata[cpu]->csdev); 1759 return 0; 1760 } 1761 1762 static int etm4_starting_cpu(unsigned int cpu) 1763 { 1764 if (!etmdrvdata[cpu]) 1765 return 0; 1766 1767 raw_spin_lock(&etmdrvdata[cpu]->spinlock); 1768 if (!etmdrvdata[cpu]->os_unlock) 1769 etm4_os_unlock(etmdrvdata[cpu]); 1770 1771 if (coresight_get_mode(etmdrvdata[cpu]->csdev)) 1772 etm4_enable_hw(etmdrvdata[cpu]); 1773 raw_spin_unlock(&etmdrvdata[cpu]->spinlock); 1774 return 0; 1775 } 1776 1777 static int etm4_dying_cpu(unsigned int cpu) 1778 { 1779 if (!etmdrvdata[cpu]) 1780 return 0; 1781 1782 raw_spin_lock(&etmdrvdata[cpu]->spinlock); 1783 if (coresight_get_mode(etmdrvdata[cpu]->csdev)) 1784 etm4_disable_hw(etmdrvdata[cpu]); 1785 raw_spin_unlock(&etmdrvdata[cpu]->spinlock); 1786 return 0; 1787 } 1788 1789 static int __etm4_cpu_save(struct etmv4_drvdata *drvdata) 1790 { 1791 int i, ret = 0; 1792 struct etmv4_save_state *state; 1793 struct coresight_device *csdev = drvdata->csdev; 1794 struct csdev_access *csa; 1795 struct device *etm_dev; 1796 1797 if (WARN_ON(!csdev)) 1798 return -ENODEV; 1799 1800 etm_dev = &csdev->dev; 1801 csa = &csdev->access; 1802 1803 /* 1804 * As recommended by 3.4.1 ("The procedure when powering down the PE") 1805 * of ARM IHI 0064D 1806 */ 1807 dsb(sy); 1808 isb(); 1809 1810 etm4_cs_unlock(drvdata, csa); 1811 /* Lock the OS lock to disable trace and external debugger access */ 1812 etm4_os_lock(drvdata); 1813 1814 /* wait for TRCSTATR.PMSTABLE to go up */ 1815 if (etm4x_wait_status(csa, TRCSTATR_PMSTABLE_BIT, 1)) { 1816 dev_err(etm_dev, 1817 "timeout while waiting for PM Stable Status\n"); 1818 etm4_os_unlock(drvdata); 1819 ret = -EBUSY; 1820 goto out; 1821 } 1822 1823 state = drvdata->save_state; 1824 1825 state->trcprgctlr = etm4x_read32(csa, TRCPRGCTLR); 1826 if (drvdata->nr_pe) 1827 state->trcprocselr = etm4x_read32(csa, TRCPROCSELR); 1828 state->trcconfigr = etm4x_read32(csa, TRCCONFIGR); 1829 state->trcauxctlr = etm4x_read32(csa, TRCAUXCTLR); 1830 state->trceventctl0r = etm4x_read32(csa, TRCEVENTCTL0R); 1831 state->trceventctl1r = etm4x_read32(csa, TRCEVENTCTL1R); 1832 if (drvdata->stallctl) 1833 state->trcstallctlr = etm4x_read32(csa, TRCSTALLCTLR); 1834 state->trctsctlr = etm4x_read32(csa, TRCTSCTLR); 1835 state->trcsyncpr = etm4x_read32(csa, TRCSYNCPR); 1836 state->trcccctlr = etm4x_read32(csa, TRCCCCTLR); 1837 state->trcbbctlr = etm4x_read32(csa, TRCBBCTLR); 1838 state->trctraceidr = etm4x_read32(csa, TRCTRACEIDR); 1839 if (drvdata->q_filt) 1840 state->trcqctlr = etm4x_read32(csa, TRCQCTLR); 1841 1842 state->trcvictlr = etm4x_read32(csa, TRCVICTLR); 1843 state->trcviiectlr = etm4x_read32(csa, TRCVIIECTLR); 1844 state->trcvissctlr = etm4x_read32(csa, TRCVISSCTLR); 1845 if (drvdata->nr_pe_cmp) 1846 state->trcvipcssctlr = etm4x_read32(csa, TRCVIPCSSCTLR); 1847 1848 for (i = 0; i < drvdata->nrseqstate - 1; i++) 1849 state->trcseqevr[i] = etm4x_read32(csa, TRCSEQEVRn(i)); 1850 1851 if (drvdata->nrseqstate) { 1852 state->trcseqrstevr = etm4x_read32(csa, TRCSEQRSTEVR); 1853 state->trcseqstr = etm4x_read32(csa, TRCSEQSTR); 1854 } 1855 state->trcextinselr = etm4x_read32(csa, TRCEXTINSELR); 1856 1857 for (i = 0; i < drvdata->nr_cntr; i++) { 1858 state->trccntrldvr[i] = etm4x_read32(csa, TRCCNTRLDVRn(i)); 1859 state->trccntctlr[i] = etm4x_read32(csa, TRCCNTCTLRn(i)); 1860 state->trccntvr[i] = etm4x_read32(csa, TRCCNTVRn(i)); 1861 } 1862 1863 /* Resource selector pair 0 is reserved */ 1864 for (i = 2; i < drvdata->nr_resource * 2; i++) 1865 state->trcrsctlr[i] = etm4x_read32(csa, TRCRSCTLRn(i)); 1866 1867 for (i = 0; i < drvdata->nr_ss_cmp; i++) { 1868 state->trcssccr[i] = etm4x_read32(csa, TRCSSCCRn(i)); 1869 state->trcsscsr[i] = etm4x_read32(csa, TRCSSCSRn(i)); 1870 if (etm4x_sspcicrn_present(drvdata, i)) 1871 state->trcsspcicr[i] = etm4x_read32(csa, TRCSSPCICRn(i)); 1872 } 1873 1874 for (i = 0; i < drvdata->nr_addr_cmp * 2; i++) { 1875 state->trcacvr[i] = etm4x_read64(csa, TRCACVRn(i)); 1876 state->trcacatr[i] = etm4x_read64(csa, TRCACATRn(i)); 1877 } 1878 1879 /* 1880 * Data trace stream is architecturally prohibited for A profile cores 1881 * so we don't save (or later restore) trcdvcvr and trcdvcmr - As per 1882 * section 1.3.4 ("Possible functional configurations of an ETMv4 trace 1883 * unit") of ARM IHI 0064D. 1884 */ 1885 1886 for (i = 0; i < drvdata->numcidc; i++) 1887 state->trccidcvr[i] = etm4x_read64(csa, TRCCIDCVRn(i)); 1888 1889 for (i = 0; i < drvdata->numvmidc; i++) 1890 state->trcvmidcvr[i] = etm4x_read64(csa, TRCVMIDCVRn(i)); 1891 1892 state->trccidcctlr0 = etm4x_read32(csa, TRCCIDCCTLR0); 1893 if (drvdata->numcidc > 4) 1894 state->trccidcctlr1 = etm4x_read32(csa, TRCCIDCCTLR1); 1895 1896 state->trcvmidcctlr0 = etm4x_read32(csa, TRCVMIDCCTLR0); 1897 if (drvdata->numvmidc > 4) 1898 state->trcvmidcctlr0 = etm4x_read32(csa, TRCVMIDCCTLR1); 1899 1900 state->trcclaimset = etm4x_read32(csa, TRCCLAIMCLR); 1901 1902 if (!drvdata->skip_power_up) 1903 state->trcpdcr = etm4x_read32(csa, TRCPDCR); 1904 1905 /* wait for TRCSTATR.IDLE to go up */ 1906 if (etm4x_wait_status(csa, TRCSTATR_PMSTABLE_BIT, 1)) { 1907 dev_err(etm_dev, 1908 "timeout while waiting for Idle Trace Status\n"); 1909 etm4_os_unlock(drvdata); 1910 ret = -EBUSY; 1911 goto out; 1912 } 1913 1914 drvdata->state_needs_restore = true; 1915 1916 /* 1917 * Power can be removed from the trace unit now. We do this to 1918 * potentially save power on systems that respect the TRCPDCR_PU 1919 * despite requesting software to save/restore state. 1920 */ 1921 if (!drvdata->skip_power_up) 1922 etm4x_relaxed_write32(csa, (state->trcpdcr & ~TRCPDCR_PU), 1923 TRCPDCR); 1924 out: 1925 etm4_cs_lock(drvdata, csa); 1926 return ret; 1927 } 1928 1929 static int etm4_cpu_save(struct etmv4_drvdata *drvdata) 1930 { 1931 int ret = 0; 1932 1933 /* Save the TRFCR irrespective of whether the ETM is ON */ 1934 if (drvdata->trfcr) 1935 drvdata->save_trfcr = read_trfcr(); 1936 /* 1937 * Save and restore the ETM Trace registers only if 1938 * the ETM is active. 1939 */ 1940 if (coresight_get_mode(drvdata->csdev) && drvdata->save_state) 1941 ret = __etm4_cpu_save(drvdata); 1942 return ret; 1943 } 1944 1945 static void __etm4_cpu_restore(struct etmv4_drvdata *drvdata) 1946 { 1947 int i; 1948 struct etmv4_save_state *state = drvdata->save_state; 1949 struct csdev_access *csa = &drvdata->csdev->access; 1950 1951 if (WARN_ON(!drvdata->csdev)) 1952 return; 1953 1954 etm4_cs_unlock(drvdata, csa); 1955 etm4x_relaxed_write32(csa, state->trcclaimset, TRCCLAIMSET); 1956 1957 etm4x_relaxed_write32(csa, state->trcprgctlr, TRCPRGCTLR); 1958 if (drvdata->nr_pe) 1959 etm4x_relaxed_write32(csa, state->trcprocselr, TRCPROCSELR); 1960 etm4x_relaxed_write32(csa, state->trcconfigr, TRCCONFIGR); 1961 etm4x_relaxed_write32(csa, state->trcauxctlr, TRCAUXCTLR); 1962 etm4x_relaxed_write32(csa, state->trceventctl0r, TRCEVENTCTL0R); 1963 etm4x_relaxed_write32(csa, state->trceventctl1r, TRCEVENTCTL1R); 1964 if (drvdata->stallctl) 1965 etm4x_relaxed_write32(csa, state->trcstallctlr, TRCSTALLCTLR); 1966 etm4x_relaxed_write32(csa, state->trctsctlr, TRCTSCTLR); 1967 etm4x_relaxed_write32(csa, state->trcsyncpr, TRCSYNCPR); 1968 etm4x_relaxed_write32(csa, state->trcccctlr, TRCCCCTLR); 1969 etm4x_relaxed_write32(csa, state->trcbbctlr, TRCBBCTLR); 1970 etm4x_relaxed_write32(csa, state->trctraceidr, TRCTRACEIDR); 1971 if (drvdata->q_filt) 1972 etm4x_relaxed_write32(csa, state->trcqctlr, TRCQCTLR); 1973 1974 etm4x_relaxed_write32(csa, state->trcvictlr, TRCVICTLR); 1975 etm4x_relaxed_write32(csa, state->trcviiectlr, TRCVIIECTLR); 1976 etm4x_relaxed_write32(csa, state->trcvissctlr, TRCVISSCTLR); 1977 if (drvdata->nr_pe_cmp) 1978 etm4x_relaxed_write32(csa, state->trcvipcssctlr, TRCVIPCSSCTLR); 1979 1980 for (i = 0; i < drvdata->nrseqstate - 1; i++) 1981 etm4x_relaxed_write32(csa, state->trcseqevr[i], TRCSEQEVRn(i)); 1982 1983 if (drvdata->nrseqstate) { 1984 etm4x_relaxed_write32(csa, state->trcseqrstevr, TRCSEQRSTEVR); 1985 etm4x_relaxed_write32(csa, state->trcseqstr, TRCSEQSTR); 1986 } 1987 etm4x_relaxed_write32(csa, state->trcextinselr, TRCEXTINSELR); 1988 1989 for (i = 0; i < drvdata->nr_cntr; i++) { 1990 etm4x_relaxed_write32(csa, state->trccntrldvr[i], TRCCNTRLDVRn(i)); 1991 etm4x_relaxed_write32(csa, state->trccntctlr[i], TRCCNTCTLRn(i)); 1992 etm4x_relaxed_write32(csa, state->trccntvr[i], TRCCNTVRn(i)); 1993 } 1994 1995 /* Resource selector pair 0 is reserved */ 1996 for (i = 2; i < drvdata->nr_resource * 2; i++) 1997 etm4x_relaxed_write32(csa, state->trcrsctlr[i], TRCRSCTLRn(i)); 1998 1999 for (i = 0; i < drvdata->nr_ss_cmp; i++) { 2000 etm4x_relaxed_write32(csa, state->trcssccr[i], TRCSSCCRn(i)); 2001 etm4x_relaxed_write32(csa, state->trcsscsr[i], TRCSSCSRn(i)); 2002 if (etm4x_sspcicrn_present(drvdata, i)) 2003 etm4x_relaxed_write32(csa, state->trcsspcicr[i], TRCSSPCICRn(i)); 2004 } 2005 2006 for (i = 0; i < drvdata->nr_addr_cmp * 2; i++) { 2007 etm4x_relaxed_write64(csa, state->trcacvr[i], TRCACVRn(i)); 2008 etm4x_relaxed_write64(csa, state->trcacatr[i], TRCACATRn(i)); 2009 } 2010 2011 for (i = 0; i < drvdata->numcidc; i++) 2012 etm4x_relaxed_write64(csa, state->trccidcvr[i], TRCCIDCVRn(i)); 2013 2014 for (i = 0; i < drvdata->numvmidc; i++) 2015 etm4x_relaxed_write64(csa, state->trcvmidcvr[i], TRCVMIDCVRn(i)); 2016 2017 etm4x_relaxed_write32(csa, state->trccidcctlr0, TRCCIDCCTLR0); 2018 if (drvdata->numcidc > 4) 2019 etm4x_relaxed_write32(csa, state->trccidcctlr1, TRCCIDCCTLR1); 2020 2021 etm4x_relaxed_write32(csa, state->trcvmidcctlr0, TRCVMIDCCTLR0); 2022 if (drvdata->numvmidc > 4) 2023 etm4x_relaxed_write32(csa, state->trcvmidcctlr0, TRCVMIDCCTLR1); 2024 2025 etm4x_relaxed_write32(csa, state->trcclaimset, TRCCLAIMSET); 2026 2027 if (!drvdata->skip_power_up) 2028 etm4x_relaxed_write32(csa, state->trcpdcr, TRCPDCR); 2029 2030 drvdata->state_needs_restore = false; 2031 2032 /* 2033 * As recommended by section 4.3.7 ("Synchronization when using the 2034 * memory-mapped interface") of ARM IHI 0064D 2035 */ 2036 dsb(sy); 2037 isb(); 2038 2039 /* Unlock the OS lock to re-enable trace and external debug access */ 2040 etm4_os_unlock(drvdata); 2041 etm4_cs_lock(drvdata, csa); 2042 } 2043 2044 static void etm4_cpu_restore(struct etmv4_drvdata *drvdata) 2045 { 2046 if (drvdata->trfcr) 2047 write_trfcr(drvdata->save_trfcr); 2048 if (drvdata->state_needs_restore) 2049 __etm4_cpu_restore(drvdata); 2050 } 2051 2052 static int etm4_cpu_pm_notify(struct notifier_block *nb, unsigned long cmd, 2053 void *v) 2054 { 2055 struct etmv4_drvdata *drvdata; 2056 unsigned int cpu = smp_processor_id(); 2057 2058 if (!etmdrvdata[cpu]) 2059 return NOTIFY_OK; 2060 2061 drvdata = etmdrvdata[cpu]; 2062 2063 if (WARN_ON_ONCE(drvdata->cpu != cpu)) 2064 return NOTIFY_BAD; 2065 2066 switch (cmd) { 2067 case CPU_PM_ENTER: 2068 if (etm4_cpu_save(drvdata)) 2069 return NOTIFY_BAD; 2070 break; 2071 case CPU_PM_EXIT: 2072 case CPU_PM_ENTER_FAILED: 2073 etm4_cpu_restore(drvdata); 2074 break; 2075 default: 2076 return NOTIFY_DONE; 2077 } 2078 2079 return NOTIFY_OK; 2080 } 2081 2082 static struct notifier_block etm4_cpu_pm_nb = { 2083 .notifier_call = etm4_cpu_pm_notify, 2084 }; 2085 2086 /* Setup PM. Deals with error conditions and counts */ 2087 static int __init etm4_pm_setup(void) 2088 { 2089 int ret; 2090 2091 ret = cpu_pm_register_notifier(&etm4_cpu_pm_nb); 2092 if (ret) 2093 return ret; 2094 2095 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ARM_CORESIGHT_STARTING, 2096 "arm/coresight4:starting", 2097 etm4_starting_cpu, etm4_dying_cpu); 2098 2099 if (ret) 2100 goto unregister_notifier; 2101 2102 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, 2103 "arm/coresight4:online", 2104 etm4_online_cpu, NULL); 2105 2106 /* HP dyn state ID returned in ret on success */ 2107 if (ret > 0) { 2108 hp_online = ret; 2109 return 0; 2110 } 2111 2112 /* failed dyn state - remove others */ 2113 cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT_STARTING); 2114 2115 unregister_notifier: 2116 cpu_pm_unregister_notifier(&etm4_cpu_pm_nb); 2117 return ret; 2118 } 2119 2120 static void etm4_pm_clear(void) 2121 { 2122 cpu_pm_unregister_notifier(&etm4_cpu_pm_nb); 2123 cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT_STARTING); 2124 if (hp_online) { 2125 cpuhp_remove_state_nocalls(hp_online); 2126 hp_online = 0; 2127 } 2128 } 2129 2130 static int etm4_add_coresight_dev(struct etm4_init_arg *init_arg) 2131 { 2132 int ret; 2133 struct coresight_platform_data *pdata = NULL; 2134 struct device *dev = init_arg->dev; 2135 struct etmv4_drvdata *drvdata = dev_get_drvdata(dev); 2136 struct coresight_desc desc = { 0 }; 2137 u8 major, minor; 2138 char *type_name; 2139 2140 if (!drvdata) 2141 return -EINVAL; 2142 2143 desc.access = *init_arg->csa; 2144 2145 if (!drvdata->arch) 2146 return -EINVAL; 2147 2148 major = ETM_ARCH_MAJOR_VERSION(drvdata->arch); 2149 minor = ETM_ARCH_MINOR_VERSION(drvdata->arch); 2150 2151 if (etm4x_is_ete(drvdata)) { 2152 type_name = "ete"; 2153 /* ETE v1 has major version == 0b101. Adjust this for logging.*/ 2154 major -= 4; 2155 } else { 2156 type_name = "etm"; 2157 } 2158 2159 desc.name = devm_kasprintf(dev, GFP_KERNEL, 2160 "%s%d", type_name, drvdata->cpu); 2161 if (!desc.name) 2162 return -ENOMEM; 2163 2164 etm4_set_default(&drvdata->config); 2165 2166 pdata = coresight_get_platform_data(dev); 2167 if (IS_ERR(pdata)) 2168 return PTR_ERR(pdata); 2169 2170 dev->platform_data = pdata; 2171 2172 desc.type = CORESIGHT_DEV_TYPE_SOURCE; 2173 desc.subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC; 2174 desc.ops = &etm4_cs_ops; 2175 desc.pdata = pdata; 2176 desc.dev = dev; 2177 desc.groups = coresight_etmv4_groups; 2178 drvdata->csdev = coresight_register(&desc); 2179 if (IS_ERR(drvdata->csdev)) 2180 return PTR_ERR(drvdata->csdev); 2181 2182 ret = etm_perf_symlink(drvdata->csdev, true); 2183 if (ret) { 2184 coresight_unregister(drvdata->csdev); 2185 return ret; 2186 } 2187 2188 /* register with config infrastructure & load any current features */ 2189 ret = etm4_cscfg_register(drvdata->csdev); 2190 if (ret) { 2191 coresight_unregister(drvdata->csdev); 2192 return ret; 2193 } 2194 2195 etmdrvdata[drvdata->cpu] = drvdata; 2196 2197 dev_info(&drvdata->csdev->dev, "CPU%d: %s v%d.%d initialized\n", 2198 drvdata->cpu, type_name, major, minor); 2199 2200 if (boot_enable) { 2201 coresight_enable_sysfs(drvdata->csdev); 2202 drvdata->boot_enable = true; 2203 } 2204 2205 return 0; 2206 } 2207 2208 static int etm4_probe(struct device *dev) 2209 { 2210 struct etmv4_drvdata *drvdata = dev_get_drvdata(dev); 2211 struct csdev_access access = { 0 }; 2212 struct etm4_init_arg init_arg = { 0 }; 2213 struct etm4_init_arg *delayed; 2214 2215 if (WARN_ON(!drvdata)) 2216 return -ENOMEM; 2217 2218 if (pm_save_enable == PARAM_PM_SAVE_FIRMWARE) 2219 pm_save_enable = coresight_loses_context_with_cpu(dev) ? 2220 PARAM_PM_SAVE_SELF_HOSTED : PARAM_PM_SAVE_NEVER; 2221 2222 if (pm_save_enable != PARAM_PM_SAVE_NEVER) { 2223 drvdata->save_state = devm_kmalloc(dev, 2224 sizeof(struct etmv4_save_state), GFP_KERNEL); 2225 if (!drvdata->save_state) 2226 return -ENOMEM; 2227 } 2228 2229 raw_spin_lock_init(&drvdata->spinlock); 2230 2231 drvdata->cpu = coresight_get_cpu(dev); 2232 if (drvdata->cpu < 0) 2233 return drvdata->cpu; 2234 2235 init_arg.dev = dev; 2236 init_arg.csa = &access; 2237 2238 /* 2239 * Serialize against CPUHP callbacks to avoid race condition 2240 * between the smp call and saving the delayed probe. 2241 */ 2242 cpus_read_lock(); 2243 if (smp_call_function_single(drvdata->cpu, 2244 etm4_init_arch_data, &init_arg, 1)) { 2245 /* The CPU was offline, try again once it comes online. */ 2246 delayed = devm_kmalloc(dev, sizeof(*delayed), GFP_KERNEL); 2247 if (!delayed) { 2248 cpus_read_unlock(); 2249 return -ENOMEM; 2250 } 2251 2252 *delayed = init_arg; 2253 2254 per_cpu(delayed_probe, drvdata->cpu) = delayed; 2255 2256 cpus_read_unlock(); 2257 return 0; 2258 } 2259 cpus_read_unlock(); 2260 2261 return etm4_add_coresight_dev(&init_arg); 2262 } 2263 2264 static int etm4_probe_amba(struct amba_device *adev, const struct amba_id *id) 2265 { 2266 struct etmv4_drvdata *drvdata; 2267 void __iomem *base; 2268 struct device *dev = &adev->dev; 2269 struct resource *res = &adev->res; 2270 int ret; 2271 2272 /* Validity for the resource is already checked by the AMBA core */ 2273 base = devm_ioremap_resource(dev, res); 2274 if (IS_ERR(base)) 2275 return PTR_ERR(base); 2276 2277 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL); 2278 if (!drvdata) 2279 return -ENOMEM; 2280 2281 drvdata->base = base; 2282 dev_set_drvdata(dev, drvdata); 2283 ret = etm4_probe(dev); 2284 if (!ret) 2285 pm_runtime_put(&adev->dev); 2286 2287 return ret; 2288 } 2289 2290 static int etm4_probe_platform_dev(struct platform_device *pdev) 2291 { 2292 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2293 struct etmv4_drvdata *drvdata; 2294 int ret; 2295 2296 drvdata = devm_kzalloc(&pdev->dev, sizeof(*drvdata), GFP_KERNEL); 2297 if (!drvdata) 2298 return -ENOMEM; 2299 2300 drvdata->pclk = coresight_get_enable_apb_pclk(&pdev->dev); 2301 if (IS_ERR(drvdata->pclk)) 2302 return -ENODEV; 2303 2304 if (res) { 2305 drvdata->base = devm_ioremap_resource(&pdev->dev, res); 2306 if (IS_ERR(drvdata->base)) { 2307 clk_put(drvdata->pclk); 2308 return PTR_ERR(drvdata->base); 2309 } 2310 } 2311 2312 dev_set_drvdata(&pdev->dev, drvdata); 2313 pm_runtime_get_noresume(&pdev->dev); 2314 pm_runtime_set_active(&pdev->dev); 2315 pm_runtime_enable(&pdev->dev); 2316 2317 ret = etm4_probe(&pdev->dev); 2318 2319 pm_runtime_put(&pdev->dev); 2320 if (ret) 2321 pm_runtime_disable(&pdev->dev); 2322 2323 return ret; 2324 } 2325 2326 static int etm4_probe_cpu(unsigned int cpu) 2327 { 2328 int ret; 2329 struct etm4_init_arg init_arg; 2330 struct csdev_access access = { 0 }; 2331 struct etm4_init_arg *iap = *this_cpu_ptr(&delayed_probe); 2332 2333 if (!iap) 2334 return 0; 2335 2336 init_arg = *iap; 2337 devm_kfree(init_arg.dev, iap); 2338 *this_cpu_ptr(&delayed_probe) = NULL; 2339 2340 ret = pm_runtime_resume_and_get(init_arg.dev); 2341 if (ret < 0) { 2342 dev_err(init_arg.dev, "Failed to get PM runtime!\n"); 2343 return 0; 2344 } 2345 2346 init_arg.csa = &access; 2347 etm4_init_arch_data(&init_arg); 2348 2349 etm4_add_coresight_dev(&init_arg); 2350 2351 pm_runtime_put(init_arg.dev); 2352 return 0; 2353 } 2354 2355 static struct amba_cs_uci_id uci_id_etm4[] = { 2356 { 2357 /* ETMv4 UCI data */ 2358 .devarch = ETM_DEVARCH_ETMv4x_ARCH, 2359 .devarch_mask = ETM_DEVARCH_ID_MASK, 2360 .devtype = CS_DEVTYPE_PE_TRACE, 2361 } 2362 }; 2363 2364 static void clear_etmdrvdata(void *info) 2365 { 2366 int cpu = *(int *)info; 2367 2368 etmdrvdata[cpu] = NULL; 2369 per_cpu(delayed_probe, cpu) = NULL; 2370 } 2371 2372 static void etm4_remove_dev(struct etmv4_drvdata *drvdata) 2373 { 2374 bool had_delayed_probe; 2375 /* 2376 * Taking hotplug lock here to avoid racing between etm4_remove_dev() 2377 * and CPU hotplug call backs. 2378 */ 2379 cpus_read_lock(); 2380 2381 had_delayed_probe = per_cpu(delayed_probe, drvdata->cpu); 2382 2383 /* 2384 * The readers for etmdrvdata[] are CPU hotplug call backs 2385 * and PM notification call backs. Change etmdrvdata[i] on 2386 * CPU i ensures these call backs has consistent view 2387 * inside one call back function. 2388 */ 2389 if (smp_call_function_single(drvdata->cpu, clear_etmdrvdata, &drvdata->cpu, 1)) 2390 clear_etmdrvdata(&drvdata->cpu); 2391 2392 cpus_read_unlock(); 2393 2394 if (!had_delayed_probe) { 2395 etm_perf_symlink(drvdata->csdev, false); 2396 cscfg_unregister_csdev(drvdata->csdev); 2397 coresight_unregister(drvdata->csdev); 2398 } 2399 } 2400 2401 static void etm4_remove_amba(struct amba_device *adev) 2402 { 2403 struct etmv4_drvdata *drvdata = dev_get_drvdata(&adev->dev); 2404 2405 if (drvdata) 2406 etm4_remove_dev(drvdata); 2407 } 2408 2409 static void etm4_remove_platform_dev(struct platform_device *pdev) 2410 { 2411 struct etmv4_drvdata *drvdata = dev_get_drvdata(&pdev->dev); 2412 2413 if (drvdata) 2414 etm4_remove_dev(drvdata); 2415 pm_runtime_disable(&pdev->dev); 2416 2417 if (drvdata && !IS_ERR_OR_NULL(drvdata->pclk)) 2418 clk_put(drvdata->pclk); 2419 } 2420 2421 static const struct amba_id etm4_ids[] = { 2422 CS_AMBA_ID(0x000bb95d), /* Cortex-A53 */ 2423 CS_AMBA_ID(0x000bb95e), /* Cortex-A57 */ 2424 CS_AMBA_ID(0x000bb95a), /* Cortex-A72 */ 2425 CS_AMBA_ID(0x000bb959), /* Cortex-A73 */ 2426 CS_AMBA_UCI_ID(0x000bb9da, uci_id_etm4),/* Cortex-A35 */ 2427 CS_AMBA_UCI_ID(0x000bbd05, uci_id_etm4),/* Cortex-A55 */ 2428 CS_AMBA_UCI_ID(0x000bbd0a, uci_id_etm4),/* Cortex-A75 */ 2429 CS_AMBA_UCI_ID(0x000bbd0c, uci_id_etm4),/* Neoverse N1 */ 2430 CS_AMBA_UCI_ID(0x000bbd41, uci_id_etm4),/* Cortex-A78 */ 2431 CS_AMBA_UCI_ID(0x000f0205, uci_id_etm4),/* Qualcomm Kryo */ 2432 CS_AMBA_UCI_ID(0x000f0211, uci_id_etm4),/* Qualcomm Kryo */ 2433 CS_AMBA_UCI_ID(0x000bb802, uci_id_etm4),/* Qualcomm Kryo 385 Cortex-A55 */ 2434 CS_AMBA_UCI_ID(0x000bb803, uci_id_etm4),/* Qualcomm Kryo 385 Cortex-A75 */ 2435 CS_AMBA_UCI_ID(0x000bb805, uci_id_etm4),/* Qualcomm Kryo 4XX Cortex-A55 */ 2436 CS_AMBA_UCI_ID(0x000bb804, uci_id_etm4),/* Qualcomm Kryo 4XX Cortex-A76 */ 2437 CS_AMBA_UCI_ID(0x000bbd0d, uci_id_etm4),/* Qualcomm Kryo 5XX Cortex-A77 */ 2438 CS_AMBA_UCI_ID(0x000cc0af, uci_id_etm4),/* Marvell ThunderX2 */ 2439 CS_AMBA_UCI_ID(0x000b6d01, uci_id_etm4),/* HiSilicon-Hip08 */ 2440 CS_AMBA_UCI_ID(0x000b6d02, uci_id_etm4),/* HiSilicon-Hip09 */ 2441 /* 2442 * Match all PIDs with ETM4 DEVARCH. No need for adding any of the new 2443 * CPUs to the list here. 2444 */ 2445 CS_AMBA_MATCH_ALL_UCI(uci_id_etm4), 2446 {}, 2447 }; 2448 2449 MODULE_DEVICE_TABLE(amba, etm4_ids); 2450 2451 static struct amba_driver etm4x_amba_driver = { 2452 .drv = { 2453 .name = "coresight-etm4x", 2454 .suppress_bind_attrs = true, 2455 }, 2456 .probe = etm4_probe_amba, 2457 .remove = etm4_remove_amba, 2458 .id_table = etm4_ids, 2459 }; 2460 2461 #ifdef CONFIG_PM 2462 static int etm4_runtime_suspend(struct device *dev) 2463 { 2464 struct etmv4_drvdata *drvdata = dev_get_drvdata(dev); 2465 2466 if (drvdata->pclk && !IS_ERR(drvdata->pclk)) 2467 clk_disable_unprepare(drvdata->pclk); 2468 2469 return 0; 2470 } 2471 2472 static int etm4_runtime_resume(struct device *dev) 2473 { 2474 struct etmv4_drvdata *drvdata = dev_get_drvdata(dev); 2475 2476 if (drvdata->pclk && !IS_ERR(drvdata->pclk)) 2477 clk_prepare_enable(drvdata->pclk); 2478 2479 return 0; 2480 } 2481 #endif 2482 2483 static const struct dev_pm_ops etm4_dev_pm_ops = { 2484 SET_RUNTIME_PM_OPS(etm4_runtime_suspend, etm4_runtime_resume, NULL) 2485 }; 2486 2487 static const struct of_device_id etm4_sysreg_match[] = { 2488 { .compatible = "arm,coresight-etm4x-sysreg" }, 2489 { .compatible = "arm,embedded-trace-extension" }, 2490 {} 2491 }; 2492 2493 #ifdef CONFIG_ACPI 2494 static const struct acpi_device_id etm4x_acpi_ids[] = { 2495 {"ARMHC500", 0, 0, 0}, /* ARM CoreSight ETM4x */ 2496 {} 2497 }; 2498 MODULE_DEVICE_TABLE(acpi, etm4x_acpi_ids); 2499 #endif 2500 2501 static struct platform_driver etm4_platform_driver = { 2502 .probe = etm4_probe_platform_dev, 2503 .remove = etm4_remove_platform_dev, 2504 .driver = { 2505 .name = "coresight-etm4x", 2506 .of_match_table = etm4_sysreg_match, 2507 .acpi_match_table = ACPI_PTR(etm4x_acpi_ids), 2508 .suppress_bind_attrs = true, 2509 .pm = &etm4_dev_pm_ops, 2510 }, 2511 }; 2512 2513 static int __init etm4x_init(void) 2514 { 2515 int ret; 2516 2517 ret = etm4_pm_setup(); 2518 2519 /* etm4_pm_setup() does its own cleanup - exit on error */ 2520 if (ret) 2521 return ret; 2522 2523 ret = amba_driver_register(&etm4x_amba_driver); 2524 if (ret) { 2525 pr_err("Error registering etm4x AMBA driver\n"); 2526 goto clear_pm; 2527 } 2528 2529 ret = platform_driver_register(&etm4_platform_driver); 2530 if (!ret) 2531 return 0; 2532 2533 pr_err("Error registering etm4x platform driver\n"); 2534 amba_driver_unregister(&etm4x_amba_driver); 2535 2536 clear_pm: 2537 etm4_pm_clear(); 2538 return ret; 2539 } 2540 2541 static void __exit etm4x_exit(void) 2542 { 2543 amba_driver_unregister(&etm4x_amba_driver); 2544 platform_driver_unregister(&etm4_platform_driver); 2545 etm4_pm_clear(); 2546 } 2547 2548 module_init(etm4x_init); 2549 module_exit(etm4x_exit); 2550 2551 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>"); 2552 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>"); 2553 MODULE_DESCRIPTION("Arm CoreSight Program Flow Trace v4.x driver"); 2554 MODULE_LICENSE("GPL v2"); 2555