1 /* $NetBSD: cpufunc.c,v 1.40 2026/11/25 15:36:24 skrll Exp $ */ 2 3 /* 4 * Copyright (c) 2017 Ryo Shimizu 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "opt_multiprocessor.h" 30 31 #include <sys/cdefs.h> 32 __KERNEL_RCSID(0, "$NetBSD: cpufunc.c,v 1.40 2026/11/25 15:36:24 skrll Exp $"); 33 34 #include <sys/param.h> 35 #include <sys/types.h> 36 #include <sys/kmem.h> 37 #include <sys/cpu.h> 38 39 #include <uvm/uvm.h> 40 #include <uvm/uvm_page.h> 41 42 #include <arm/cpufunc.h> 43 44 u_int cputype; /* compat arm */ 45 u_int arm_dcache_align; /* compat arm */ 46 u_int arm_dcache_align_mask; /* compat arm */ 47 u_int arm_dcache_maxline; 48 49 u_int aarch64_cache_vindexsize; 50 u_int aarch64_cache_prefer_mask; 51 52 int aarch64_hafdbs_enabled __read_mostly; 53 int aarch64_pan_enabled __read_mostly; 54 int aarch64_pac_enabled __read_mostly; 55 56 static void __noasan 57 extract_cacheunit(int level, bool insn, int cachetype, 58 struct aarch64_cache_info *cacheinfo) 59 { 60 struct aarch64_cache_unit *cunit; 61 uint64_t ccsidr, mmfr2; 62 63 /* select and extract level N data cache */ 64 reg_csselr_el1_write(__SHIFTIN(level, CSSELR_LEVEL) | 65 __SHIFTIN(insn ? 1 : 0, CSSELR_IND)); 66 isb(); 67 68 ccsidr = reg_ccsidr_el1_read(); 69 mmfr2 = reg_id_aa64mmfr2_el1_read(); 70 71 if (insn) 72 cunit = &cacheinfo[level].icache; 73 else 74 cunit = &cacheinfo[level].dcache; 75 76 cunit->cache_type = cachetype; 77 78 switch (__SHIFTOUT(mmfr2, ID_AA64MMFR2_EL1_CCIDX)) { 79 case ID_AA64MMFR2_EL1_CCIDX_32BIT: 80 cunit->cache_line_size = 81 1 << (__SHIFTOUT(ccsidr, CCSIDR_LINESIZE) + 4); 82 cunit->cache_ways = __SHIFTOUT(ccsidr, CCSIDR_ASSOC) + 1; 83 cunit->cache_sets = __SHIFTOUT(ccsidr, CCSIDR_NUMSET) + 1; 84 break; 85 case ID_AA64MMFR2_EL1_CCIDX_64BIT: 86 cunit->cache_line_size = 87 1 << (__SHIFTOUT(ccsidr, CCSIDR64_LINESIZE) + 4); 88 cunit->cache_ways = __SHIFTOUT(ccsidr, CCSIDR64_ASSOC) + 1; 89 cunit->cache_sets = __SHIFTOUT(ccsidr, CCSIDR64_NUMSET) + 1; 90 break; 91 } 92 93 /* calc waysize and whole size */ 94 cunit->cache_way_size = cunit->cache_line_size * cunit->cache_sets; 95 cunit->cache_size = cunit->cache_way_size * cunit->cache_ways; 96 } 97 98 /* Must be called on each processor */ 99 void __noasan 100 aarch64_getcacheinfo(struct cpu_info *ci) 101 { 102 struct aarch64_cache_info * const cinfo = ci->ci_cacheinfo; 103 uint32_t clidr, ctr; 104 int level, cachetype; 105 106 /* 107 * CTR - Cache Type Register 108 */ 109 ctr = reg_ctr_el0_read(); 110 switch (__SHIFTOUT(ctr, CTR_EL0_L1IP_MASK)) { 111 case CTR_EL0_L1IP_VPIPT: 112 cachetype = CACHE_TYPE_VPIPT; 113 break; 114 case CTR_EL0_L1IP_AIVIVT: 115 cachetype = CACHE_TYPE_VIVT; 116 break; 117 case CTR_EL0_L1IP_VIPT: 118 cachetype = CACHE_TYPE_VIPT; 119 break; 120 case CTR_EL0_L1IP_PIPT: 121 cachetype = CACHE_TYPE_PIPT; 122 break; 123 } 124 125 /* 126 * CLIDR - Cache Level ID Register 127 * CSSELR - Cache Size Selection Register 128 * CCSIDR - CurrentCache Size ID Register (selected by CSSELR) 129 */ 130 131 /* L1, L2, L3, ..., L8 cache */ 132 for (level = 0, clidr = reg_clidr_el1_read(); 133 level < MAX_CACHE_LEVEL; level++, clidr >>= 3) { 134 135 int cacheable; 136 137 switch (clidr & 7) { 138 case CLIDR_TYPE_NOCACHE: 139 cacheable = CACHE_CACHEABLE_NONE; 140 break; 141 case CLIDR_TYPE_ICACHE: 142 cacheable = CACHE_CACHEABLE_ICACHE; 143 extract_cacheunit(level, true, cachetype, cinfo); 144 break; 145 case CLIDR_TYPE_DCACHE: 146 cacheable = CACHE_CACHEABLE_DCACHE; 147 extract_cacheunit(level, false, CACHE_TYPE_PIPT, cinfo); 148 break; 149 case CLIDR_TYPE_IDCACHE: 150 cacheable = CACHE_CACHEABLE_IDCACHE; 151 extract_cacheunit(level, true, cachetype, cinfo); 152 extract_cacheunit(level, false, CACHE_TYPE_PIPT, cinfo); 153 break; 154 case CLIDR_TYPE_UNIFIEDCACHE: 155 cacheable = CACHE_CACHEABLE_UNIFIED; 156 extract_cacheunit(level, false, CACHE_TYPE_PIPT, cinfo); 157 break; 158 default: 159 cacheable = CACHE_CACHEABLE_NONE; 160 break; 161 } 162 163 cinfo[level].cacheable = cacheable; 164 if (cacheable == CACHE_CACHEABLE_NONE) { 165 /* no more level */ 166 break; 167 } 168 169 /* 170 * L1 insn cachetype is CTR_EL0:L1IP, 171 * all other cachetype is PIPT. 172 */ 173 cachetype = CACHE_TYPE_PIPT; 174 } 175 } 176 177 void 178 aarch64_parsecacheinfo(struct cpu_info *ci) 179 { 180 struct aarch64_cache_info * const cinfo = ci->ci_cacheinfo; 181 struct aarch64_sysctl_cpu_id *id = &ci->ci_id; 182 const uint32_t ctr = id->ac_ctr; 183 u_int vindexsize; 184 185 /* remember maximum alignment */ 186 if (arm_dcache_maxline < __SHIFTOUT(ctr, CTR_EL0_DMIN_LINE)) { 187 arm_dcache_maxline = __SHIFTOUT(ctr, CTR_EL0_DMIN_LINE); 188 arm_dcache_align = sizeof(int) << arm_dcache_maxline; 189 arm_dcache_align_mask = arm_dcache_align - 1; 190 } 191 192 #ifdef MULTIPROCESSOR 193 if (coherency_unit < arm_dcache_align) 194 panic("coherency_unit %ld < %d; increase COHERENCY_UNIT", 195 coherency_unit, arm_dcache_align); 196 #endif 197 198 /* calculate L1 icache virtual index size */ 199 if ((cinfo[0].icache.cache_type == CACHE_TYPE_VIVT || 200 cinfo[0].icache.cache_type == CACHE_TYPE_VIPT) && 201 (cinfo[0].cacheable == CACHE_CACHEABLE_ICACHE || 202 cinfo[0].cacheable == CACHE_CACHEABLE_IDCACHE)) { 203 204 vindexsize = 205 cinfo[0].icache.cache_size / 206 cinfo[0].icache.cache_ways; 207 208 KASSERT(vindexsize != 0); 209 } else { 210 vindexsize = 0; 211 } 212 213 if (vindexsize > aarch64_cache_vindexsize) { 214 aarch64_cache_vindexsize = vindexsize; 215 aarch64_cache_prefer_mask = vindexsize - 1; 216 217 if (uvm.page_init_done) 218 uvm_page_recolor(vindexsize / PAGE_SIZE); 219 } 220 } 221 222 static int 223 prt_cache(device_t self, struct aarch64_cache_info *cinfo, int level) 224 { 225 struct aarch64_cache_unit *cunit; 226 int i; 227 const char *cacheable, *cachetype; 228 229 if (cinfo[level].cacheable == CACHE_CACHEABLE_NONE) 230 return -1; 231 232 for (i = 0; i < 2; i++) { 233 switch (cinfo[level].cacheable) { 234 case CACHE_CACHEABLE_ICACHE: 235 cunit = &cinfo[level].icache; 236 cacheable = "Instruction"; 237 break; 238 case CACHE_CACHEABLE_DCACHE: 239 cunit = &cinfo[level].dcache; 240 cacheable = "Data"; 241 break; 242 case CACHE_CACHEABLE_IDCACHE: 243 if (i == 0) { 244 cunit = &cinfo[level].icache; 245 cacheable = "Instruction"; 246 } else { 247 cunit = &cinfo[level].dcache; 248 cacheable = "Data"; 249 } 250 break; 251 case CACHE_CACHEABLE_UNIFIED: 252 cunit = &cinfo[level].dcache; 253 cacheable = "Unified"; 254 break; 255 default: 256 cunit = &cinfo[level].dcache; 257 cacheable = "*UNK*"; 258 break; 259 } 260 261 switch (cunit->cache_type) { 262 case CACHE_TYPE_VPIPT: 263 cachetype = "VPIPT"; 264 break; 265 case CACHE_TYPE_VIVT: 266 cachetype = "VIVT"; 267 break; 268 case CACHE_TYPE_VIPT: 269 cachetype = "VIPT"; 270 break; 271 case CACHE_TYPE_PIPT: 272 cachetype = "PIPT"; 273 break; 274 default: 275 cachetype = "*UNK*"; 276 break; 277 } 278 279 aprint_verbose_dev(self, 280 "L%d %uKB/%uB %u-way (%u set) %s %s cache\n", 281 level + 1, 282 cunit->cache_size / 1024, 283 cunit->cache_line_size, 284 cunit->cache_ways, 285 cunit->cache_sets, 286 cachetype, cacheable); 287 288 if (cinfo[level].cacheable != CACHE_CACHEABLE_IDCACHE) 289 break; 290 } 291 292 return 0; 293 } 294 295 void 296 aarch64_printcacheinfo(device_t dev, struct cpu_info *ci) 297 { 298 struct aarch64_cache_info * const cinfo = ci->ci_cacheinfo; 299 int level; 300 301 for (level = 0; level < MAX_CACHE_LEVEL; level++) 302 if (prt_cache(dev, cinfo, level) < 0) 303 break; 304 } 305 306 static inline void 307 ln_dcache_wb_all(int level, struct aarch64_cache_unit *cunit) 308 { 309 unsigned int setshift = ffs(cunit->cache_line_size) - 1; 310 unsigned int wayshift = 32 - (ffs(cunit->cache_ways) - 1); 311 312 for (unsigned int way = 0; way < cunit->cache_ways; way++) { 313 for (unsigned int set = 0; set < cunit->cache_sets; set++) { 314 uint64_t x = (way << wayshift) | (set << setshift) | 315 (level << 1); 316 __asm __volatile ("dc csw, %0" :: "r"(x)); 317 } 318 } 319 /* Complete all set/way operations for this cache level. */ 320 dsb(sy); 321 } 322 323 static inline void 324 ln_dcache_wbinv_all(int level, struct aarch64_cache_unit *cunit) 325 { 326 unsigned int setshift = ffs(cunit->cache_line_size) - 1; 327 unsigned int wayshift = 32 - (ffs(cunit->cache_ways) - 1); 328 329 for (unsigned int way = 0; way < cunit->cache_ways; way++) { 330 for (unsigned int set = 0; set < cunit->cache_sets; set++) { 331 uint64_t x = (way << wayshift) | (set << setshift) | 332 (level << 1); 333 __asm __volatile ("dc cisw, %0" :: "r"(x)); 334 } 335 } 336 /* Complete all set/way operations for this cache level. */ 337 dsb(sy); 338 } 339 340 static inline void 341 ln_dcache_inv_all(int level, struct aarch64_cache_unit *cunit) 342 { 343 unsigned int setshift = ffs(cunit->cache_line_size) - 1; 344 unsigned int wayshift = 32 - (ffs(cunit->cache_ways) - 1); 345 346 for (unsigned int way = 0; way < cunit->cache_ways; way++) { 347 for (unsigned int set = 0; set < cunit->cache_sets; set++) { 348 uint64_t x = (way << wayshift) | (set << setshift) | 349 (level << 1); 350 __asm __volatile ("dc isw, %0" :: "r"(x)); 351 } 352 } 353 /* Complete all set/way operations for this cache level. */ 354 dsb(sy); 355 } 356 357 void 358 aarch64_dcache_wbinv_all(void) 359 { 360 KASSERT(kpreempt_disabled()); 361 362 struct cpu_info * const ci = curcpu(); 363 struct aarch64_cache_info * const cinfo = ci->ci_cacheinfo; 364 365 for (unsigned int level = 0; level < MAX_CACHE_LEVEL; level++) { 366 if (cinfo[level].cacheable == CACHE_CACHEABLE_NONE) 367 break; 368 369 dsb(ish); 370 ln_dcache_wbinv_all(level, &cinfo[level].dcache); 371 } 372 dsb(ish); 373 } 374 375 void 376 aarch64_dcache_inv_all(void) 377 { 378 KASSERT(kpreempt_disabled()); 379 380 struct cpu_info * const ci = curcpu(); 381 struct aarch64_cache_info * const cinfo = ci->ci_cacheinfo; 382 383 for (unsigned int level = 0; level < MAX_CACHE_LEVEL; level++) { 384 if (cinfo[level].cacheable == CACHE_CACHEABLE_NONE) 385 break; 386 387 dsb(ish); 388 ln_dcache_inv_all(level, &cinfo[level].dcache); 389 } 390 dsb(ish); 391 } 392 393 void 394 aarch64_dcache_wb_all(void) 395 { 396 KASSERT(kpreempt_disabled()); 397 398 struct cpu_info * const ci = curcpu(); 399 struct aarch64_cache_info * const cinfo = ci->ci_cacheinfo; 400 401 for (unsigned int level = 0; level < MAX_CACHE_LEVEL; level++) { 402 if (cinfo[level].cacheable == CACHE_CACHEABLE_NONE) 403 break; 404 405 dsb(ish); 406 ln_dcache_wb_all(level, &cinfo[level].dcache); 407 } 408 dsb(ish); 409 } 410 411 bool 412 aarch64_earlydevice_va_p(void) 413 { 414 /* This function may be called before enabling MMU, or mapping KVA */ 415 if ((reg_sctlr_el1_read() & SCTLR_M) == 0) 416 return false; 417 418 /* device mapping will be available after pmap_devmap_bootstrap() */ 419 if (!pmap_devmap_bootstrapped_p()) 420 return false; 421 422 return true; 423 } 424 425 int 426 set_cpufuncs(void) 427 { 428 // This is only called from the BP 429 430 return aarch64_setcpufuncs(&cpu_info_store[0]); 431 } 432 433 int 434 aarch64_setcpufuncs(struct cpu_info *ci) 435 { 436 const uint64_t ctr = reg_ctr_el0_read(); 437 const uint64_t clidr = reg_clidr_el1_read(); 438 439 /* install default functions */ 440 ci->ci_cpufuncs.cf_set_ttbr0 = aarch64_set_ttbr0; 441 ci->ci_cpufuncs.cf_icache_sync_range = aarch64_icache_sync_range; 442 443 /* 444 * install core/cluster specific functions 445 */ 446 447 /* Icache sync op */ 448 if (__SHIFTOUT(ctr, CTR_EL0_DIC) == 1) { 449 /* Icache invalidation to the PoU is not required */ 450 ci->ci_cpufuncs.cf_icache_sync_range = 451 aarch64_icache_barrier_range; 452 } else if (__SHIFTOUT(ctr, CTR_EL0_IDC) == 1 || 453 __SHIFTOUT(clidr, CLIDR_LOC) == 0 || 454 (__SHIFTOUT(clidr, CLIDR_LOUIS) == 0 && __SHIFTOUT(clidr, CLIDR_LOUU) == 0)) { 455 /* Dcache clean to the PoU is not required for Icache */ 456 ci->ci_cpufuncs.cf_icache_sync_range = 457 aarch64_icache_inv_range; 458 } 459 460 #ifdef CPU_THUNDERX 461 const uint32_t midr = reg_midr_el1_read(); 462 463 /* Cavium erratum 27456 */ 464 if ((midr == CPU_ID_THUNDERXP1d0) || 465 (midr == CPU_ID_THUNDERXP1d1) || 466 (midr == CPU_ID_THUNDERXP2d1) || 467 (midr == CPU_ID_THUNDERX81XXRX)) { 468 ci->ci_cpufuncs.cf_set_ttbr0 = aarch64_set_ttbr0_thunderx; 469 } 470 #endif 471 472 return 0; 473 } 474 475 void 476 aarch64_hafdbs_init(int primary) 477 { 478 #ifdef ARMV81_HAFDBS 479 uint64_t tcr; 480 int hafdbs; 481 482 hafdbs = __SHIFTOUT(reg_id_aa64mmfr1_el1_read(), 483 ID_AA64MMFR1_EL1_HAFDBS); 484 485 /* 486 * hafdbs 487 * 0:HAFDBS_NONE - no support for any hardware flags 488 * 1:HAFDBS_A - only hardware access flag supported 489 * 2:HAFDBS_AD - hardware access and modified flags supported. 490 */ 491 492 if (primary) { 493 /* CPU0 does the detection. */ 494 switch (hafdbs) { 495 case ID_AA64MMFR1_EL1_HAFDBS_NONE: 496 default: 497 aarch64_hafdbs_enabled = 0; 498 break; 499 case ID_AA64MMFR1_EL1_HAFDBS_A: 500 case ID_AA64MMFR1_EL1_HAFDBS_AD: 501 aarch64_hafdbs_enabled = hafdbs; 502 break; 503 } 504 } else { 505 /* 506 * The support status of HAFDBS on the primary CPU is different 507 * from that of the application processor. 508 * 509 * XXX: 510 * The correct way to do this is to disable it on all cores, 511 * or call pmap_fault_fixup() only on the unsupported cores, 512 * but for now, do panic(). 513 */ 514 if (aarch64_hafdbs_enabled != hafdbs) 515 panic("HAFDBS is supported (%d) on primary cpu, " 516 "but isn't equal (%d) on secondary cpu", 517 aarch64_hafdbs_enabled, hafdbs); 518 } 519 520 /* enable Hardware updates to Access flag and Dirty state */ 521 tcr = reg_tcr_el1_read(); 522 switch (hafdbs) { 523 case ID_AA64MMFR1_EL1_HAFDBS_NONE: 524 default: 525 break; 526 case ID_AA64MMFR1_EL1_HAFDBS_A: 527 /* enable only access */ 528 reg_tcr_el1_write(tcr | TCR_HA); 529 isb(); 530 break; 531 case ID_AA64MMFR1_EL1_HAFDBS_AD: 532 /* enable both access and dirty */ 533 reg_tcr_el1_write(tcr | TCR_HD | TCR_HA); 534 isb(); 535 break; 536 } 537 #endif 538 } 539 540 void 541 aarch64_pan_init(int primary) 542 { 543 #ifdef ARMV81_PAN 544 uint64_t reg, sctlr; 545 546 /* CPU0 does the detection. */ 547 if (primary) { 548 reg = reg_id_aa64mmfr1_el1_read(); 549 if (__SHIFTOUT(reg, ID_AA64MMFR1_EL1_PAN) != 550 ID_AA64MMFR1_EL1_PAN_NONE) 551 aarch64_pan_enabled = 1; 552 } 553 554 if (!aarch64_pan_enabled) 555 return; 556 557 /* 558 * On an exception to EL1, have the CPU set the PAN bit automatically. 559 * This ensures PAN is enabled each time the kernel is entered. 560 */ 561 sctlr = reg_sctlr_el1_read(); 562 sctlr &= ~SCTLR_SPAN; 563 reg_sctlr_el1_write(sctlr); 564 565 /* Set the PAN bit right now. */ 566 reg_pan_write(1); 567 #endif 568 } 569 570 /* 571 * In order to avoid inconsistencies with pointer authentication 572 * in this function itself, the caller must enable PAC according 573 * to the return value. 574 */ 575 int 576 aarch64_pac_init(int primary) 577 { 578 #ifdef ARMV83_PAC 579 uint64_t reg; 580 581 /* CPU0 does the detection. */ 582 if (primary) { 583 reg = reg_id_aa64isar1_el1_read(); 584 if (__SHIFTOUT(reg, ID_AA64ISAR1_EL1_APA) != 585 ID_AA64ISAR1_EL1_APA_NONE) 586 aarch64_pac_enabled = 1; 587 if (__SHIFTOUT(reg, ID_AA64ISAR1_EL1_API) != 588 ID_AA64ISAR1_EL1_API_NONE) 589 aarch64_pac_enabled = 1; 590 if (__SHIFTOUT(reg, ID_AA64ISAR1_EL1_GPA) != 591 ID_AA64ISAR1_EL1_GPA_NONE) 592 aarch64_pac_enabled = 1; 593 if (__SHIFTOUT(reg, ID_AA64ISAR1_EL1_GPI) != 594 ID_AA64ISAR1_EL1_GPI_NONE) 595 aarch64_pac_enabled = 1; 596 } 597 598 if (!aarch64_pac_enabled) 599 return -1; 600 601 /* Set the key. Curlwp here is the CPU's idlelwp. */ 602 reg_APIAKeyLo_EL1_write(curlwp->l_md.md_ia_kern[0]); 603 reg_APIAKeyHi_EL1_write(curlwp->l_md.md_ia_kern[1]); 604 605 return 0; 606 #else 607 return -1; 608 #endif 609 } 610