1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
|
/*
Copyright 2021 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sst
import (
"fmt"
stdlog "log"
"os"
grclog "github.com/intel/goresctrl/pkg/log"
"github.com/intel/goresctrl/pkg/utils"
)
// SstPackageInfo contains status of Intel Speed Select Technologies (SST)
// for one CPU package
type SstPackageInfo struct {
// Package related to this SST info
pkg *cpuPackageInfo
// Gereric PP info
PPSupported bool
PPLocked bool
PPVersion int
PPCurrentLevel int
PPMaxLevel int
// Information about the currently active PP level
CPSupported bool
CPEnabled bool
CPPriority CPPriorityType
BFSupported bool
BFEnabled bool
BFCores utils.IDSet
TFSupported bool
TFEnabled bool
ClosInfo [NumClos]SstClosInfo
ClosCPUInfo ClosCPUSet
}
// NumClos is the number of CLOSes suported by SST-CP
const NumClos = 4
// SstClosInfo contains parameters of one CLOS of SST-CP
type SstClosInfo struct {
EPP int
ProportionalPriority int
MinFreq int
MaxFreq int
DesiredFreq int
}
// CPPriorityType denotes the type CLOS priority ordering used in SST-CP
type CPPriorityType int
const (
Proportional CPPriorityType = 0
Ordered CPPriorityType = 1
)
// ClosCPUSet contains mapping from Clos id to a set of CPU ids
type ClosCPUSet map[int]utils.IDSet
const isstDevPath = "/dev/isst_interface"
var sstlog grclog.Logger = grclog.NewLoggerWrapper(stdlog.New(os.Stderr, "[ sst ] ", 0))
// SstSupported returns true if Intel Speed Select Technologies (SST) is supported
// by the system and can be interfaced via the Linux kernel device
func SstSupported() bool {
if _, err := os.Stat(isstDevPath); err != nil {
if !os.IsNotExist(err) {
sstlog.Warnf("failed to access sst device %q: %v", isstDevPath, err)
} else {
sstlog.Debugf("sst device %q does not exist", isstDevPath)
}
return false
}
return true
}
// Check that a list of CPUs belong to a given package
func CheckPackageCpus(info *SstPackageInfo, cpus utils.IDSet) bool {
return info.pkg.hasCpus(cpus)
}
// GetPackageInfo returns information of those packages given as a parameter
// or all if none given.
func GetPackageInfo(pkgs ...int) (map[int]*SstPackageInfo, error) {
var numPkgs int
var pkglist []int
// Get topology information from sysfs
packages, err := getOnlineCpuPackages()
if err != nil {
return nil, fmt.Errorf("failed to determine cpu topology: %w", err)
}
if len(pkgs) == 0 {
for i := range packages {
pkglist = append(pkglist, i)
}
} else {
for _, i := range pkgs {
if _, ok := packages[i]; !ok {
return nil, fmt.Errorf("cpu package %d not present", i)
} else {
pkglist = append(pkglist, i)
}
}
}
numPkgs = len(pkglist)
infomap := make(map[int]*SstPackageInfo, numPkgs)
for _, i := range pkglist {
info, err := getSinglePackageInfo(packages[i])
if err != nil {
return nil, err
}
infomap[i] = &info
}
return infomap, nil
}
// getSinglePackageInfo returns information of the SST configuration of one cpu
// package.
func getSinglePackageInfo(pkg *cpuPackageInfo) (SstPackageInfo, error) {
info := SstPackageInfo{}
cpu := pkg.cpus[0] // We just need to pass one logical cpu from the pkg as an arg
var rsp uint32
var err error
// Read perf-profile feature info
if rsp, err = sendMboxCmd(cpu, CONFIG_TDP, CONFIG_TDP_GET_LEVELS_INFO, 0, 0); err != nil {
return info, fmt.Errorf("failed to read SST PP info: %v", err)
}
info.PPSupported = getBits(rsp, 31, 31) != 0
info.PPLocked = getBits(rsp, 24, 24) != 0
info.PPCurrentLevel = int(getBits(rsp, 16, 23))
info.PPMaxLevel = int(getBits(rsp, 8, 15))
info.PPVersion = int(getBits(rsp, 0, 7))
info.pkg = pkg
// Forget about older hw with partial/convoluted support
if info.PPVersion < 3 {
sstlog.Infof("SST PP version %d (less than 3), giving up...")
return info, nil
}
// Read the status of currently active perf-profile
if !info.PPSupported {
sstlog.Debugf("SST PP feature not supported, only profile level %d is valid", info.PPCurrentLevel)
}
if rsp, err = sendMboxCmd(cpu, CONFIG_TDP, CONFIG_TDP_GET_TDP_CONTROL, 0, uint32(info.PPCurrentLevel)); err != nil {
return info, fmt.Errorf("failed to read SST BF/TF status: %v", err)
}
info.BFSupported = isBitSet(rsp, 1)
info.BFEnabled = isBitSet(rsp, 17)
info.TFSupported = isBitSet(rsp, 0)
info.TFEnabled = isBitSet(rsp, 16)
// Read base-frequency info
if info.BFSupported {
info.BFCores = utils.IDSet{}
punitCoreIDs := make(map[utils.ID]utils.IDSet, len(pkg.cpus))
var maxPunitCore utils.ID
for _, id := range pkg.cpus {
pc, err := punitCPU(id)
if err != nil {
return info, err
}
punitCore := pc >> 1
if _, ok := punitCoreIDs[punitCore]; !ok {
punitCoreIDs[punitCore] = utils.IDSet{}
}
punitCoreIDs[punitCore].Add(id)
if punitCore > maxPunitCore {
maxPunitCore = punitCore
}
}
// Read out core masks in batches of 32 (32 bits per response)
for i := 0; i <= int(maxPunitCore)/32; i++ {
if rsp, err = sendMboxCmd(cpu, CONFIG_TDP, CONFIG_TDP_PBF_GET_CORE_MASK_INFO, 0, uint32(info.PPCurrentLevel+(i<<8))); err != nil {
return info, fmt.Errorf("failed to read SST BF core mask (#%d): %v", i, err)
}
for bit := 0; bit < 32; bit++ {
if isBitSet(rsp, uint32(bit)) {
info.BFCores.Add(punitCoreIDs[utils.ID(i*32+bit)].Members()...)
}
}
}
}
// Read core-power feature info
if rsp, err = sendMboxCmd(cpu, READ_PM_CONFIG, PM_FEATURE, 0, 0); err != nil {
return info, fmt.Errorf("failed to read SST CP info: %v", err)
}
info.CPSupported = isBitSet(rsp, 0)
info.CPEnabled = isBitSet(rsp, 16)
if info.CPSupported {
if rsp, err = sendMboxCmd(cpu, CONFIG_CLOS, CLOS_PM_QOS_CONFIG, 0, 0); err != nil {
return info, fmt.Errorf("failed to read SST CP status: %v", err)
}
info.CPPriority = CPPriorityType(getBits(rsp, 2, 2))
info.ClosCPUInfo = make(map[int]utils.IDSet, NumClos)
for i := 0; i < NumClos; i++ {
if rsp, err = sendClosCmd(cpu, CLOS_PM_CLOS, uint32(i), 0); err != nil {
return info, fmt.Errorf("failed to read SST CLOS #%d info: %v", i, err)
}
info.ClosInfo[i] = SstClosInfo{
EPP: int(getBits(rsp, 0, 3)),
ProportionalPriority: int(getBits(rsp, 4, 7)),
MinFreq: int(getBits(rsp, 8, 15)),
MaxFreq: int(getBits(rsp, 16, 23)),
DesiredFreq: int(getBits(rsp, 24, 31)),
}
}
for _, id := range pkg.cpus {
closId, err := GetCPUClosID(id)
if err != nil {
continue
}
if info.ClosCPUInfo[closId] == nil {
info.ClosCPUInfo[closId] = utils.NewIDSet(id)
} else {
info.ClosCPUInfo[closId].Add(id)
}
}
}
return info, nil
}
func getPunitCoreId(cpu utils.ID) (uint32, error) {
p, err := punitCPU(cpu)
if err != nil {
return 0, err
}
punitCore := uint32(p) >> 1
return punitCore, nil
}
// GetCPUClosID returns the SST-CP CLOS id that a cpu is associated with.
func GetCPUClosID(cpu utils.ID) (int, error) {
punitCore, err := getPunitCoreId(cpu)
if err != nil {
return -1, fmt.Errorf("invalid core id %d for cpu %d: %v", punitCore, cpu, err)
}
rsp, err := sendClosCmd(cpu, CLOS_PQR_ASSOC, punitCore, 0)
if err != nil {
return -1, fmt.Errorf("failed to read CLOS number of cpu %d: %v", cpu, err)
}
return int(getBits(rsp, 16, 17)), nil
}
func getBits(val, i, j uint32) uint32 {
lsb := i
msb := j
if i > j {
lsb = j
msb = i
}
return (val >> lsb) & ((1 << (msb - lsb + 1)) - 1)
}
func isBitSet(val, n uint32) bool {
return val&(1<<n) != 0
}
func setBit(val, n uint32) uint32 {
return val | (1 << n)
}
func clearBit(val, n uint32) uint32 {
return val &^ (1 << n)
}
func setBFStatus(info *SstPackageInfo, status bool) error {
rsp, err := sendMboxCmd(info.pkg.cpus[0], CONFIG_TDP, CONFIG_TDP_GET_TDP_CONTROL, 0, uint32(info.PPCurrentLevel))
if err != nil {
return fmt.Errorf("failed to read SST status: %w", err)
}
req := clearBit(rsp, 17)
if status {
req = setBit(rsp, 17)
}
if _, err = sendMboxCmd(info.pkg.cpus[0], CONFIG_TDP, CONFIG_TDP_SET_TDP_CONTROL, 0, req); err != nil {
return fmt.Errorf("failed to enable SST %s: %w", "BF", err)
}
info.BFEnabled = status
return nil
}
func setScalingMin2CPUInfoMax(info *SstPackageInfo) error {
for _, cpu := range info.pkg.cpus {
err := setCPUScalingMin2CPUInfoMaxFreq(cpu)
if err != nil {
return err
}
}
return nil
}
func enableBF(info *SstPackageInfo) error {
if !info.BFSupported {
return fmt.Errorf("SST BF not supported")
}
if err := setBFStatus(info, true); err != nil {
return err
}
if err := setScalingMin2CPUInfoMax(info); err != nil {
return err
}
return nil
}
// EnableBF enables SST-BF and sets it up properly
func EnableBF(pkgs ...int) error {
if !isHWPEnabled() {
return fmt.Errorf("HWP is not enabled")
}
info, err := GetPackageInfo(pkgs...)
if err != nil {
return err
}
for _, i := range info {
err = enableBF(i)
if err != nil {
// Ignore but log error as there might be packages in the
// user supplied list that do not exists
sstlog.Errorf("sst-bf : %w", err)
}
}
return nil
}
func setScalingMin2CPUInfoMin(info *SstPackageInfo) error {
for _, cpu := range info.pkg.cpus {
err := setCPUScalingMin2CPUInfoMinFreq(cpu)
if err != nil {
return err
}
}
return nil
}
func disableBF(info *SstPackageInfo) error {
if !info.BFSupported {
return fmt.Errorf("SST BF not supported")
}
if err := setBFStatus(info, false); err != nil {
return err
}
if err := setScalingMin2CPUInfoMin(info); err != nil {
return err
}
return nil
}
// DisableBF disables SST-BF and clears things properly
func DisableBF(pkgs ...int) error {
info, err := GetPackageInfo(pkgs...)
if err != nil {
return err
}
for _, i := range info {
err = disableBF(i)
if err != nil {
// Ignore but log error as there might be packages in the
// user supplied list that do not exists
sstlog.Errorf("sst-bf : %w", err)
}
}
return nil
}
func sendClosCmd(cpu utils.ID, subCmd uint16, parameter uint32, reqData uint32) (uint32, error) {
var id, offset uint32
switch subCmd {
case CLOS_PQR_ASSOC:
id = parameter & 0xff // core id
offset = PQR_ASSOC_OFFSET
case CLOS_PM_CLOS:
id = parameter & 0x03 // clos id
offset = PM_CLOS_OFFSET
case CLOS_STATUS:
fallthrough
default:
return 0, nil
}
return sendMMIOCmd(cpu, (id<<2)+offset, reqData, isBitSet(parameter, MBOX_CMD_WRITE_BIT))
}
func saveClos(closInfo *SstClosInfo, cpu utils.ID, clos int) error {
req := closInfo.EPP & 0x0f
req |= (closInfo.ProportionalPriority & 0x0f) << 4
req |= (closInfo.MinFreq & 0xff) << 8
req |= (closInfo.MaxFreq & 0xff) << 16
req |= (closInfo.DesiredFreq & 0xff) << 24
param := setBit(uint32(clos), MBOX_CMD_WRITE_BIT)
if _, err := sendClosCmd(cpu, CLOS_PM_CLOS, param, uint32(req)); err != nil {
return fmt.Errorf("failed to save Clos: %v", err)
}
return nil
}
func associate2Clos(cpu utils.ID, clos int) error {
coreId, err := getPunitCoreId(cpu)
if err != nil {
return fmt.Errorf("invalid core id %d for cpu %d: %v", coreId, cpu, err)
}
req := (clos & 0x03) << 16
param := setBit(coreId, MBOX_CMD_WRITE_BIT)
if _, err := sendClosCmd(cpu, CLOS_PQR_ASSOC, param, uint32(req)); err != nil {
return fmt.Errorf("failed to associate cpu %d to clos %d: %v", cpu, clos, err)
}
return nil
}
func writePMConfig(info *SstPackageInfo, cpu utils.ID, enable bool) (uint32, error) {
var req uint32
if enable {
req = setBit(0, 16)
}
if _, err := sendMboxCmd(cpu, WRITE_PM_CONFIG, PM_FEATURE, 0, req); err != nil {
return 0, fmt.Errorf("failed to set SST-CP status: %v", err)
}
rsp, err := sendMboxCmd(cpu, READ_PM_CONFIG, PM_FEATURE, 0, 0)
if err != nil {
return 0, fmt.Errorf("failed to get SST-CP status: %v", err)
}
return rsp, nil
}
func writeClosPmQosConfig(info *SstPackageInfo, cpu utils.ID, enable bool) error {
var req uint32
param := setBit(0, MBOX_CMD_WRITE_BIT)
if enable {
req = setBit(0, 1)
if info.CPPriority > 0 {
req = setBit(req, 2)
}
}
if _, err := sendMboxCmd(cpu, CONFIG_CLOS, CLOS_PM_QOS_CONFIG, param, req); err != nil {
return fmt.Errorf("failed to set SST-CP status: %v", err)
}
return nil
}
func enableCP(info *SstPackageInfo, cpu utils.ID) (uint32, error) {
if err := writeClosPmQosConfig(info, cpu, true); err != nil {
return 0, fmt.Errorf("Cannot set Clos status: %v", err)
}
return writePMConfig(info, cpu, true)
}
func disableCP(info *SstPackageInfo, cpu utils.ID) (uint32, error) {
if err := writeClosPmQosConfig(info, cpu, false); err != nil {
return 0, fmt.Errorf("Cannot set Clos status: %v", err)
}
return writePMConfig(info, cpu, false)
}
func setDefaultClosParam(info *SstPackageInfo, cpu utils.ID) error {
defaultConfig := &SstClosInfo{MaxFreq: 255}
for clos := 0; clos < 4; clos++ {
if err := saveClos(defaultConfig, cpu, clos); err != nil {
return err
}
}
return nil
}
func assignCPU2Clos(info *SstPackageInfo, clos int) error {
sstlog.Debugf("Setting Clos %d for cpus %v\n", clos, info.ClosCPUInfo[clos].Members())
for _, cpu := range info.ClosCPUInfo[clos].Members() {
if err := associate2Clos(cpu, clos); err != nil {
return fmt.Errorf("failed to associate cpu %d to clos %d: %v", cpu, clos, err)
}
}
return nil
}
// ConfigureCP will allow caller to configure CPUs to various Clos.
func ConfigureCP(info *SstPackageInfo, priority int, cpu2clos *ClosCPUSet) error {
if priority < 0 || priority > 1 {
return fmt.Errorf("Invalid CP priority value %d (valid 0 or 1)", priority)
}
if info.ClosCPUInfo == nil {
info.ClosCPUInfo = make(map[int]utils.IDSet, len(*cpu2clos))
}
for clos, cpus := range *cpu2clos {
info.ClosCPUInfo[clos] = cpus.Clone()
// Remove the CPU from other Clos if found
for i := 0; i < NumClos; i++ {
if i == clos {
continue
}
for id := range cpus {
info.ClosCPUInfo[i].Del(id)
}
}
if err := assignCPU2Clos(info, clos); err != nil {
return err
}
}
info.CPPriority = CPPriorityType(priority)
return nil
}
// ClosSetup stores the user supplied Clos information into punit
func ClosSetup(info *SstPackageInfo, clos int, closInfo *SstClosInfo) error {
if clos < 0 || clos >= NumClos {
return fmt.Errorf("Invalid Clos value (%d)", clos)
}
if closInfo.MinFreq < 0 || closInfo.MinFreq > 255 {
return fmt.Errorf("Invalid min freq (%d)", closInfo.MinFreq)
}
if closInfo.MaxFreq < 0 || closInfo.MaxFreq > 255 {
return fmt.Errorf("Invalid max freq (%d)", closInfo.MaxFreq)
}
if closInfo.MinFreq > closInfo.MaxFreq {
return fmt.Errorf("Min freq %d must be smaller than max freq %d", closInfo.MinFreq, closInfo.MaxFreq)
}
if closInfo.DesiredFreq < 0 || closInfo.DesiredFreq > 255 {
return fmt.Errorf("Invalid value %d for desired freq", closInfo.DesiredFreq)
}
if closInfo.EPP < 0 || closInfo.EPP > 15 {
return fmt.Errorf("Invalid value %d for EPP", closInfo.EPP)
}
if closInfo.ProportionalPriority < 0 || closInfo.ProportionalPriority > 15 {
return fmt.Errorf("Invalid value %d for proportionalPriority", closInfo.ProportionalPriority)
}
info.ClosInfo[clos] = *closInfo
return saveClos(&info.ClosInfo[clos], info.pkg.cpus[0], clos)
}
// ResetCPConfig will bring the system to a known state. This means that all
// CLOS groups are reset to their default values, all package cores are assigned to
// CLOS group 0 and ordered priority mode is enabled.
func ResetCPConfig() error {
infomap, err := GetPackageInfo()
if err != nil {
return err
}
for _, info := range infomap {
for _, cpu := range info.pkg.cpus {
if info.pkg.cpus[0] == cpu {
if err := setDefaultClosParam(info, cpu); err != nil {
return err
}
}
if err := associate2Clos(cpu, 0); err != nil {
return fmt.Errorf("failed to associate cpu %d to clos %d: %w", cpu, 0, err)
}
}
}
return nil
}
// EnableCP enables SST-CP feature
func EnableCP(info *SstPackageInfo) error {
if !info.CPSupported {
return fmt.Errorf("SST CP not supported")
}
if len(info.ClosCPUInfo) == 0 {
return fmt.Errorf("failed to enable CP: Clos to CPU mapping missing")
}
rsp, err := enableCP(info, info.pkg.cpus[0])
if err != nil {
return fmt.Errorf("failed to enable SST-CP: %v", err)
}
info.CPSupported = isBitSet(rsp, 0)
info.CPEnabled = isBitSet(rsp, 16)
return nil
}
// DisableCP disables SST-CP feature
func DisableCP(info *SstPackageInfo) error {
if !info.CPSupported {
return fmt.Errorf("SST CP not supported")
}
if info.TFEnabled {
return fmt.Errorf("SST TF still enabled, disable it first.")
}
rsp, err := disableCP(info, info.pkg.cpus[0])
if err != nil {
return fmt.Errorf("failed to disable SST-CP: %v", err)
}
info.CPSupported = isBitSet(rsp, 0)
info.CPEnabled = isBitSet(rsp, 16)
return nil
}
|