├── .github └── workflows │ ├── release.yaml │ └── test.yaml ├── .gitignore ├── .releaserc.yaml ├── Cargo.lock ├── Cargo.toml ├── Dockerfile ├── LICENSE ├── Makefile ├── README.md ├── bindings └── dbus-xml │ ├── org-shadowblip-cpu-core.xml │ ├── org-shadowblip-cpu.xml │ ├── org-shadowblip-gpu-card-connector.xml │ ├── org-shadowblip-gpu-card.xml │ └── org-shadowblip-gpu.xml ├── docs ├── cpu-core.md ├── cpu.md ├── dbus2markdown.xsl ├── gpu-card-connector.md ├── gpu-card.md └── gpu.md ├── icon.svg ├── rootfs └── usr │ ├── lib │ └── systemd │ │ └── system │ │ └── powerstation.service │ └── share │ ├── dbus-1 │ ├── system-services │ │ └── org.shadowblip.PowerStation.service │ └── system.d │ │ └── org.shadowblip.PowerStation.conf │ └── powerstation │ └── platform │ ├── amd_apu_database.toml │ ├── dmi_overrides_apu_database.toml │ └── intel_apu_database.toml └── src ├── constants.rs ├── main.rs └── performance ├── cpu ├── core.rs ├── cpu_features.rs └── mod.rs ├── gpu ├── acpi │ ├── firmware.rs │ └── mod.rs ├── amd │ ├── amdgpu.rs │ ├── mod.rs │ ├── ryzenadj.rs │ └── tdp.rs ├── asus │ ├── asus_wmi.rs │ └── mod.rs ├── connector.rs ├── dbus │ ├── devices.rs │ ├── gpu.rs │ ├── mod.rs │ └── tdp.rs ├── intel │ ├── intelgpu.rs │ ├── mod.rs │ └── tdp.rs ├── interface.rs ├── mod.rs ├── platform │ ├── hardware.rs │ ├── mod.rs │ └── model_config.rs └── tdp.rs └── mod.rs /.github/workflows/release.yaml: -------------------------------------------------------------------------------- 1 | # Build and publish a release of PowerStation using semantic-release whenever 2 | # changes are merged into main. 3 | name: "🎉 Release" 4 | 5 | on: 6 | push: 7 | branches: 8 | - main 9 | - v0.x 10 | - v1.x 11 | paths-ignore: 12 | - README.md 13 | - "docs/**" 14 | 15 | # Jobs to run 16 | jobs: 17 | release: 18 | name: Publish 19 | runs-on: ubuntu-latest 20 | 21 | steps: 22 | - name: Checkout 23 | uses: actions/checkout@v3 24 | 25 | - name: Setup Node.js 26 | uses: actions/setup-node@v1 27 | with: 28 | node-version: "20" 29 | 30 | - name: Install Dependencies 31 | run: npm install @semantic-release/exec @google/semantic-release-replace-plugin @semantic-release/git 32 | 33 | - name: Release 34 | env: 35 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 36 | run: make sem-release 37 | -------------------------------------------------------------------------------- /.github/workflows/test.yaml: -------------------------------------------------------------------------------- 1 | name: 🪲 Test 2 | 3 | on: [push] 4 | 5 | jobs: 6 | run-tests: 7 | name: Run tests 8 | runs-on: ubuntu-latest 9 | 10 | steps: 11 | - uses: actions/checkout@v3 12 | - run: | 13 | make in-docker TARGET='test' 14 | 15 | build-x86_64: 16 | name: Run x86_64 build 17 | runs-on: ubuntu-latest 18 | 19 | steps: 20 | - uses: actions/checkout@v3 21 | - run: | 22 | make in-docker TARGET='dist' BUILD_TYPE='debug' 23 | 24 | build-aarch64: 25 | name: Run aarch64 build 26 | runs-on: ubuntu-latest 27 | 28 | steps: 29 | - uses: actions/checkout@v3 30 | - run: | 31 | make in-docker TARGET='dist' BUILD_TYPE='debug' TARGET_ARCH='aarch64-unknown-linux-gnu' 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | .cache 3 | dist 4 | -------------------------------------------------------------------------------- /.releaserc.yaml: -------------------------------------------------------------------------------- 1 | # Semantic Release Configuration 2 | # https://semantic-release.gitbook.io/semantic-release/usage/configuration 3 | 4 | # Any merges into branches that match these patterns will trigger a release. 5 | branches: 6 | - name: main 7 | #- name: 'v+([0-9])?(.{+([0-9]),x}).x' 8 | 9 | # These plugins will run when a release is triggered. They will analyze commit 10 | # messages to determine what kind of release this is and publish a new release. 11 | plugins: 12 | # Analyze commit messages to determine next version 13 | - "@semantic-release/commit-analyzer" 14 | 15 | # Generate release notes 16 | - "@semantic-release/release-notes-generator" 17 | 18 | # Replace version strings in the project. The 'git' plugin is needed to 19 | # commit the version strings to the repository. 20 | - - "@google/semantic-release-replace-plugin" 21 | - replacements: 22 | - files: 23 | - Cargo.toml 24 | from: '^version = .*"$' 25 | to: 'version = "${nextRelease.version}"' 26 | #results: 27 | # - file: Cargo.toml 28 | # hasChanged: true 29 | # numMatches: 1 30 | # numReplacements: 1 31 | #countMatches: true 32 | 33 | # Execute commands to build the project 34 | - - "@semantic-release/exec" 35 | - shell: true 36 | prepareCmd: | 37 | make in-docker TARGET='dist' 38 | make in-docker TARGET='dist' TARGET_ARCH="aarch64-unknown-linux-gnu" 39 | 40 | # Commit the following changes to git after other plugins have run 41 | - - "@semantic-release/git" 42 | - assets: 43 | - Cargo.toml 44 | - Cargo.lock 45 | 46 | # Publish artifacts as a GitHub release 47 | - - "@semantic-release/github" 48 | - assets: 49 | - path: dist/powerstation-*.rpm 50 | - path: dist/powerstation-*.rpm.sha256.txt 51 | - path: dist/powerstation-*.tar.gz 52 | - path: dist/powerstation-*.tar.gz.sha256.txt 53 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "powerstation" 3 | version = "0.5.0" 4 | edition = "2021" 5 | license = "GPL-3.0-or-later" 6 | description = "Daemon for controlling TDP and performance over DBus" 7 | 8 | [package.metadata.generate-rpm] 9 | assets = [ 10 | { source = "target/release/powerstation", dest = "/usr/bin/powerstation", mode = "755" }, 11 | { source = "rootfs/usr/share/dbus-1/system.d/org.shadowblip.PowerStation.conf", dest = "/usr/share/dbus-1/system.d/org.shadowblip.PowerStation.conf", mode = "644" }, 12 | { source = "rootfs/usr/lib/systemd/system/powerstation.service", dest = "/usr/lib/systemd/system/powerstation.service", mode = "644" }, 13 | { source = "rootfs/usr/share/powerstation/platform/*.toml", dest = "/usr/share/powerstation/platform/", mode = "644" }, 14 | ] 15 | 16 | [package.metadata.generate-rpm.requires] 17 | dbus = "*" 18 | pciutils = "*" 19 | 20 | [dependencies] 21 | log = "0.4.20" 22 | simple_logger = "4.2.0" 23 | tokio = { version = "*", features = ["full"] } 24 | zbus = { version = "3.14.1", default-features = false, features = ["tokio"] } 25 | zbus_macros = "3.14.1" 26 | rog_platform = { git = "https://gitlab.com/asus-linux/asusctl.git", default-features = true } 27 | xdg = "2.5.2" 28 | toml = "0.7.8" 29 | serde = { version = "1.0", features = ["derive"] } 30 | 31 | [target.'cfg(target_arch = "x86_64")'.dependencies] 32 | libryzenadj = { git = "https://gitlab.com/shadowapex/libryzenadj-rs.git" } 33 | 34 | [profile.release] 35 | debug = false 36 | strip = true 37 | lto = true 38 | codegen-units = 1 39 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM rust:1.86 2 | 3 | RUN dpkg --add-architecture arm64 4 | RUN apt-get update && apt-get install -y \ 5 | cmake \ 6 | build-essential \ 7 | libpci-dev \ 8 | libclang-15-dev 9 | 10 | RUN apt-get install -y \ 11 | g++-aarch64-linux-gnu \ 12 | libc6-dev-arm64-cross \ 13 | libpci-dev:arm64 14 | 15 | RUN rustup target add aarch64-unknown-linux-gnu 16 | 17 | ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc 18 | ENV CC_aarch64_unknown_linux_gnu=aarch64-linux-gnu-gcc 19 | ENV CXX_aarch64_unknown_linux_gnu=aarch64-linux-gnu-g++ 20 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | NAME := $(shell grep 'name =' Cargo.toml | head -n 1 | cut -d'"' -f2) 2 | VERSION := $(shell grep '^version =' Cargo.toml | cut -d'"' -f2) 3 | TARGET_ARCH ?= $(shell rustc -vV | sed -n 's/host: //p') 4 | ARCH := $(shell echo "$(TARGET_ARCH)" | cut -d'-' -f1) 5 | ALL_RS := $(shell find src -name '*.rs') 6 | PREFIX ?= /usr 7 | CACHE_DIR := .cache 8 | 9 | # Docker image variables 10 | IMAGE_NAME ?= rust-cmake 11 | IMAGE_TAG ?= latest 12 | 13 | ##@ General 14 | 15 | # The help target prints out all targets with their descriptions organized 16 | # beneath their categories. The categories are represented by '##@' and the 17 | # target descriptions by '##'. The awk commands is responsible for reading the 18 | # entire set of makefiles included in this invocation, looking for lines of the 19 | # file as xyz: ## something, and then pretty-format the target and help. Then, 20 | # if there's a line with ##@ something, that gets pretty-printed as a category. 21 | # More info on the usage of ANSI control characters for terminal formatting: 22 | # https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters 23 | # More info on the awk command: 24 | # http://linuxcommand.org/lc3_adv_awk.php 25 | 26 | .PHONY: help 27 | help: ## Display this help. 28 | @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) 29 | 30 | .PHONY: install 31 | install: build ## Install PowerStation to the given prefix (default: PREFIX=/usr) 32 | install -D -m 755 target/$(TARGET_ARCH)/release/powerstation \ 33 | $(PREFIX)/bin/powerstation 34 | install -D -m 644 rootfs/usr/share/dbus-1/system.d/org.shadowblip.PowerStation.conf \ 35 | $(PREFIX)/share/dbus-1/system.d/org.shadowblip.PowerStation.conf 36 | install -D -m 644 rootfs/usr/lib/systemd/system/powerstation.service \ 37 | $(PREFIX)/lib/systemd/system/powerstation.service 38 | install -D -m644 -t $(PREFIX)/share/powerstation/platform/ \ 39 | rootfs/usr/share/powerstation/platform/* 40 | ifndef NO_RELOAD 41 | systemctl reload dbus 42 | endif 43 | 44 | .PHONY: uninstall 45 | uninstall: ## Uninstall PowerStation 46 | rm $(PREFIX)/bin/powerstation 47 | rm $(PREFIX)/share/dbus-1/system.d/org.shadowblip.PowerStation.conf 48 | rm $(PREFIX)/lib/systemd/system/powerstation.service 49 | 50 | ##@ Development 51 | 52 | .PHONY: debug 53 | debug: target/$(TARGET_ARCH)/debug/powerstation ## Build debug build 54 | target/$(TARGET_ARCH)/debug/powerstation: $(ALL_RS) Cargo.lock 55 | cargo build --target $(TARGET_ARCH) 56 | 57 | .PHONY: build 58 | build: target/$(TARGET_ARCH)/release/powerstation ## Build release build 59 | target/$(TARGET_ARCH)/release/powerstation: $(ALL_RS) Cargo.lock 60 | cargo build --release --target $(TARGET_ARCH) 61 | 62 | .PHONY: all 63 | all: build debug ## Build release and debug builds 64 | 65 | .PHONY: run 66 | run: debug ## Build and run 67 | sudo ./target/$(TARGET_ARCH)/debug/powerstation 68 | 69 | .PHONY: test 70 | test: debug ## Build and run all tests 71 | cargo test -- --show-output 72 | 73 | .PHONY: clean 74 | clean: ## Remove build artifacts 75 | rm -rf target 76 | rm -rf .cache 77 | rm -rf dist 78 | 79 | .PHONY: format 80 | format: ## Run rustfmt on all source files 81 | rustfmt --edition 2021 $(ALL_RS) 82 | 83 | .PHONY: setup 84 | setup: /usr/share/dbus-1/system.d/org.shadowblip.PowerStation.conf ## Install dbus policies 85 | /usr/share/dbus-1/system.d/org.shadowblip.P$(CACHE_DIR)/owerStation.conf: 86 | sudo ln $(PWD)/rootfs/usr/share/dbus-1/system.d/org.shadowblip.PowerStation.conf \ 87 | /usr/share/dbus-1/system.d/org.shadowblip.PowerStation.conf 88 | sudo systemctl reload dbus 89 | 90 | ##@ Distribution 91 | 92 | .PHONY: dist 93 | dist: dist/$(NAME)-$(ARCH).tar.gz dist/$(NAME)-$(VERSION)-1.$(ARCH).rpm ## Create all redistributable versions of the project 94 | 95 | .PHONY: dist-archive 96 | dist-archive: dist/powerstation-$(ARCH).tar.gz ## Build a redistributable archive of the project 97 | dist/powerstation-$(ARCH).tar.gz: build 98 | rm -rf $(CACHE_DIR)/powerstation 99 | mkdir -p $(CACHE_DIR)/powerstation 100 | $(MAKE) install PREFIX=$(CACHE_DIR)/powerstation/usr NO_RELOAD=true 101 | mkdir -p dist 102 | tar cvfz $@ -C $(CACHE_DIR) powerstation 103 | cd dist && sha256sum powerstation-$(ARCH).tar.gz > powerstation-$(ARCH).tar.gz.sha256.txt 104 | 105 | .PHONY: dist-rpm 106 | dist-rpm: dist/$(NAME)-$(VERSION)-1.$(ARCH).rpm ## Build a redistributable RPM package 107 | dist/$(NAME)-$(VERSION)-1.$(ARCH).rpm: target/$(TARGET_ARCH)/release/$(NAME) 108 | mkdir -p dist 109 | cargo install cargo-generate-rpm 110 | cargo generate-rpm --target $(TARGET_ARCH) 111 | cp ./target/$(TARGET_ARCH)/generate-rpm/$(NAME)-$(VERSION)-1.$(ARCH).rpm dist 112 | cd dist && sha256sum $(NAME)-$(VERSION)-1.$(ARCH).rpm > $(NAME)-$(VERSION)-1.$(ARCH).rpm.sha256.txt 113 | 114 | INTROSPECT_CARD ?= Card2 115 | INTROSPECT_CONNECTOR ?= eDP/1 116 | .PHONY: introspect 117 | introspect: ## Generate DBus XML 118 | echo "Generating DBus XML spec..." 119 | mkdir -p bindings/dbus-xml 120 | busctl introspect org.shadowblip.PowerStation \ 121 | /org/shadowblip/Performance/CPU --xml-interface > bindings/dbus-xml/org-shadowblip-cpu.xml 122 | xmlstarlet ed -L -d '//node[@name]' bindings/dbus-xml/org-shadowblip-cpu.xml 123 | busctl introspect org.shadowblip.PowerStation \ 124 | /org/shadowblip/Performance/CPU/Core0 --xml-interface > bindings/dbus-xml/org-shadowblip-cpu-core.xml 125 | busctl introspect org.shadowblip.PowerStation \ 126 | /org/shadowblip/Performance/GPU --xml-interface > bindings/dbus-xml/org-shadowblip-gpu.xml 127 | xmlstarlet ed -L -d '//node[@name]' bindings/dbus-xml/org-shadowblip-gpu.xml 128 | busctl introspect org.shadowblip.PowerStation \ 129 | /org/shadowblip/Performance/GPU/$(INTROSPECT_CARD) --xml-interface > bindings/dbus-xml/org-shadowblip-gpu-card.xml 130 | xmlstarlet ed -L -d '//node[@name]' bindings/dbus-xml/org-shadowblip-gpu-card.xml 131 | busctl introspect org.shadowblip.PowerStation \ 132 | /org/shadowblip/Performance/GPU/Card2/$(INTROSPECT_CONNECTOR) --xml-interface > bindings/dbus-xml/org-shadowblip-gpu-card-connector.xml 133 | 134 | XSL_TEMPLATE := ./docs/dbus2markdown.xsl 135 | .PHONY: docs 136 | docs: ## Generate markdown docs for DBus interfaces 137 | mkdir -p docs 138 | xsltproc --novalid -o docs/cpu.md $(XSL_TEMPLATE) bindings/dbus-xml/org-shadowblip-cpu.xml 139 | mdformat ./docs/cpu.md 140 | sed -i 's/DBus Interface API/CPU DBus Interface API/g' ./docs/cpu.md 141 | xsltproc --novalid -o docs/cpu-core.md $(XSL_TEMPLATE) bindings/dbus-xml/org-shadowblip-cpu-core.xml 142 | mdformat ./docs/cpu-core.md 143 | sed -i 's/DBus Interface API/CPU.Core DBus Interface API/g' ./docs/cpu-core.md 144 | xsltproc --novalid -o docs/gpu.md $(XSL_TEMPLATE) bindings/dbus-xml/org-shadowblip-gpu.xml 145 | mdformat ./docs/gpu.md 146 | sed -i 's/DBus Interface API/GPU DBus Interface API/g' ./docs/gpu.md 147 | xsltproc --novalid -o docs/gpu-card.md $(XSL_TEMPLATE) bindings/dbus-xml/org-shadowblip-gpu-card.xml 148 | mdformat ./docs/gpu-card.md 149 | sed -i 's/DBus Interface API/GPU.Card DBus Interface API/g' ./docs/gpu-card.md 150 | xsltproc --novalid -o docs/gpu-card-connector.md $(XSL_TEMPLATE) bindings/dbus-xml/org-shadowblip-gpu-card-connector.xml 151 | mdformat ./docs/gpu-card-connector.md 152 | sed -i 's/DBus Interface API/GPU.Card.Connector DBus Interface API/g' ./docs/gpu-card-connector.md 153 | 154 | # Refer to .releaserc.yaml for release configuration 155 | .PHONY: sem-release 156 | sem-release: ## Publish a release with semantic release 157 | npx semantic-release 158 | 159 | # Build the docker container for running in docker 160 | .PHONY: docker-builder 161 | docker-builder: 162 | docker build -t $(IMAGE_NAME):$(IMAGE_TAG) . 163 | 164 | # E.g. make in-docker TARGET=build 165 | .PHONY: in-docker 166 | in-docker: docker-builder 167 | @# Run the given make target inside Docker 168 | docker run --rm \ 169 | -v $(PWD):/src \ 170 | --workdir /src \ 171 | -e HOME=/home/build \ 172 | -e TARGET_ARCH=$(TARGET_ARCH) \ 173 | -e PKG_CONFIG_SYSROOT_DIR="/usr/$(ARCH)-linux-gnu" \ 174 | --user $(shell id -u):$(shell id -g) \ 175 | $(IMAGE_NAME):$(IMAGE_TAG) \ 176 | make $(TARGET) 177 | 178 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | PowerStation Logo 3 |
4 | PowerStation 5 |

6 | 7 |

8 | 9 | 10 | Discord 11 |
12 |

13 | 14 | ## About 15 | 16 | PowerStation is an open source TDP control and performance daemon for Linux that 17 | can be used to control CPU and GPU settings for better performance and battery 18 | life. Performance control is done through [DBus](https://www.freedesktop.org/wiki/Software/dbus/) 19 | to provide a UI-agnostic interface to CPU and GPU settings. 20 | 21 | ## Install 22 | 23 | You can install with: 24 | 25 | ```bash 26 | make build 27 | sudo make install 28 | ``` 29 | 30 | If you are using ArchLinux, you can install PowerStation from the AUR: 31 | 32 | ```bash 33 | yay -S powerstation-bin 34 | ``` 35 | 36 | Then start the service with: 37 | 38 | ```bash 39 | sudo systemctl enable powerstation 40 | sudo systemctl start powerstation 41 | ``` 42 | 43 | ## Documentation 44 | 45 | XML specifications for all interfaces can be found in [bindings/dbus-xml](./bindings/dbus-xml). 46 | 47 | Individual interface documentation can be found here: 48 | 49 | * [org.shadowblip.CPU](./docs/cpu.md) 50 | * [org.shadowblip.CPU.Core](./docs/cpu-core.md) 51 | * [org.shadowblip.GPU](./docs/gpu.md) 52 | * [org.shadowblip.GPU.Card](./docs/gpu-card.md) 53 | * [org.shadowblip.GPU.Card.Connector](./docs/gpu-card-connector.md) 54 | 55 | ## Usage 56 | 57 | When PowerStation is running as a service, you can interact with it over DBus. 58 | There are various DBus libraries available for popular programming languages 59 | like Python, Rust, C++, etc. 60 | 61 | You can also interface with DBus using the `busctl` command: 62 | 63 | ```bash 64 | busctl tree org.shadowblip.PowerStation 65 | ``` 66 | 67 | ```bash 68 | └─ /org 69 | └─ /org/shadowblip 70 | └─ /org/shadowblip/Performance 71 | ├─ /org/shadowblip/Performance/CPU 72 | │ ├─ /org/shadowblip/Performance/CPU/Core0 73 | │ ├─ /org/shadowblip/Performance/CPU/Core1 74 | │ ├─ /org/shadowblip/Performance/CPU/Core10 75 | │ ├─ /org/shadowblip/Performance/CPU/Core11 76 | │ ├─ /org/shadowblip/Performance/CPU/Core2 77 | │ ├─ /org/shadowblip/Performance/CPU/Core3 78 | │ ├─ /org/shadowblip/Performance/CPU/Core4 79 | │ ├─ /org/shadowblip/Performance/CPU/Core5 80 | │ ├─ /org/shadowblip/Performance/CPU/Core6 81 | │ ├─ /org/shadowblip/Performance/CPU/Core7 82 | │ ├─ /org/shadowblip/Performance/CPU/Core8 83 | │ └─ /org/shadowblip/Performance/CPU/Core9 84 | └─ /org/shadowblip/Performance/GPU 85 | ├─ /org/shadowblip/Performance/GPU/Card1 86 | │ └─ /org/shadowblip/Performance/GPU/Card1/HDMI 87 | │ └─ /org/shadowblip/Performance/GPU/Card1/HDMI/A 88 | │ └─ /org/shadowblip/Performance/GPU/Card1/HDMI/A/1 89 | └─ /org/shadowblip/Performance/GPU/Card2 90 | └─ /org/shadowblip/Performance/GPU/Card2/eDP 91 | └─ /org/shadowblip/Performance/GPU/Card2/eDP/1 92 | ``` 93 | 94 | ```bash 95 | busctl introspect org.shadowblip.PowerStation /org/shadowblip/Performance/GPU/Card2 96 | ``` 97 | 98 | ```bash 99 | NAME TYPE SIGNATURE RESULT/VALUE FLAGS 100 | org.freedesktop.DBus.Introspectable interface - - - 101 | .Introspect method - s - 102 | org.freedesktop.DBus.Peer interface - - - 103 | .GetMachineId method - s - 104 | .Ping method - - - 105 | org.freedesktop.DBus.Properties interface - - - 106 | .Get method ss v - 107 | .GetAll method s a{sv} - 108 | .Set method ssv - - 109 | .PropertiesChanged signal sa{sv}as - - 110 | org.shadowblip.GPU.Card interface - - - 111 | .EnumerateConnectors method - ao - 112 | .Class property s "integrated" emits-change 113 | .ClassId property s "030000" emits-change 114 | .ClockLimitMhzMax property d - emits-change 115 | .ClockLimitMhzMin property d - emits-change 116 | .ClockValueMhzMax property d - emits-change writable 117 | .ClockValueMhzMin property d - emits-change writable 118 | .Device property s "Renoir" emits-change 119 | .DeviceId property s "1636" emits-change 120 | .ManualClock property b false emits-change writable 121 | .Name property s "card2" emits-change 122 | .Path property s "/sys/class/drm/card2" emits-change 123 | .RevisionId property s "c7" emits-change 124 | .Subdevice property s "" emits-change 125 | .SubdeviceId property s "12b5" emits-change 126 | .SubvendorId property s "1462" emits-change 127 | .Vendor property s "AMD" emits-change 128 | .VendorId property s "1002" emits-change 129 | org.shadowblip.GPU.Card.TDP interface - - - 130 | .Boost property d 11 emits-change writable 131 | .PowerProfile property s "max-performance" emits-change writable 132 | .TDP property d 55 emits-change writable 133 | .ThermalThrottleLimitC property d 95 emits-change writable 134 | ``` 135 | 136 | ## Testing 137 | 138 | When PowerStation is running, you can test setting properties with: 139 | 140 | ```bash 141 | busctl set-property org.shadowblip.PowerStation /org/shadowblip/Performance/CPU/Core11 org.shadowblip.CPU.Core Online "b" False 142 | ``` 143 | 144 | 145 | ## License 146 | 147 | PowerStation is licensed under THE GNU GPLv3+. See LICENSE for details. 148 | -------------------------------------------------------------------------------- /bindings/dbus-xml/org-shadowblip-cpu-core.xml: -------------------------------------------------------------------------------- 1 | 2 | 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 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /bindings/dbus-xml/org-shadowblip-cpu.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 15 | 16 | 17 | 18 | 21 | 22 | 23 | 24 | 25 | 26 | 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 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /bindings/dbus-xml/org-shadowblip-gpu-card-connector.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /bindings/dbus-xml/org-shadowblip-gpu-card.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 11 | 12 | 15 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 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 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /bindings/dbus-xml/org-shadowblip-gpu.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /docs/cpu-core.md: -------------------------------------------------------------------------------- 1 | # CPU.Core DBus Interface API 2 | 3 | ## org.freedesktop.DBus.Peer 4 | 5 | ### Methods 6 | 7 | #### Ping 8 | 9 | #### GetMachineId 10 | 11 | ##### Arguments 12 | 13 | | Name | Direction | Type | Description | 14 | | --- | :---: | :---: | --- | 15 | | \*\*\*\* | *out* | *s* | | 16 | 17 | ### Signals 18 | 19 | ## org.shadowblip.CPU.Core 20 | 21 | ### Properties 22 | 23 | | Name | Access | Type | Description | 24 | | --- | :---: | :---: | --- | 25 | | **CoreId** | *read* | *u* | | 26 | | **Number** | *read* | *u* | | 27 | | **Online** | *readwrite* | *b* | | 28 | 29 | ### Methods 30 | 31 | ### Signals 32 | 33 | ## org.freedesktop.DBus.Properties 34 | 35 | ### Methods 36 | 37 | #### Get 38 | 39 | ##### Arguments 40 | 41 | | Name | Direction | Type | Description | 42 | | --- | :---: | :---: | --- | 43 | | **interface_name** | *in* | *s* | | 44 | | **property_name** | *in* | *s* | | 45 | | \*\*\*\* | *out* | *v* | | 46 | 47 | #### Set 48 | 49 | ##### Arguments 50 | 51 | | Name | Direction | Type | Description | 52 | | --- | :---: | :---: | --- | 53 | | **interface_name** | *in* | *s* | | 54 | | **property_name** | *in* | *s* | | 55 | | **value** | *in* | *v* | | 56 | 57 | #### GetAll 58 | 59 | ##### Arguments 60 | 61 | | Name | Direction | Type | Description | 62 | | --- | :---: | :---: | --- | 63 | | **interface_name** | *in* | *s* | | 64 | | \*\*\*\* | *out* | *a{sv}* | | 65 | 66 | ### Signals 67 | 68 | #### PropertiesChanged 69 | 70 | ##### Arguments 71 | 72 | | Name | Direction | Type | Description | 73 | | --- | :---: | :---: | --- | 74 | | **interface_name** | \*\* | *s* | | 75 | | **changed_properties** | \*\* | *a{sv}* | | 76 | | **invalidated_properties** | \*\* | *as* | | 77 | 78 | ## org.freedesktop.DBus.Introspectable 79 | 80 | ### Methods 81 | 82 | #### Introspect 83 | 84 | ##### Arguments 85 | 86 | | Name | Direction | Type | Description | 87 | | --- | :---: | :---: | --- | 88 | | \*\*\*\* | *out* | *s* | | 89 | 90 | ### Signals 91 | -------------------------------------------------------------------------------- /docs/cpu.md: -------------------------------------------------------------------------------- 1 | # CPU DBus Interface API 2 | 3 | ## org.freedesktop.DBus.Peer 4 | 5 | ### Methods 6 | 7 | #### Ping 8 | 9 | #### GetMachineId 10 | 11 | ##### Arguments 12 | 13 | | Name | Direction | Type | Description | 14 | | --- | :---: | :---: | --- | 15 | | \*\*\*\* | *out* | *s* | | 16 | 17 | ### Signals 18 | 19 | ## org.shadowblip.CPU 20 | 21 | ### Properties 22 | 23 | | Name | Access | Type | Description | 24 | | --- | :---: | :---: | --- | 25 | | **BoostEnabled** | *readwrite* | *b* | | 26 | | **CoresCount** | *read* | *u* | | 27 | | **CoresEnabled** | *readwrite* | *u* | | 28 | | **Features** | *read* | *as* | | 29 | | **SmtEnabled** | *readwrite* | *b* | | 30 | 31 | ### Methods 32 | 33 | #### EnumerateCores 34 | 35 | ##### Arguments 36 | 37 | | Name | Direction | Type | Description | 38 | | --- | :---: | :---: | --- | 39 | | \*\*\*\* | *out* | *ao* | | 40 | 41 | #### HasFeature 42 | 43 | ##### Arguments 44 | 45 | | Name | Direction | Type | Description | 46 | | --- | :---: | :---: | --- | 47 | | **flag** | *in* | *s* | | 48 | | \*\*\*\* | *out* | *b* | | 49 | 50 | ### Signals 51 | 52 | ## org.freedesktop.DBus.Introspectable 53 | 54 | ### Methods 55 | 56 | #### Introspect 57 | 58 | ##### Arguments 59 | 60 | | Name | Direction | Type | Description | 61 | | --- | :---: | :---: | --- | 62 | | \*\*\*\* | *out* | *s* | | 63 | 64 | ### Signals 65 | 66 | ## org.freedesktop.DBus.Properties 67 | 68 | ### Methods 69 | 70 | #### Get 71 | 72 | ##### Arguments 73 | 74 | | Name | Direction | Type | Description | 75 | | --- | :---: | :---: | --- | 76 | | **interface_name** | *in* | *s* | | 77 | | **property_name** | *in* | *s* | | 78 | | \*\*\*\* | *out* | *v* | | 79 | 80 | #### Set 81 | 82 | ##### Arguments 83 | 84 | | Name | Direction | Type | Description | 85 | | --- | :---: | :---: | --- | 86 | | **interface_name** | *in* | *s* | | 87 | | **property_name** | *in* | *s* | | 88 | | **value** | *in* | *v* | | 89 | 90 | #### GetAll 91 | 92 | ##### Arguments 93 | 94 | | Name | Direction | Type | Description | 95 | | --- | :---: | :---: | --- | 96 | | **interface_name** | *in* | *s* | | 97 | | \*\*\*\* | *out* | *a{sv}* | | 98 | 99 | ### Signals 100 | 101 | #### PropertiesChanged 102 | 103 | ##### Arguments 104 | 105 | | Name | Direction | Type | Description | 106 | | --- | :---: | :---: | --- | 107 | | **interface_name** | \*\* | *s* | | 108 | | **changed_properties** | \*\* | *a{sv}* | | 109 | | **invalidated_properties** | \*\* | *as* | | 110 | -------------------------------------------------------------------------------- /docs/dbus2markdown.xsl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 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 | # DBus Interface API 36 | 37 | ## 38 | 39 | 40 | ### Properties 41 | 42 | | Name | Access | Type | Description | 43 | | --- | :---: | :---: | --- | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | | **** | ** | ** | | 60 | 61 | ### Methods 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | #### 71 | 72 | 73 | 74 | 75 | ##### Arguments 76 | 77 | | Name | Direction | Type | Description | 78 | | --- | :---: | :---: | --- | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | | **** | ** | ** | | 95 | 96 | 97 | 98 | 99 | ### Signals 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | #### 109 | 110 | 111 | 112 | 113 | ##### Arguments 114 | 115 | | Name | Direction | Type | Description | 116 | | --- | :---: | :---: | --- | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | | **** | ** | ** | | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | -------------------------------------------------------------------------------- /docs/gpu-card-connector.md: -------------------------------------------------------------------------------- 1 | # GPU.Card.Connector DBus Interface API 2 | 3 | ## org.freedesktop.DBus.Peer 4 | 5 | ### Methods 6 | 7 | #### Ping 8 | 9 | #### GetMachineId 10 | 11 | ##### Arguments 12 | 13 | | Name | Direction | Type | Description | 14 | | --- | :---: | :---: | --- | 15 | | \*\*\*\* | *out* | *s* | | 16 | 17 | ### Signals 18 | 19 | ## org.freedesktop.DBus.Properties 20 | 21 | ### Methods 22 | 23 | #### Get 24 | 25 | ##### Arguments 26 | 27 | | Name | Direction | Type | Description | 28 | | --- | :---: | :---: | --- | 29 | | **interface_name** | *in* | *s* | | 30 | | **property_name** | *in* | *s* | | 31 | | \*\*\*\* | *out* | *v* | | 32 | 33 | #### Set 34 | 35 | ##### Arguments 36 | 37 | | Name | Direction | Type | Description | 38 | | --- | :---: | :---: | --- | 39 | | **interface_name** | *in* | *s* | | 40 | | **property_name** | *in* | *s* | | 41 | | **value** | *in* | *v* | | 42 | 43 | #### GetAll 44 | 45 | ##### Arguments 46 | 47 | | Name | Direction | Type | Description | 48 | | --- | :---: | :---: | --- | 49 | | **interface_name** | *in* | *s* | | 50 | | \*\*\*\* | *out* | *a{sv}* | | 51 | 52 | ### Signals 53 | 54 | #### PropertiesChanged 55 | 56 | ##### Arguments 57 | 58 | | Name | Direction | Type | Description | 59 | | --- | :---: | :---: | --- | 60 | | **interface_name** | \*\* | *s* | | 61 | | **changed_properties** | \*\* | *a{sv}* | | 62 | | **invalidated_properties** | \*\* | *as* | | 63 | 64 | ## org.shadowblip.GPU.Card.Connector 65 | 66 | ### Properties 67 | 68 | | Name | Access | Type | Description | 69 | | --- | :---: | :---: | --- | 70 | | **DPMS** | *read* | *b* | | 71 | | **Enabled** | *read* | *b* | | 72 | | **Id** | *read* | *u* | | 73 | | **Modes** | *read* | *as* | | 74 | | **Name** | *read* | *s* | | 75 | | **Path** | *read* | *s* | | 76 | | **Status** | *read* | *s* | | 77 | 78 | ### Methods 79 | 80 | ### Signals 81 | 82 | ## org.freedesktop.DBus.Introspectable 83 | 84 | ### Methods 85 | 86 | #### Introspect 87 | 88 | ##### Arguments 89 | 90 | | Name | Direction | Type | Description | 91 | | --- | :---: | :---: | --- | 92 | | \*\*\*\* | *out* | *s* | | 93 | 94 | ### Signals 95 | -------------------------------------------------------------------------------- /docs/gpu-card.md: -------------------------------------------------------------------------------- 1 | # GPU.Card DBus Interface API 2 | 3 | ## org.shadowblip.GPU.Card.TDP 4 | 5 | ### Properties 6 | 7 | | Name | Access | Type | Description | 8 | | --- | :---: | :---: | --- | 9 | | **Boost** | *readwrite* | *d* | | 10 | | **PowerProfile** | *readwrite* | *s* | | 11 | | **TDP** | *readwrite* | *d* | | 12 | | **ThermalThrottleLimitC** | *readwrite* | *d* | | 13 | 14 | ### Methods 15 | 16 | ### Signals 17 | 18 | ## org.shadowblip.GPU.Card 19 | 20 | ### Properties 21 | 22 | | Name | Access | Type | Description | 23 | | --- | :---: | :---: | --- | 24 | | **Class** | *read* | *s* | | 25 | | **ClassId** | *read* | *s* | | 26 | | **ClockLimitMhzMax** | *read* | *d* | | 27 | | **ClockLimitMhzMin** | *read* | *d* | | 28 | | **ClockValueMhzMax** | *readwrite* | *d* | | 29 | | **ClockValueMhzMin** | *readwrite* | *d* | | 30 | | **Device** | *read* | *s* | | 31 | | **DeviceId** | *read* | *s* | | 32 | | **ManualClock** | *readwrite* | *b* | | 33 | | **Name** | *read* | *s* | | 34 | | **Path** | *read* | *s* | | 35 | | **RevisionId** | *read* | *s* | | 36 | | **Subdevice** | *read* | *s* | | 37 | | **SubdeviceId** | *read* | *s* | | 38 | | **SubvendorId** | *read* | *s* | | 39 | | **Vendor** | *read* | *s* | | 40 | | **VendorId** | *read* | *s* | | 41 | 42 | ### Methods 43 | 44 | #### EnumerateConnectors 45 | 46 | ##### Arguments 47 | 48 | | Name | Direction | Type | Description | 49 | | --- | :---: | :---: | --- | 50 | | \*\*\*\* | *out* | *ao* | | 51 | 52 | ### Signals 53 | 54 | ## org.freedesktop.DBus.Properties 55 | 56 | ### Methods 57 | 58 | #### Get 59 | 60 | ##### Arguments 61 | 62 | | Name | Direction | Type | Description | 63 | | --- | :---: | :---: | --- | 64 | | **interface_name** | *in* | *s* | | 65 | | **property_name** | *in* | *s* | | 66 | | \*\*\*\* | *out* | *v* | | 67 | 68 | #### Set 69 | 70 | ##### Arguments 71 | 72 | | Name | Direction | Type | Description | 73 | | --- | :---: | :---: | --- | 74 | | **interface_name** | *in* | *s* | | 75 | | **property_name** | *in* | *s* | | 76 | | **value** | *in* | *v* | | 77 | 78 | #### GetAll 79 | 80 | ##### Arguments 81 | 82 | | Name | Direction | Type | Description | 83 | | --- | :---: | :---: | --- | 84 | | **interface_name** | *in* | *s* | | 85 | | \*\*\*\* | *out* | *a{sv}* | | 86 | 87 | ### Signals 88 | 89 | #### PropertiesChanged 90 | 91 | ##### Arguments 92 | 93 | | Name | Direction | Type | Description | 94 | | --- | :---: | :---: | --- | 95 | | **interface_name** | \*\* | *s* | | 96 | | **changed_properties** | \*\* | *a{sv}* | | 97 | | **invalidated_properties** | \*\* | *as* | | 98 | 99 | ## org.freedesktop.DBus.Peer 100 | 101 | ### Methods 102 | 103 | #### Ping 104 | 105 | #### GetMachineId 106 | 107 | ##### Arguments 108 | 109 | | Name | Direction | Type | Description | 110 | | --- | :---: | :---: | --- | 111 | | \*\*\*\* | *out* | *s* | | 112 | 113 | ### Signals 114 | 115 | ## org.freedesktop.DBus.Introspectable 116 | 117 | ### Methods 118 | 119 | #### Introspect 120 | 121 | ##### Arguments 122 | 123 | | Name | Direction | Type | Description | 124 | | --- | :---: | :---: | --- | 125 | | \*\*\*\* | *out* | *s* | | 126 | 127 | ### Signals 128 | -------------------------------------------------------------------------------- /docs/gpu.md: -------------------------------------------------------------------------------- 1 | # GPU DBus Interface API 2 | 3 | ## org.freedesktop.DBus.Properties 4 | 5 | ### Methods 6 | 7 | #### Get 8 | 9 | ##### Arguments 10 | 11 | | Name | Direction | Type | Description | 12 | | --- | :---: | :---: | --- | 13 | | **interface_name** | *in* | *s* | | 14 | | **property_name** | *in* | *s* | | 15 | | \*\*\*\* | *out* | *v* | | 16 | 17 | #### Set 18 | 19 | ##### Arguments 20 | 21 | | Name | Direction | Type | Description | 22 | | --- | :---: | :---: | --- | 23 | | **interface_name** | *in* | *s* | | 24 | | **property_name** | *in* | *s* | | 25 | | **value** | *in* | *v* | | 26 | 27 | #### GetAll 28 | 29 | ##### Arguments 30 | 31 | | Name | Direction | Type | Description | 32 | | --- | :---: | :---: | --- | 33 | | **interface_name** | *in* | *s* | | 34 | | \*\*\*\* | *out* | *a{sv}* | | 35 | 36 | ### Signals 37 | 38 | #### PropertiesChanged 39 | 40 | ##### Arguments 41 | 42 | | Name | Direction | Type | Description | 43 | | --- | :---: | :---: | --- | 44 | | **interface_name** | \*\* | *s* | | 45 | | **changed_properties** | \*\* | *a{sv}* | | 46 | | **invalidated_properties** | \*\* | *as* | | 47 | 48 | ## org.shadowblip.GPU 49 | 50 | ### Methods 51 | 52 | #### EnumerateCards 53 | 54 | ##### Arguments 55 | 56 | | Name | Direction | Type | Description | 57 | | --- | :---: | :---: | --- | 58 | | \*\*\*\* | *out* | *ao* | | 59 | 60 | ### Signals 61 | 62 | ## org.freedesktop.DBus.Introspectable 63 | 64 | ### Methods 65 | 66 | #### Introspect 67 | 68 | ##### Arguments 69 | 70 | | Name | Direction | Type | Description | 71 | | --- | :---: | :---: | --- | 72 | | \*\*\*\* | *out* | *s* | | 73 | 74 | ### Signals 75 | 76 | ## org.freedesktop.DBus.Peer 77 | 78 | ### Methods 79 | 80 | #### Ping 81 | 82 | #### GetMachineId 83 | 84 | ##### Arguments 85 | 86 | | Name | Direction | Type | Description | 87 | | --- | :---: | :---: | --- | 88 | | \*\*\*\* | *out* | *s* | | 89 | 90 | ### Signals 91 | -------------------------------------------------------------------------------- /icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 14 | 16 | 34 | 41 | 44 | 50 | 56 | 63 | 69 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /rootfs/usr/lib/systemd/system/powerstation.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=PowerStation Service 3 | After=graphical-session.target 4 | 5 | [Service] 6 | ExecStart=/usr/bin/powerstation 7 | 8 | [Install] 9 | WantedBy=multi-user.target 10 | -------------------------------------------------------------------------------- /rootfs/usr/share/dbus-1/system-services/org.shadowblip.PowerStation.service: -------------------------------------------------------------------------------- 1 | [D-BUS Service] 2 | Name=com.github.guelfey.Demo 3 | User=${USER} 4 | Exec=/tmp/dbus-demo-server 5 | -------------------------------------------------------------------------------- /rootfs/usr/share/dbus-1/system.d/org.shadowblip.PowerStation.conf: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /rootfs/usr/share/powerstation/platform/amd_apu_database.toml: -------------------------------------------------------------------------------- 1 | [[models]] 2 | model_name = "AMD Athlon Silver 3020e with Radeon Graphics" 3 | min_tdp = 2.0 4 | max_tdp = 12.0 5 | max_boost = 6.0 6 | 7 | [[models]] 8 | model_name = "AMD Athlon Silver 3050e with Radeon Graphics" 9 | min_tdp = 2.0 10 | max_tdp = 20.0 11 | max_boost = 5.0 12 | 13 | [[models]] 14 | model_name = "AMD Custom APU 0405" 15 | min_tdp = 3.0 16 | max_tdp = 15.0 17 | max_boost = 5.0 18 | 19 | [[models]] 20 | model_name = "AMD Ryzen 3 2200U with Radeon Graphics" 21 | min_tdp = 2.0 22 | max_tdp = 20.0 23 | max_boost = 5.0 24 | 25 | [[models]] 26 | model_name = "AMD Ryzen 3 2300U with Radeon Graphics" 27 | min_tdp = 2.0 28 | max_tdp = 25.0 29 | max_boost = 5.0 30 | 31 | [[models]] 32 | model_name = "AMD Ryzen 3 3200U with Radeon Graphics" 33 | min_tdp = 2.0 34 | max_tdp = 20.0 35 | max_boost = 5.0 36 | 37 | [[models]] 38 | model_name = "AMD Ryzen 3 3300U with Radeon Graphics" 39 | min_tdp = 2.0 40 | max_tdp = 25.0 41 | max_boost = 5.0 42 | 43 | [[models]] 44 | model_name = "AMD Ryzen 3 4300U with Radeon Graphics" 45 | min_tdp = 2.0 46 | max_tdp = 23.0 47 | max_boost = 2.0 48 | 49 | [[models]] 50 | model_name = "AMD Ryzen 3 5125C with Radeon Graphics" 51 | min_tdp = 2.0 52 | max_tdp = 15.0 53 | max_boost = 2.0 54 | 55 | [[models]] 56 | model_name = "AMD Ryzen 3 5300U with Radeon Graphics" 57 | min_tdp = 5.0 58 | max_tdp = 23.0 59 | max_boost = 2.0 60 | 61 | [[models]] 62 | model_name = "AMD Ryzen 3 5400U with Radeon Graphics" 63 | min_tdp = 5.0 64 | max_tdp = 23.0 65 | max_boost = 2.0 66 | 67 | [[models]] 68 | model_name = "AMD Ryzen 3 5425C with Radeon Graphics" 69 | min_tdp = 5.0 70 | max_tdp = 23.0 71 | max_boost = 2.0 72 | 73 | [[models]] 74 | model_name = "AMD Ryzen 3 5425U with Radeon Graphics" 75 | min_tdp = 2.0 76 | max_tdp = 15.0 77 | max_boost = 2.0 78 | 79 | [[models]] 80 | model_name = "AMD Ryzen 5 2500U with Radeon Graphics" 81 | min_tdp = 5.0 82 | max_tdp = 25.0 83 | max_boost = 5.0 84 | 85 | [[models]] 86 | model_name = "AMD Ryzen 5 3500U with Radeon Graphics" 87 | min_tdp = 5.0 88 | max_tdp = 30.0 89 | max_boost = 5.0 90 | 91 | [[models]] 92 | model_name = "AMD Ryzen 5 3550H with Radeon Graphics" 93 | min_tdp = 5.0 94 | max_tdp = 35.0 95 | max_boost = 5.0 96 | 97 | [[models]] 98 | model_name = "AMD Ryzen 5 4500U with Radeon Graphics" 99 | min_tdp = 5.0 100 | max_tdp = 28.0 101 | max_boost = 2.0 102 | 103 | [[models]] 104 | model_name = "AMD Ryzen 5 4600H with Radeon Graphics" 105 | min_tdp = 5.0 106 | max_tdp = 55.0 107 | max_boost = 11.0 108 | 109 | [[models]] 110 | model_name = "AMD Ryzen 5 4600HS with Radeon Graphics" 111 | min_tdp = 5.0 112 | max_tdp = 45.0 113 | max_boost = 11.0 114 | 115 | [[models]] 116 | model_name = "AMD Ryzen 5 4600U with Radeon Graphics" 117 | min_tdp = 5.0 118 | max_tdp = 33.0 119 | max_boost = 5.0 120 | 121 | [[models]] 122 | model_name = "AMD Ryzen 5 5500U with Radeon Graphics" 123 | min_tdp = 5.0 124 | max_tdp = 28.0 125 | max_boost = 2.0 126 | 127 | [[models]] 128 | model_name = "AMD Ryzen 5 5560U with Radeon Graphics" 129 | min_tdp = 3.0 130 | max_tdp = 28.0 131 | max_boost = 2.0 132 | 133 | [[models]] 134 | model_name = "AMD Ryzen 5 5600H with Radeon Graphics" 135 | min_tdp = 2.0 136 | max_tdp = 12.0 137 | max_boost = 6.0 138 | 139 | [[models]] 140 | model_name = "AMD Ryzen 5 5600HS with Radeon Graphics" 141 | min_tdp = 5.0 142 | max_tdp = 45.0 143 | max_boost = 11.0 144 | 145 | [[models]] 146 | model_name = "AMD Ryzen 5 5600U with Radeon Graphics" 147 | min_tdp = 5.0 148 | max_tdp = 33.0 149 | max_boost = 5.0 150 | 151 | [[models]] 152 | model_name = "AMD Ryzen 5 5625C with Radeon Graphics" 153 | min_tdp = 2.0 154 | max_tdp = 15.0 155 | max_boost = 2.0 156 | 157 | [[models]] 158 | model_name = "AMD Ryzen 5 5625U with Radeon Graphics" 159 | min_tdp = 5.0 160 | max_tdp = 33.0 161 | max_boost = 5.0 162 | 163 | [[models]] 164 | model_name = "AMD Ryzen 5 6600H with Radeon Graphics" 165 | min_tdp = 5.0 166 | max_tdp = 58.0 167 | max_boost = 10.0 168 | 169 | [[models]] 170 | model_name = "AMD Ryzen 5 6600HS with Radeon Graphics" 171 | min_tdp = 5.0 172 | max_tdp = 45.0 173 | max_boost = 11.0 174 | 175 | [[models]] 176 | model_name = "AMD Ryzen 5 6600U with Radeon Graphics" 177 | min_tdp = 5.0 178 | max_tdp = 33.0 179 | max_boost = 5.0 180 | 181 | [[models]] 182 | model_name = "AMD Ryzen 7 2700U with Radeon Graphics" 183 | min_tdp = 5.0 184 | max_tdp = 25.0 185 | max_boost = 5.0 186 | 187 | [[models]] 188 | model_name = "AMD Ryzen 7 3700U with Radeon Graphics" 189 | min_tdp = 5.0 190 | max_tdp = 30.0 191 | max_boost = 10.0 192 | 193 | [[models]] 194 | model_name = "AMD Ryzen 7 3750H with Radeon Graphics" 195 | min_tdp = 5.0 196 | max_tdp = 40.0 197 | max_boost = 10.0 198 | 199 | [[models]] 200 | model_name = "AMD Ryzen 7 4700U with Radeon Graphics" 201 | min_tdp = 2.0 202 | max_tdp = 12.0 203 | max_boost = 6.0 204 | 205 | [[models]] 206 | model_name = "AMD Ryzen 7 4800H with Radeon Graphics" 207 | min_tdp = 5.0 208 | max_tdp = 60.0 209 | max_boost = 8.0 210 | 211 | [[models]] 212 | model_name = "AMD Ryzen 7 4800HS with Radeon Graphics" 213 | min_tdp = 5.0 214 | max_tdp = 50.0 215 | max_boost = 8.0 216 | 217 | [[models]] 218 | model_name = "AMD Ryzen 7 4800U with Radeon Graphics" 219 | min_tdp = 5.0 220 | max_tdp = 33.0 221 | max_boost = 5.0 222 | 223 | [[models]] 224 | model_name = "AMD Ryzen 7 4980U with Radeon Graphics" 225 | min_tdp = 10.0 226 | max_tdp = 15.0 227 | max_boost = 10.0 228 | 229 | [[models]] 230 | model_name = "AMD Ryzen 7 5700U with Radeon Graphics" 231 | min_tdp = 5.0 232 | max_tdp = 28.0 233 | max_boost = 2.0 234 | 235 | [[models]] 236 | model_name = "AMD Ryzen 7 5800H with Radeon Graphics" 237 | min_tdp = 10.0 238 | max_tdp = 68.0 239 | max_boost = 4.0 240 | 241 | [[models]] 242 | model_name = "AMD Ryzen 7 5800HS with Radeon Graphics" 243 | min_tdp = 10.0 244 | max_tdp = 50.0 245 | max_boost = 8.0 246 | 247 | [[models]] 248 | model_name = "AMD Ryzen 7 5800U with Radeon Graphics" 249 | min_tdp = 5.0 250 | max_tdp = 33.0 251 | max_boost = 5.0 252 | 253 | [[models]] 254 | model_name = "AMD Ryzen 7 5825C with Radeon Graphics" 255 | min_tdp = 10.0 256 | max_tdp = 15.0 257 | max_boost = 10.0 258 | 259 | [[models]] 260 | model_name = "AMD Ryzen 7 5825U with Radeon Graphics" 261 | min_tdp = 5.0 262 | max_tdp = 33.0 263 | max_boost = 5.0 264 | 265 | [[models]] 266 | model_name = "AMD Ryzen 7 6800H with Radeon Graphics" 267 | min_tdp = 10.0 268 | max_tdp = 58.0 269 | max_boost = 10.0 270 | 271 | [[models]] 272 | model_name = "AMD Ryzen 7 6800HS with Radeon Graphics" 273 | min_tdp = 10.0 274 | max_tdp = 50.0 275 | max_boost = 8.0 276 | 277 | [[models]] 278 | model_name = "AMD Ryzen 7 6800U with Radeon Graphics" 279 | min_tdp = 5.0 280 | max_tdp = 33.0 281 | max_boost = 5.0 282 | 283 | [[models]] 284 | model_name = "AMD Ryzen 9 4900H with Radeon Graphics" 285 | min_tdp = 10.0 286 | max_tdp = 60.0 287 | max_boost = 8.0 288 | 289 | [[models]] 290 | model_name = "AMD Ryzen 9 4900HS with Radeon Graphics" 291 | min_tdp = 5.0 292 | max_tdp = 50.0 293 | max_boost = 8.0 294 | 295 | [[models]] 296 | model_name = "AMD Ryzen 9 5900HS with Radeon Graphics" 297 | min_tdp = 10.0 298 | max_tdp = 50.0 299 | max_boost = 8.0 300 | 301 | [[models]] 302 | model_name = "AMD Ryzen 9 5900HX with Radeon Graphics" 303 | min_tdp = 10.0 304 | max_tdp = 70.0 305 | max_boost = 20.0 306 | 307 | [[models]] 308 | model_name = "AMD Ryzen 9 5980HS with Radeon Graphics" 309 | min_tdp = 10.0 310 | max_tdp = 50.0 311 | max_boost = 8.0 312 | 313 | [[models]] 314 | model_name = "AMD Ryzen 9 5980HX with Radeon Graphics" 315 | min_tdp = 10.0 316 | max_tdp = 70.0 317 | max_boost = 20.0 318 | 319 | [[models]] 320 | model_name = "AMD Ryzen 9 6900HS with Radeon Graphics" 321 | min_tdp = 10.0 322 | max_tdp = 50.0 323 | max_boost = 8.0 324 | 325 | [[models]] 326 | model_name = "AMD Ryzen 9 6900HX with Radeon Graphics" 327 | min_tdp = 10.0 328 | max_tdp = 70.0 329 | max_boost = 20.0 330 | 331 | [[models]] 332 | model_name = "AMD Ryzen 9 6980HS with Radeon Graphics" 333 | min_tdp = 10.0 334 | max_tdp = 50.0 335 | max_boost = 8.0 336 | 337 | [[models]] 338 | model_name = "AMD Ryzen 9 6980HX with Radeon Graphics" 339 | min_tdp = 10.0 340 | max_tdp = 70.0 341 | max_boost = 20.0 342 | 343 | [[models]] 344 | model_name = "AMD Ryzen Embedded R1305G with Radeon Vega Gfx" 345 | min_tdp = 6.0 346 | max_tdp = 25.0 347 | max_boost = 2.0 348 | 349 | [[models]] 350 | model_name = "AMD Ryzen Embedded R1505G with Radeon Vega Gfx" 351 | min_tdp = 6.0 352 | max_tdp = 25.0 353 | max_boost = 2.0 354 | 355 | [[models]] 356 | model_name = "AMD Ryzen Embedded R1606G with Radeon Vega Gfx" 357 | min_tdp = 6.0 358 | max_tdp = 10.0 359 | max_boost = 2.0 360 | 361 | [[models]] 362 | model_name = "AMD Ryzen Z1 Extreme" 363 | min_tdp = 5.0 364 | max_tdp = 35.0 365 | max_boost = 5.0 366 | 367 | [[models]] 368 | model_name = "AMD Ryzen 7 7840U w/ Radeon 780M Graphics" 369 | min_tdp = 5.0 370 | max_tdp = 35.0 371 | max_boost = 5.0 372 | 373 | [[models]] 374 | model_name = "AMD Ryzen 9 7940HS w/ Radeon 780M Graphics" 375 | min_tdp = 35.0 376 | max_tdp = 54.0 377 | max_boost = 8.0 378 | 379 | [[models]] 380 | model_name = "AMD Ryzen 9 7945HX w/ Radeon 610M Graphics" 381 | min_tdp = 55.0 382 | max_tdp = 75.0 383 | max_boost = 10.0 384 | 385 | [[models]] 386 | model_name = "AMD Ryzen 9 7845HX w/ Radeon 610M Graphics" 387 | min_tdp = 45.0 388 | max_tdp = 75.0 389 | max_boost = 10.0 390 | 391 | [[models]] 392 | model_name = "AMD Ryzen 9 7745HX w/ Radeon 610M Graphics" 393 | min_tdp = 45.0 394 | max_tdp = 75.0 395 | max_boost = 10.0 396 | 397 | [[models]] 398 | model_name = "AMD Ryzen 9 7644HX w/ Radeon 610M Graphics" 399 | min_tdp = 45.0 400 | max_tdp = 75.0 401 | max_boost = 10.0 402 | 403 | [[models]] 404 | model_name = "AMD Ryzen 7 7840HS w/ Radeon 780M Graphics" 405 | min_tdp = 35.0 406 | max_tdp = 54.0 407 | max_boost = 8.0 408 | 409 | [[models]] 410 | model_name = "AMD Ryzen 7 7735HS with Radeon Graphics" 411 | min_tdp = 35.0 412 | max_tdp = 54.0 413 | max_boost = 8.0 414 | 415 | [[models]] 416 | model_name = "AMD Ryzen 7 7736U w/ Radeon 680M Graphics" 417 | min_tdp = 5.0 418 | max_tdp = 28.0 419 | max_boost = 5.0 420 | 421 | [[models]] 422 | model_name = "AMD Ryzen 7 7735U w/ Radeon 680M Graphics" 423 | min_tdp = 5.0 424 | max_tdp = 28.0 425 | max_boost = 5.0 426 | 427 | [[models]] 428 | model_name = "AMD Ryzen 7 7730U w/ Radeon Graphics" 429 | min_tdp = 5.0 430 | max_tdp = 15.0 431 | max_boost = 3.0 432 | 433 | [[models]] 434 | model_name = "AMD Ryzen 5 7645HX w/ Radeon 760M Graphics" 435 | min_tdp = 35.0 436 | max_tdp = 55.0 437 | max_boost = 8.0 438 | 439 | [[models]] 440 | model_name = "AMD Ryzen 5 7640HS w/ Radeon 760M Graphics" 441 | min_tdp = 35.0 442 | max_tdp = 54.0 443 | max_boost = 8.0 444 | 445 | [[models]] 446 | model_name = "AMD Ryzen 5 7535HS w/ Radeon 680M Graphics" 447 | min_tdp = 35.0 448 | max_tdp = 54.0 449 | max_boost = 8.0 450 | 451 | [[models]] 452 | model_name = "AMD Ryzen 5 7640U w/ Radeon 760M Graphics" 453 | min_tdp = 5.0 454 | max_tdp = 30.0 455 | max_boost = 5.0 456 | 457 | [[models]] 458 | model_name = "AMD Ryzen 5 7540U w/ Radeon 740M Graphics" 459 | min_tdp = 5.0 460 | max_tdp = 30.0 461 | max_boost = 5.0 462 | 463 | [[models]] 464 | model_name = "AMD Ryzen 5 7535U w/ Radeon 660M Graphics" 465 | min_tdp = 5.0 466 | max_tdp = 28.0 467 | max_boost = 5.0 468 | 469 | [[models]] 470 | model_name = "AMD Ryzen 5 7530U w/ Radeon Graphics" 471 | min_tdp = 5.0 472 | max_tdp = 15.0 473 | max_boost = 3.0 474 | 475 | [[models]] 476 | model_name = "AMD Ryzen 5 7520U w/ Radeon 610M Graphics" 477 | min_tdp = 5.0 478 | max_tdp = 15.0 479 | max_boost = 3.0 480 | 481 | [[models]] 482 | model_name = "AMD Ryzen 3 7440U w/ Radeon 740M Graphics" 483 | min_tdp = 5.0 484 | max_tdp = 30.0 485 | max_boost = 5.0 486 | 487 | [[models]] 488 | model_name = "AMD Ryzen 3 7335U w/ Radeon 660M Graphics" 489 | min_tdp = 5.0 490 | max_tdp = 28.0 491 | max_boost = 5.0 492 | 493 | [[models]] 494 | model_name = "AMD Ryzen 3 7330U w/ Radeon Graphics" 495 | min_tdp = 5.0 496 | max_tdp = 15.0 497 | max_boost = 3.0 498 | 499 | [[models]] 500 | model_name = "AMD Ryzen 3 7320U w/ Radeon 610M Graphics" 501 | min_tdp = 5.0 502 | max_tdp = 15.0 503 | max_boost = 0.0 504 | 505 | [[models]] 506 | model_name = "AMD Custom APU 0932" 507 | min_tdp = 3.0 508 | max_tdp = 15.0 509 | max_boost = 5.0 510 | 511 | [[models]] 512 | model_name = "AMD Ryzen 9 8945HS w/ Radeon 780M Graphics" 513 | min_tdp = 35.0 514 | max_tdp = 54.0 515 | max_boost = 8.0 516 | 517 | [[models]] 518 | model_name = "AMD Ryzen 7 8845HS w/ Radeon 780M Graphics" 519 | min_tdp = 35.0 520 | max_tdp = 54.0 521 | max_boost = 8.0 522 | 523 | [[models]] 524 | model_name = "AMD Ryzen 7 8840HS w/ Radeon 780M Graphics" 525 | min_tdp = 5.0 526 | max_tdp = 35.0 527 | max_boost = 5.0 528 | 529 | [[models]] 530 | model_name = "AMD Ryzen 7 8840U w/ Radeon 780M Graphics" 531 | min_tdp = 5.0 532 | max_tdp = 35.0 533 | max_boost = 5.0 534 | 535 | [[models]] 536 | model_name = "AMD Ryzen 5 8645HS w/ Radeon 760M Graphics" 537 | min_tdp = 35.0 538 | max_tdp = 54.0 539 | max_boost = 8.0 540 | 541 | [[models]] 542 | model_name = "AMD Ryzen 5 8640HS w/ Radeon 760M Graphics" 543 | min_tdp = 5.0 544 | max_tdp = 30.0 545 | max_boost = 5.0 546 | 547 | [[models]] 548 | model_name = "AMD Ryzen 5 8640U w/ Radeon 760M Graphics" 549 | min_tdp = 5.0 550 | max_tdp = 30.0 551 | max_boost = 5.0 552 | 553 | [[models]] 554 | model_name = "AMD Ryzen 5 8540U w/ Radeon 740M Graphics" 555 | min_tdp = 5.0 556 | max_tdp = 30.0 557 | max_boost = 5.0 558 | 559 | [[models]] 560 | model_name = "AMD Ryzen 3 8540U w/ Radeon 740M Graphics" 561 | min_tdp = 5.0 562 | max_tdp = 30.0 563 | max_boost = 0.0 564 | -------------------------------------------------------------------------------- /rootfs/usr/share/powerstation/platform/dmi_overrides_apu_database.toml: -------------------------------------------------------------------------------- 1 | [[models]] 2 | model_name = "NEO-01" 3 | min_tdp = 8.0 4 | max_tdp = 28.0 5 | max_boost = 4.0 6 | 7 | [[models]] 8 | model_name = "AIR" 9 | min_tdp = 5.0 10 | max_tdp = 15.0 11 | max_boost = 0.0 12 | 13 | [[models]] 14 | model_name = "AIR Pro" 15 | min_tdp = 5.0 16 | max_tdp = 18.0 17 | max_boost = 0.0 18 | 19 | [[models]] 20 | model_name = "AIR 1S" 21 | min_tdp = 5.0 22 | max_tdp = 25.0 23 | max_boost = 0.0 24 | 25 | [[models]] 26 | model_name = "AIR 1S Limited" 27 | min_tdp = 5.0 28 | max_tdp = 25.0 29 | max_boost = 0.0 30 | 31 | [[models]] 32 | model_name = "AIR Plus" 33 | min_tdp = 5.0 34 | max_tdp = 28.0 35 | max_boost = 0.0 36 | 37 | [[models]] 38 | model_name = "AYANEO 2S" 39 | min_tdp = 5.0 40 | max_tdp = 25.0 41 | max_boost = 0.0 42 | 43 | [[models]] 44 | model_name = "GEEK 1S" 45 | min_tdp = 5.0 46 | max_tdp = 25.0 47 | max_boost = 0.0 48 | 49 | [[models]] 50 | model_name = "SuiPlay0X1" 51 | min_tdp = 5.0 52 | max_tdp = 25.0 53 | max_boost = 0.0 54 | 55 | [[models]] 56 | model_name = "ROG Ally RC71L_RC71L" 57 | min_tdp = 5.0 58 | max_tdp = 30.0 59 | max_boost = 10.0 60 | 61 | [[models]] 62 | model_name = "ROG Ally RC71L" 63 | min_tdp = 5.0 64 | max_tdp = 30.0 65 | max_boost = 10.0 66 | 67 | [[models]] 68 | model_name = "ROG Ally X RC72LA_RC72LA" 69 | min_tdp = 5.0 70 | max_tdp = 30.0 71 | max_boost = 10.0 72 | 73 | 74 | [[models]] 75 | model_name = "G1617-01" 76 | min_tdp = 5.0 77 | max_tdp = 28.0 78 | max_boost = 0.0 79 | 80 | [[models]] 81 | model_name = "G1617-02" 82 | min_tdp = 5.0 83 | max_tdp = 28.0 84 | max_boost = 0.0 85 | 86 | [[models]] 87 | model_name = "G1618-03" 88 | min_tdp = 5.0 89 | max_tdp = 28.0 90 | max_boost = 0.0 91 | 92 | [[models]] 93 | model_name = "G1618-04" 94 | min_tdp = 5.0 95 | max_tdp = 28.0 96 | max_boost = 0.0 97 | 98 | [[models]] 99 | model_name = "Claw A1M" 100 | min_tdp = 5.0 101 | max_tdp = 28.0 102 | max_boost = 5.0 103 | -------------------------------------------------------------------------------- /rootfs/usr/share/powerstation/platform/intel_apu_database.toml: -------------------------------------------------------------------------------- 1 | [[models]] 2 | model_name = "11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz" 3 | min_tdp = 12.0 4 | max_tdp = 28.0 5 | max_boost = 2.0 6 | 7 | [[models]] 8 | model_name = "11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz" 9 | min_tdp = 12.0 10 | max_tdp = 28.0 11 | max_boost = 2.0 12 | 13 | [[models]] 14 | model_name = "11th Gen Intel(R) Core(TM) i7-1195G7 @ 2.90GHz" 15 | min_tdp = 12.0 16 | max_tdp = 28.0 17 | max_boost = 2.0 18 | 19 | [[models]] 20 | model_name = "Intel(R) Core(TM) m3-8100Y CPU @ 1.10GHz" 21 | min_tdp = 3.0 22 | max_tdp = 8.0 23 | max_boost = 2.0 24 | 25 | [[models]] 26 | model_name = "Intel(R) Core(TM) m3-7Y30 CPU @ 1.00GHz" 27 | min_tdp = 3.0 28 | max_tdp = 7.0 29 | max_boost = 2.0 30 | 31 | [[models]] 32 | model_name = "Intel(R) Core(TM) Ultra 7 258V" 33 | min_tdp = 5.0 34 | max_tdp = 30.0 35 | max_boost = 2.0 36 | -------------------------------------------------------------------------------- /src/constants.rs: -------------------------------------------------------------------------------- 1 | pub const BUS_NAME: &str = "org.shadowblip.PowerStation"; 2 | pub const PREFIX: &str = "/org/shadowblip/Performance"; 3 | pub const CPU_PATH: &str = "/org/shadowblip/Performance/CPU"; 4 | pub const GPU_PATH: &str = "/org/shadowblip/Performance/GPU"; 5 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use constants::PREFIX; 2 | use simple_logger::SimpleLogger; 3 | use std::{error::Error, future::pending}; 4 | use zbus::fdo::ObjectManager; 5 | use zbus::Connection; 6 | 7 | use crate::constants::{BUS_NAME, CPU_PATH, GPU_PATH}; 8 | use crate::dbus::gpu::{get_connectors, get_gpus, GPUBus}; 9 | use crate::performance::{cpu::cpu_features, gpu::dbus}; 10 | 11 | mod constants; 12 | mod performance; 13 | 14 | #[tokio::main(flavor = "multi_thread", worker_threads = 2)] 15 | async fn main() -> Result<(), Box> { 16 | SimpleLogger::new().init().unwrap(); 17 | const VERSION: &str = env!("CARGO_PKG_VERSION"); 18 | log::info!("Starting PowerStation v{}", VERSION); 19 | 20 | // Discover all CPUs 21 | let cpu = cpu_features::Cpu::new(); 22 | let cores = cpu_features::get_cores(); 23 | 24 | // Configure the connection 25 | let connection = Connection::system().await?; 26 | 27 | // Create an ObjectManager to signal when objects are added/removed 28 | let object_manager = ObjectManager {}; 29 | let object_manager_path = String::from(PREFIX); 30 | connection 31 | .object_server() 32 | .at(object_manager_path, object_manager) 33 | .await?; 34 | 35 | // Generate CPU objects to serve 36 | connection.object_server().at(CPU_PATH, cpu).await?; 37 | for core in cores { 38 | let core_path = format!("{0}/Core{1}", CPU_PATH, core.number()); 39 | connection.object_server().at(core_path, core).await?; 40 | } 41 | 42 | // Discover all GPUs and Generate GPU objects to serve 43 | let mut gpu_obj_paths: Vec = Vec::new(); 44 | for mut card in get_gpus().await { 45 | // Build the DBus object path for this card 46 | let gpu_name = card.name().await; 47 | let card_name = gpu_name.as_str().title(); 48 | let gpu_path = card.gpu_path().await; 49 | gpu_obj_paths.push(gpu_path.clone()); 50 | 51 | // Get the TDP interface from the card and serve it on DBus 52 | match card.get_tdp_interface().await { 53 | Some(tdp) => { 54 | log::debug!("Discovered TDP interface on card: {}", card_name); 55 | connection.object_server().at(gpu_path.clone(), tdp).await?; 56 | } 57 | None => { 58 | log::warn!("Card {} does not have a TDP interface", card_name); 59 | } 60 | } 61 | 62 | // Get GPU connectors from the card and serve them on DBus 63 | let mut connector_paths: Vec = Vec::new(); 64 | let connectors = get_connectors(gpu_name); 65 | for connector in connectors { 66 | let name = connector.name.clone().replace('-', "/"); 67 | let port_path = format!("{0}/{1}", gpu_path, name); 68 | connector_paths.push(port_path.clone()); 69 | log::debug!("Discovered connector on {}: {}", card_name, port_path); 70 | connection.object_server().at(port_path, connector).await?; 71 | } 72 | card.set_connector_paths(connector_paths).await; 73 | 74 | // Serve the GPU interface on DBus 75 | connection 76 | .object_server() 77 | .at(gpu_path.clone(), card) 78 | .await?; 79 | } 80 | 81 | // Create a GPU Bus instance which allows card enumeration 82 | let gpu_bus = GPUBus::new(gpu_obj_paths); 83 | connection.object_server().at(GPU_PATH, gpu_bus).await?; 84 | 85 | // Request a name 86 | connection.request_name(BUS_NAME).await?; 87 | 88 | // Do other things or go to wait forever 89 | pending::<()>().await; 90 | 91 | Ok(()) 92 | } 93 | 94 | trait TitleCase { 95 | fn title(&self) -> String; 96 | } 97 | 98 | impl TitleCase for &str { 99 | fn title(&self) -> String { 100 | if !self.is_ascii() || self.is_empty() { 101 | return String::from(*self); 102 | } 103 | let (head, tail) = self.split_at(1); 104 | head.to_uppercase() + tail 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/performance/cpu/core.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | fs::{self, OpenOptions}, 3 | io::Write, 4 | }; 5 | use tokio::io::AsyncWriteExt; 6 | use zbus::fdo; 7 | use zbus_macros::dbus_interface; 8 | 9 | // Instance of a single CPU core 10 | #[derive(Debug)] 11 | pub struct CPUCore { 12 | // CPU core number 13 | pub number: u32, 14 | // sysfs path to the CPU core 15 | // E.g. /sys/bus/cpu/devices/cpu{} 16 | pub path: String, 17 | } 18 | 19 | impl CPUCore { 20 | pub fn new(number: u32, path: String) -> CPUCore { 21 | CPUCore { number, path } 22 | } 23 | 24 | /// Asyncronously set the core to online 25 | pub async fn set_online_async(&self, enabled: bool) -> Result<(), std::io::Error> { 26 | let enabled_str = if enabled { "enabled" } else { "disabled" }; 27 | log::info!("Setting core {} to {}", self.number, enabled_str); 28 | let status = if enabled { "1" } else { "0" }; 29 | if self.number == 0 { 30 | return Ok(()); 31 | } 32 | 33 | // Open the sysfs file to write to 34 | let path = format!("{0}/online", self.path); 35 | let mut options = tokio::fs::OpenOptions::new(); 36 | let file = options.write(true).open(path); 37 | 38 | // Write the value 39 | file.await?.write_all(status.as_bytes()).await?; 40 | 41 | Ok(()) 42 | } 43 | } 44 | 45 | #[dbus_interface(name = "org.shadowblip.CPU.Core")] 46 | impl CPUCore { 47 | // Returns the core number of the CPU core 48 | #[dbus_interface(property)] 49 | pub fn number(&self) -> u32 { 50 | self.number 51 | } 52 | 53 | // Returns the core ID of the CPU core. This ID will be identical for 54 | // hyperthread cores. 55 | #[dbus_interface(property)] 56 | pub fn core_id(&self) -> fdo::Result { 57 | let path = format!("{0}/topology/core_id", self.path); 58 | let result = fs::read_to_string(path); 59 | let id = result 60 | // convert the std::io::Error to a zbus::fdo::Error 61 | .map_err(|err| fdo::Error::IOError(err.to_string()))? 62 | .trim() 63 | .to_lowercase(); 64 | 65 | // Convert the ID to an integer 66 | let id = id 67 | .parse::() 68 | // convert the ParseIntError to a zbus::fdo::Error 69 | .map_err(|err| fdo::Error::Failed(err.to_string()))?; 70 | 71 | Ok(id) 72 | } 73 | 74 | // Returns true if the given core is online 75 | #[dbus_interface(property)] 76 | pub fn online(&self) -> fdo::Result { 77 | if self.number == 0 { 78 | return Ok(true); 79 | } 80 | let path = format!("{0}/online", self.path); 81 | let result = fs::read_to_string(path); 82 | let status = result 83 | // convert the std::io::Error to a zbus::fdo::Error 84 | .map_err(|err| fdo::Error::IOError(err.to_string()))? 85 | .trim() 86 | .to_lowercase(); 87 | 88 | Ok(status == "1" || status == "on") 89 | } 90 | 91 | // Sets the given core to online 92 | #[dbus_interface(property)] 93 | pub fn set_online(&mut self, enabled: bool) -> fdo::Result<()> { 94 | let enabled_str = if enabled { "enabled" } else { "disabled" }; 95 | log::info!("Setting core {} to {}", self.number, enabled_str); 96 | let status = if enabled { "1" } else { "0" }; 97 | if self.number == 0 { 98 | return Ok(()); 99 | } 100 | 101 | // Open the sysfs file to write to 102 | let path = format!("{0}/online", self.path); 103 | let file = OpenOptions::new().write(true).open(path); 104 | 105 | // Write the value 106 | file 107 | // convert the std::io::Error to a zbus::fdo::Error 108 | .map_err(|err| fdo::Error::Failed(err.to_string()))? 109 | .write_all(status.as_bytes()) 110 | // convert the std::io::Error to a zbus::fdo::Error 111 | .map_err(|err| fdo::Error::IOError(err.to_string()))?; 112 | 113 | Ok(()) 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/performance/cpu/cpu_features.rs: -------------------------------------------------------------------------------- 1 | use std::collections::HashMap; 2 | use std::{fs::OpenOptions, io::Write}; 3 | use tokio::fs; 4 | use zbus::fdo; 5 | use zbus::zvariant::ObjectPath; 6 | use zbus_macros::dbus_interface; 7 | 8 | use crate::constants::CPU_PATH; 9 | use crate::performance::cpu::core::CPUCore; 10 | 11 | // Path to discover the number of CPUs the system has 12 | const CPUID_PATH: &str = "/sys/bus/cpu/devices"; 13 | const SMT_PATH: &str = "/sys/devices/system/cpu/smt/control"; 14 | const BOOST_PATH: &str = "/sys/devices/system/cpu/cpufreq/boost"; 15 | 16 | // Instance of the CPU on the host machine 17 | pub struct Cpu { 18 | core_map: HashMap>, 19 | core_count: u32, 20 | } 21 | 22 | impl Cpu { 23 | // Returns a new CPU instance 24 | pub fn new() -> Cpu { 25 | // Create a hashmap to organize the cores by their core ID 26 | let mut core_map: HashMap> = HashMap::new(); 27 | let mut cores = get_cores(); 28 | 29 | // Ensure SMT is enabled 30 | let file = OpenOptions::new().write(true).open(SMT_PATH); 31 | let _ = file.unwrap().write_all("on".as_bytes()); 32 | 33 | // Ensure all cores are online 34 | for core in cores.iter_mut() { 35 | let _ = core.set_online(true); 36 | } 37 | 38 | // Organize cores by core id 39 | let mut core_count = 0; 40 | for core in cores { 41 | core_count += 1; 42 | let core_id = core.core_id().unwrap(); 43 | core_map.entry(core_id).or_insert_with(|| { 44 | let list: Vec = Vec::new(); 45 | list 46 | }); 47 | 48 | let list = core_map.get_mut(&core_id).unwrap(); 49 | list.push(core); 50 | } 51 | log::info!("Core Map: {:?}", core_map); 52 | 53 | Cpu { 54 | core_map, 55 | core_count, 56 | } 57 | } 58 | } 59 | 60 | #[dbus_interface(name = "org.shadowblip.CPU")] 61 | impl Cpu { 62 | // Returns whether or not boost is enabled 63 | #[dbus_interface(property)] 64 | pub async fn boost_enabled(&self) -> fdo::Result { 65 | if !has_feature("cpb".to_string()).await? { 66 | return Ok(false); 67 | } 68 | let result = fs::read_to_string(BOOST_PATH); 69 | let status = result 70 | .await 71 | // convert the std::io::Error to a zbus::fdo::Error 72 | .map_err(|err| fdo::Error::IOError(err.to_string()))? 73 | .trim() 74 | .to_lowercase(); 75 | 76 | Ok(status == "1" || status == "on") 77 | } 78 | 79 | // Set whether or not boost is enabled 80 | #[dbus_interface(property)] 81 | pub async fn set_boost_enabled(&mut self, enabled: bool) -> fdo::Result<()> { 82 | log::info!("Setting boost enabled to {}", enabled); 83 | let status = if enabled { "1" } else { "0" }; 84 | 85 | // Open the sysfs file to write to 86 | let file = OpenOptions::new().write(true).open(BOOST_PATH); 87 | 88 | // Write the value 89 | file 90 | // convert the std::io::Error to a zbus::fdo::Error 91 | .map_err(|err| fdo::Error::Failed(err.to_string()))? 92 | .write_all(status.as_bytes()) 93 | // convert the std::io::Error to a zbus::fdo::Error 94 | .map_err(|err| fdo::Error::IOError(err.to_string()))?; 95 | 96 | Ok(()) 97 | } 98 | 99 | // Returns whether or not SMT is currently enabled 100 | #[dbus_interface(property)] 101 | pub async fn smt_enabled(&self) -> fdo::Result { 102 | if !has_feature("ht".to_string()).await? { 103 | return Ok(false); 104 | } 105 | let result = fs::read_to_string(SMT_PATH); 106 | let status = result 107 | .await 108 | // convert the std::io::Error to a zbus::fdo::Error 109 | .map_err(|err| fdo::Error::IOError(err.to_string()))? 110 | .trim() 111 | .to_lowercase(); 112 | 113 | Ok(status == "1" || status == "on") 114 | } 115 | 116 | // Set whether or not SMT is enabled 117 | #[dbus_interface(property)] 118 | pub async fn set_smt_enabled(&mut self, enabled: bool) -> fdo::Result<()> { 119 | log::info!("Setting smt enabled to {}", enabled); 120 | let status = if enabled { "on" } else { "off" }; 121 | 122 | // Open the sysfs file to write to 123 | let file = OpenOptions::new().write(true).open(SMT_PATH); 124 | 125 | // Write the value 126 | file 127 | // convert the std::io::Error to a zbus::fdo::Error 128 | .map_err(|err| fdo::Error::Failed(err.to_string()))? 129 | .write_all(status.as_bytes()) 130 | // convert the std::io::Error to a zbus::fdo::Error 131 | .map_err(|err| fdo::Error::IOError(err.to_string()))?; 132 | 133 | Ok(()) 134 | } 135 | 136 | // Returns a list of features that the CPU supports 137 | #[dbus_interface(property)] 138 | pub async fn features(&self) -> fdo::Result> { 139 | get_features().await 140 | } 141 | 142 | /// Returns the total number of CPU cores detected 143 | #[dbus_interface(property)] 144 | pub async fn cores_count(&self) -> fdo::Result { 145 | Ok(self.core_count) 146 | } 147 | 148 | #[dbus_interface(property)] 149 | pub async fn cores_enabled(&self) -> fdo::Result { 150 | let mut count = 0; 151 | for core_list in self.core_map.values() { 152 | for core in core_list { 153 | let is_online = core.online()?; 154 | if is_online { 155 | count += 1; 156 | } 157 | } 158 | } 159 | Ok(count) 160 | } 161 | 162 | #[dbus_interface(property)] 163 | pub async fn set_cores_enabled(&mut self, num: u32) -> fdo::Result<()> { 164 | log::info!("Setting core count to {}", num); 165 | if num < 1 { 166 | return Err(fdo::Error::InvalidArgs(String::from( 167 | "Cowardly refusing to set core count to 0", 168 | ))); 169 | } 170 | 171 | let core_count = self.core_count; 172 | if num > core_count { 173 | log::warn!( 174 | "Unable to set enabled cores to {}. Maximum core count is {}. Enabling all cores.", 175 | num, 176 | core_count 177 | ); 178 | } 179 | let smt_enabled = self.smt_enabled().await?; 180 | 181 | // If SMT is not enabled and the given core number is greater than what 182 | // cores would be available, then just set it to half the core count 183 | let num = if !smt_enabled && num > (core_count / 2) { 184 | log::warn!( 185 | "Unable to set enabled cores to {} while SMT is disabled. Enabling all physical cores.", 186 | num 187 | ); 188 | core_count / 2 189 | } else { 190 | num 191 | }; 192 | 193 | // Collect all core IDs from the core map 194 | let mut core_ids = self.core_map.keys().cloned().collect::>(); 195 | core_ids.sort(); 196 | 197 | // Enable/disable cores based on their hyper-threaded sibling 198 | let mut enabled_count = 1; 199 | for core_id in core_ids { 200 | let core_list = self.core_map.get_mut(&core_id).unwrap(); 201 | let mut is_physical = true; 202 | for core in core_list.iter_mut() { 203 | if core.number == 0 { 204 | is_physical = false; 205 | continue; 206 | } 207 | if !smt_enabled && !is_physical { 208 | log::info!("Ignoring core {} while SMT is disabled.", core.number); 209 | continue; 210 | } 211 | let should_enable = enabled_count < num; 212 | if should_enable { 213 | enabled_count += 1; 214 | } 215 | core.set_online_async(should_enable) 216 | .await 217 | .map_err(|err| fdo::Error::IOError(err.to_string()))?; 218 | is_physical = false; 219 | } 220 | } 221 | 222 | Ok(()) 223 | } 224 | 225 | /// Returns a list of DBus paths to all CPU cores 226 | pub async fn enumerate_cores(&mut self) -> fdo::Result> { 227 | let mut paths: Vec = Vec::new(); 228 | 229 | for i in 0..self.core_count { 230 | let path = format!("{}/Core{1}", CPU_PATH, i); 231 | let path = ObjectPath::from_string_unchecked(path); 232 | paths.push(path); 233 | } 234 | 235 | Ok(paths) 236 | } 237 | 238 | /// Returns true if the CPU has the given feature flag. 239 | pub async fn has_feature(&mut self, flag: String) -> fdo::Result { 240 | has_feature(flag).await 241 | } 242 | } 243 | 244 | // Returns true if the CPU has the given feature flag. 245 | async fn has_feature(flag: String) -> fdo::Result { 246 | let features = get_features(); 247 | Ok(features.await?.contains(&flag)) 248 | } 249 | 250 | // Returns a list of features that the CPU supports 251 | async fn get_features() -> fdo::Result> { 252 | let mut features: Vec = Vec::new(); 253 | 254 | // Read the data from cpuinfo 255 | let path = "/proc/cpuinfo"; 256 | let result = fs::read_to_string(path); 257 | let content = result 258 | .await 259 | // convert the std::io::Error to a zbus::fdo::Error 260 | .map_err(|err| fdo::Error::IOError(err.to_string()))?; 261 | 262 | // Parse the contents to find the flags 263 | for line in content.split('\n') { 264 | if !line.starts_with("flags") { 265 | continue; 266 | } 267 | // Split the 'flags' line to get the actual CPU flags 268 | let parts = line.split(':'); 269 | for part in parts { 270 | // Only parse the right side of the ":" 271 | if part.starts_with("flags") { 272 | continue; 273 | } 274 | let flags = part.trim().split(' '); 275 | for flag in flags { 276 | features.push(flag.to_string()); 277 | } 278 | } 279 | break; 280 | } 281 | 282 | Ok(features) 283 | } 284 | 285 | // Returns a list of all detected cores 286 | pub fn get_cores() -> Vec { 287 | let mut cores: Vec = Vec::new(); 288 | let paths = std::fs::read_dir(CPUID_PATH).unwrap(); 289 | for (i, path) in paths.enumerate() { 290 | log::info!("Discovered core: {}", path.unwrap().path().display()); 291 | let core_path = format!("/sys/bus/cpu/devices/cpu{0}", i); 292 | let core = CPUCore::new(i as u32, core_path); 293 | cores.push(core); 294 | } 295 | 296 | cores 297 | } 298 | -------------------------------------------------------------------------------- /src/performance/cpu/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod core; 2 | pub mod cpu_features; 3 | -------------------------------------------------------------------------------- /src/performance/gpu/acpi/firmware.rs: -------------------------------------------------------------------------------- 1 | use crate::performance::gpu::tdp::{TDPError, TDPResult}; 2 | 3 | use std::fs; 4 | 5 | const PLATFORM_PROFILE_PATH: &str = "/sys/firmware/acpi/platform_profile"; 6 | const PLATFORM_PROFILES_AVAIAL_PATH: &str = "/sys/firmware/acpi/platform_profile_choices"; 7 | 8 | /// Implementation of acpi sysfs 9 | pub struct Acpi {} 10 | 11 | impl Acpi { 12 | /// Check if ACPI supports platform profiles on this device 13 | pub async fn new() -> Option { 14 | if fs::metadata(PLATFORM_PROFILE_PATH).is_err() 15 | || fs::metadata(PLATFORM_PROFILES_AVAIAL_PATH).is_err() 16 | { 17 | return None; 18 | } 19 | Some(Self {}) 20 | } 21 | 22 | /// Reads the currently set power profile 23 | pub async fn power_profile(&self) -> TDPResult { 24 | match fs::read(PLATFORM_PROFILE_PATH) { 25 | Ok(data) => { 26 | let profile = match String::from_utf8(data) { 27 | Ok(profile) => profile.split_whitespace().collect(), 28 | Err(e) => { 29 | return Err(TDPError::IOError(format!( 30 | "Failed to convert utf8 data to string: {e:?}" 31 | ))) 32 | } 33 | }; 34 | 35 | log::info!("Platform profile is currently set to {profile}"); 36 | Ok(profile) 37 | } 38 | Err(e) => Err(TDPError::IOError(format!( 39 | "Failed to read platform profile: {e:?}" 40 | ))), 41 | } 42 | } 43 | 44 | /// Returns a list of valid power profiles for this interface 45 | pub async fn power_profiles_available(&self) -> TDPResult> { 46 | match fs::read(PLATFORM_PROFILES_AVAIAL_PATH) { 47 | Ok(data) => { 48 | let profiles_raw = match String::from_utf8(data) { 49 | Ok(profile) => profile, 50 | Err(e) => { 51 | return Err(TDPError::IOError(format!( 52 | "Failed to convert utf8 data to string: {e:?}" 53 | ))) 54 | } 55 | }; 56 | let mut profiles = Vec::new(); 57 | for profile in profiles_raw.split_whitespace() { 58 | profiles.push(profile.to_string()); 59 | } 60 | 61 | log::info!("Available platform profiles: {profiles:?}"); 62 | Ok(profiles) 63 | } 64 | Err(e) => Err(TDPError::IOError(format!( 65 | "Failed to read platform profile: {e:?}" 66 | ))), 67 | } 68 | } 69 | 70 | /// Sets the power profile to the given value 71 | pub async fn set_power_profile(&mut self, profile: String) -> TDPResult<()> { 72 | // Get the current profile so we can check if it needs to be set. 73 | let current = self.power_profile().await?; 74 | if current == profile { 75 | return Ok(()); 76 | } 77 | 78 | let valid_profiles = self.power_profiles_available().await?; 79 | //TODO: This supports a legacy interface from when only RyzenAdj was supported. Once 80 | //OpenGamepadUI is updated to use the new power_profiles_available methods, switch to 81 | //only returning and error here. 82 | if !valid_profiles.contains(&profile) { 83 | log::warn!("Incompatible profile requested: {profile}. Attempting to translate to valid profile."); 84 | match profile.as_str() { 85 | "max-performance" => match fs::write(PLATFORM_PROFILE_PATH, "performance") { 86 | Ok(_) => { 87 | log::info!("Set platform perfomance profile to performance"); 88 | return Ok(()); 89 | } 90 | Err(e) => { 91 | return Err(TDPError::IOError(format!( 92 | "Failed to set power profile: {e:?}" 93 | ))) 94 | } 95 | }, 96 | "power-saving" => match fs::write(PLATFORM_PROFILE_PATH, "balanced") { 97 | Ok(_) => { 98 | log::info!("Set platform perfomance profile to balanced"); 99 | return Ok(()); 100 | } 101 | Err(e) => { 102 | return Err(TDPError::IOError(format!( 103 | "Failed to set power profile: {e:?}" 104 | ))) 105 | } 106 | }, 107 | _ => { 108 | return Err(TDPError::InvalidArgument(format!( 109 | "{profile} is not a valid profile for Asus WMI." 110 | ))) 111 | } 112 | }; 113 | }; 114 | 115 | match fs::write(PLATFORM_PROFILE_PATH, profile.clone()) { 116 | Ok(_) => { 117 | log::info!("Set platform perfomance profile to {profile}"); 118 | Ok(()) 119 | } 120 | Err(e) => Err(TDPError::IOError(format!( 121 | "Failed to set power profile: {e:?}" 122 | ))), 123 | } 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /src/performance/gpu/acpi/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod firmware; 2 | -------------------------------------------------------------------------------- /src/performance/gpu/amd/amdgpu.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | fs::{self, OpenOptions}, 3 | io::Write, 4 | sync::Arc, 5 | }; 6 | 7 | use tokio::sync::Mutex; 8 | 9 | use crate::constants::GPU_PATH; 10 | use crate::performance::gpu::{ 11 | dbus::devices::TDPDevices, 12 | interface::{GPUDevice, GPUError, GPUResult}, 13 | }; 14 | 15 | use super::tdp::Tdp; 16 | 17 | #[derive(Debug, Clone)] 18 | pub struct AmdGpu { 19 | pub name: String, 20 | pub path: String, 21 | pub class: String, 22 | pub class_id: String, 23 | pub vendor: String, 24 | pub vendor_id: String, 25 | pub device: String, 26 | pub device_id: String, 27 | //pub device_type: String, 28 | pub subdevice: String, 29 | pub subdevice_id: String, 30 | pub subvendor_id: String, 31 | pub revision_id: String, 32 | } 33 | 34 | impl GPUDevice for AmdGpu { 35 | async fn get_gpu_path(&self) -> String { 36 | format!("{0}/{1}", GPU_PATH, self.name().await) 37 | } 38 | 39 | /// Returns the TDP DBus interface for this GPU 40 | async fn get_tdp_interface(&self) -> Option>> { 41 | match self.class.as_str() { 42 | "integrated" => Some(Arc::new(Mutex::new(TDPDevices::Amd( 43 | Tdp::new(self.path.as_str(), self.device_id.as_str()).await, 44 | )))), 45 | _ => None, 46 | } 47 | } 48 | 49 | async fn name(&self) -> String { 50 | self.name.clone() 51 | } 52 | 53 | async fn path(&self) -> String { 54 | self.path.clone() 55 | } 56 | 57 | async fn class(&self) -> String { 58 | self.class.clone() 59 | } 60 | 61 | async fn class_id(&self) -> String { 62 | self.class_id.clone() 63 | } 64 | 65 | async fn vendor(&self) -> String { 66 | self.vendor.clone() 67 | } 68 | 69 | async fn vendor_id(&self) -> String { 70 | self.vendor_id.clone() 71 | } 72 | 73 | async fn device(&self) -> String { 74 | self.device.clone() 75 | } 76 | 77 | async fn device_id(&self) -> String { 78 | self.device_id.clone() 79 | } 80 | 81 | async fn subdevice(&self) -> String { 82 | self.subdevice.clone() 83 | } 84 | 85 | async fn subdevice_id(&self) -> String { 86 | self.subdevice_id.clone() 87 | } 88 | 89 | async fn subvendor_id(&self) -> String { 90 | self.subvendor_id.clone() 91 | } 92 | 93 | async fn revision_id(&self) -> String { 94 | self.revision_id.clone() 95 | } 96 | 97 | async fn clock_limit_mhz_min(&self) -> GPUResult { 98 | let limits = get_clock_limits(self.path().await) 99 | .map_err(|err| GPUError::IOError(err.to_string()))?; 100 | 101 | let (min, _) = limits; 102 | Ok(min) 103 | } 104 | 105 | async fn clock_limit_mhz_max(&self) -> GPUResult { 106 | let limits = get_clock_limits(self.path().await) 107 | .map_err(|err| GPUError::IOError(err.to_string()))?; 108 | 109 | let (_, max) = limits; 110 | Ok(max) 111 | } 112 | 113 | async fn clock_value_mhz_min(&self) -> GPUResult { 114 | let values = get_clock_values(self.path().await) 115 | .map_err(|err| GPUError::IOError(err.to_string()))?; 116 | 117 | let (min, _) = values; 118 | Ok(min) 119 | } 120 | 121 | async fn set_clock_value_mhz_min(&mut self, value: f64) -> GPUResult<()> { 122 | // Build the clock command to send 123 | // https://www.kernel.org/doc/html/v5.9/gpu/amdgpu.html#pp-od-clk-voltage 124 | let command = format!("s 0 {}\n", value); 125 | 126 | // Open the sysfs file to write to 127 | let path = format!("{0}/{1}", self.path().await, "device/pp_od_clk_voltage"); 128 | let file = OpenOptions::new().write(true).open(path.clone()); 129 | 130 | // Write the value 131 | log::debug!( 132 | "Writing value '{}' to: {}", 133 | command.clone().trim(), 134 | path.clone() 135 | ); 136 | file.map_err(|err| GPUError::FailedOperation(err.to_string()))? 137 | .write_all(command.as_bytes()) 138 | .map_err(|err| GPUError::IOError(err.to_string()))?; 139 | 140 | // Write the "commit" command 141 | log::debug!("Writing value '{}' to: {}", "c", path.clone()); 142 | 143 | OpenOptions::new() 144 | .write(true) 145 | .open(path) 146 | .map_err(|err| GPUError::FailedOperation(err.to_string()))? 147 | .write_all("c\n".as_bytes()) 148 | .map_err(|err| GPUError::IOError(err.to_string())) 149 | } 150 | 151 | async fn clock_value_mhz_max(&self) -> GPUResult { 152 | let values = get_clock_values(self.path().await) 153 | .map_err(|err| GPUError::IOError(err.to_string()))?; 154 | 155 | let (_, max) = values; 156 | Ok(max) 157 | } 158 | 159 | async fn set_clock_value_mhz_max(&mut self, value: f64) -> GPUResult<()> { 160 | // Build the clock command to send 161 | // https://www.kernel.org/doc/html/v5.9/gpu/amdgpu.html#pp-od-clk-voltage 162 | let command = format!("s 1 {}\n", value); 163 | 164 | // Open the sysfs file to write to 165 | let path = format!("{0}/{1}", self.path().await, "device/pp_od_clk_voltage"); 166 | let file = OpenOptions::new().write(true).open(path.clone()); 167 | 168 | // Write the value 169 | file.map_err(|err| GPUError::FailedOperation(err.to_string()))? 170 | .write_all(command.as_bytes()) 171 | .map_err(|err| GPUError::IOError(err.to_string()))?; 172 | 173 | // Write the "commit" command 174 | OpenOptions::new() 175 | .write(true) 176 | .open(path) 177 | .map_err(|err| GPUError::FailedOperation(err.to_string()))? 178 | .write_all("c\n".as_bytes()) 179 | .map_err(|err| GPUError::IOError(err.to_string())) 180 | } 181 | 182 | async fn manual_clock(&self) -> GPUResult { 183 | let path = format!( 184 | "{0}/{1}", 185 | self.path().await, 186 | "device/power_dpm_force_performance_level" 187 | ); 188 | 189 | let result = fs::read_to_string(path); 190 | let status = result 191 | .map_err(|err| GPUError::IOError(err.to_string()))? 192 | .trim() 193 | .to_lowercase(); 194 | 195 | Ok(status == "manual") 196 | } 197 | 198 | async fn set_manual_clock(&mut self, enabled: bool) -> GPUResult<()> { 199 | let status = if enabled { "manual" } else { "auto" }; 200 | 201 | // Open the sysfs file to write to 202 | let path = format!( 203 | "{0}/{1}", 204 | self.path().await, 205 | "device/power_dpm_force_performance_level" 206 | ); 207 | 208 | // Write the value 209 | OpenOptions::new() 210 | .write(true) 211 | .open(path) 212 | .map_err(|err| GPUError::FailedOperation(err.to_string()))? 213 | .write_all(status.as_bytes()) 214 | .map_err(|err| GPUError::IOError(err.to_string())) 215 | } 216 | } 217 | 218 | /// Reads the pp_od_clk_voltage from sysfs and returns the OD_RANGE values. 219 | /// This file will be empty if not in "manual" for pp_od_performance_level. 220 | fn get_clock_limits(gpu_path: String) -> Result<(f64, f64), std::io::Error> { 221 | let path = format!("{0}/{1}", gpu_path, "device/pp_od_clk_voltage"); 222 | let result = fs::read_to_string(path); 223 | let result = result?; 224 | let lines = result.split('\n'); 225 | 226 | // Parse the output 227 | let mut min: Option = None; 228 | let mut max: Option = None; 229 | for line in lines { 230 | let mut parts = line.split_whitespace(); 231 | let part1 = parts.next(); 232 | if !part1.is_some_and(|part| part == "SCLK:") { 233 | continue; 234 | } 235 | 236 | let part2 = parts.next(); 237 | if part2.is_none() { 238 | continue; 239 | } 240 | let parsed2 = part2.unwrap().trim_end_matches("Mhz").parse::(); 241 | if parsed2.is_err() { 242 | continue; 243 | } 244 | min = Some(parsed2.unwrap()); 245 | 246 | let part3 = parts.next(); 247 | if part3.is_none() { 248 | continue; 249 | } 250 | let parsed3 = part3.unwrap().trim_end_matches("Mhz").parse::(); 251 | if parsed3.is_err() { 252 | continue; 253 | } 254 | max = Some(parsed3.unwrap()); 255 | } 256 | 257 | if min.is_none() || max.is_none() { 258 | return Err(std::io::Error::new( 259 | std::io::ErrorKind::NotFound, 260 | "No limits found", 261 | )); 262 | } 263 | 264 | Ok((min.unwrap(), max.unwrap())) 265 | } 266 | 267 | /// Reads the pp_od_clk_voltage from sysfs and returns the OD_SCLK values. This file will 268 | /// be empty if not in "manual" for pp_od_performance_level. 269 | fn get_clock_values(gpu_path: String) -> Result<(f64, f64), std::io::Error> { 270 | let path = format!("{0}/{1}", gpu_path, "device/pp_od_clk_voltage"); 271 | let result = fs::read_to_string(path); 272 | let result = result?; 273 | let lines = result.split('\n'); 274 | 275 | // Parse the output 276 | let mut min: Option = None; 277 | let mut max: Option = None; 278 | for line in lines { 279 | let mut parts = line.split_whitespace(); 280 | let part1 = parts.next(); 281 | if !part1.is_some_and(|part| part == "0:" || part == "1:") { 282 | continue; 283 | } 284 | let kind = part1.unwrap(); 285 | 286 | let part2 = parts.next(); 287 | if part2.is_none() { 288 | continue; 289 | } 290 | let parsed2 = part2.unwrap().trim_end_matches("Mhz").parse::(); 291 | if parsed2.is_err() { 292 | continue; 293 | } 294 | 295 | match kind { 296 | "0:" => min = Some(parsed2.unwrap()), 297 | "1:" => max = Some(parsed2.unwrap()), 298 | _ => continue, 299 | } 300 | } 301 | 302 | if min.is_none() || max.is_none() { 303 | return Err(std::io::Error::new( 304 | std::io::ErrorKind::NotFound, 305 | "No limits found", 306 | )); 307 | } 308 | 309 | Ok((min.unwrap(), max.unwrap())) 310 | } 311 | -------------------------------------------------------------------------------- /src/performance/gpu/amd/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod amdgpu; 2 | pub mod ryzenadj; 3 | pub mod tdp; 4 | -------------------------------------------------------------------------------- /src/performance/gpu/amd/ryzenadj.rs: -------------------------------------------------------------------------------- 1 | #![cfg(target_arch = "x86_64")] 2 | use std::error::Error; 3 | 4 | use libryzenadj::RyzenAdj; 5 | 6 | use crate::performance::gpu::{ 7 | platform::hardware::Hardware, 8 | tdp::{HardwareAccess, TDPDevice, TDPError, TDPResult}, 9 | }; 10 | 11 | /// Steam Deck GPU ID 12 | const DEV_ID_VANGOGH: &str = "163f"; 13 | const DEV_ID_SEPHIROTH: &str = "1435"; 14 | 15 | /// Implementation of TDP control for AMD GPUs 16 | pub struct RyzenAdjTdp { 17 | //pub path: String, 18 | pub device_id: String, 19 | pub profile: String, 20 | ryzenadj: RyzenAdj, 21 | pub unsupported_stapm_limit: f32, 22 | pub unsupported_ppt_limit_fast: f32, 23 | pub unsupported_thm_limit: f32, 24 | // We need Hardware for the TDPDevice trait's default methods 25 | hardware: Option, 26 | } 27 | 28 | impl HardwareAccess for RyzenAdjTdp { 29 | fn hardware(&self) -> Option<&Hardware> { 30 | self.hardware.as_ref() 31 | } 32 | } 33 | 34 | unsafe impl Sync for RyzenAdjTdp {} // implementor (RyzenAdj) may be unsafe 35 | unsafe impl Send for RyzenAdjTdp {} // implementor (RyzenAdj) may be unsafe 36 | 37 | impl RyzenAdjTdp { 38 | /// Create a new TDP instance 39 | pub fn new(_path: String, device_id: String) -> Result> { 40 | // Currently there is no known way to read this value 41 | let profile = String::from("power-saving"); 42 | 43 | // Set fake TDP limits for GPUs that don't support ryzenadj monitoring (e.g. Steam Deck) 44 | let unsupported_stapm_limit: f32 = match device_id.as_str() { 45 | DEV_ID_VANGOGH => 12.0, 46 | DEV_ID_SEPHIROTH => 12.0, 47 | _ => 10.0, 48 | }; 49 | let unsupported_ppt_limit_fast: f32 = match device_id.as_str() { 50 | DEV_ID_VANGOGH => 15.0, 51 | DEV_ID_SEPHIROTH => 15.0, 52 | _ => 10.0, 53 | }; 54 | let unsupported_thm_limit: f32 = match device_id.as_str() { 55 | DEV_ID_VANGOGH => 95.0, 56 | DEV_ID_SEPHIROTH => 95.0, 57 | _ => 95.0, 58 | }; 59 | let ryzenadj = RyzenAdj::new().map_err(|err| err.to_string())?; 60 | 61 | // Get hardware instance for min/max TDP values 62 | let hardware = match Hardware::new() { 63 | Some(hardware) => { 64 | log::info!("Found Hardware interface for RyzenAdj TDP control"); 65 | Some(hardware) 66 | } 67 | None => None, 68 | }; 69 | 70 | Ok(RyzenAdjTdp { 71 | //path, 72 | device_id, 73 | profile, 74 | ryzenadj, 75 | unsupported_stapm_limit, 76 | unsupported_ppt_limit_fast, 77 | unsupported_thm_limit, 78 | hardware, 79 | }) 80 | } 81 | 82 | /// Returns true if ryzenadj cannot read values from the given GPU 83 | fn is_unsupported_gpu(&self) -> bool { 84 | matches!(self.device_id.as_str(), DEV_ID_VANGOGH | DEV_ID_SEPHIROTH) 85 | } 86 | 87 | /// Set the current Slow PPT limit using ryzenadj 88 | fn set_ppt_limit_slow(&mut self, value: u32) -> Result<(), String> { 89 | log::debug!("Setting slow ppt limit to {}", value); 90 | match self.ryzenadj.set_slow_limit(value) { 91 | Ok(x) => Ok(x), 92 | Err(e) => { 93 | let err = format!("Failed to set slow ppt limit: {}", e); 94 | log::error!("{}", err); 95 | Err(err) 96 | } 97 | } 98 | } 99 | 100 | // Get the PPT slow limit 101 | fn get_ppt_limit_slow(&self) -> Result { 102 | log::debug!("Getting ppt slow limit"); 103 | 104 | if let Err(e) = self.ryzenadj.refresh() { 105 | log::error!("Failed to refresh ryzenadj: {}", e); 106 | } 107 | 108 | match self.ryzenadj.get_slow_limit() { 109 | Ok(x) => Ok(x), 110 | Err(e) => { 111 | let err = format!("Failed to get ppt slow limit: {}", e); 112 | log::error!("{}", err); 113 | Err(err) 114 | } 115 | } 116 | } 117 | 118 | /// Set the current Fast PPT limit using ryzenadj 119 | fn set_ppt_limit_fast(&mut self, value: u32) -> Result<(), String> { 120 | log::debug!("Setting fast ppt limit to {}", value); 121 | match self.ryzenadj.set_fast_limit(value) { 122 | Ok(x) => { 123 | // Save the new value for APU's that can't read this attribute. 124 | if self.is_unsupported_gpu() { 125 | self.unsupported_ppt_limit_fast = value as f32; 126 | } 127 | Ok(x) 128 | } 129 | Err(e) => { 130 | let err = format!("Failed to set fast ppt limit: {}", e); 131 | log::error!("{}", err); 132 | Err(err) 133 | } 134 | } 135 | } 136 | 137 | /// Get the PPT fast limit 138 | fn _get_ppt_limit_fast(&self) -> Result { 139 | log::debug!("Getting ppt fast limit"); 140 | 141 | // Return what we last set the value to for APU's that can't read this 142 | // attribute. 143 | if self.is_unsupported_gpu() { 144 | return Ok(self.unsupported_ppt_limit_fast); 145 | } 146 | 147 | if let Err(e) = self.ryzenadj.refresh() { 148 | log::error!("Failed to refresh ryzenadj: {}", e); 149 | } 150 | 151 | // Get the fast limit from ryzenadj 152 | match self.ryzenadj.get_fast_limit() { 153 | Ok(x) => { 154 | log::debug!("Got fast limit: {}", x); 155 | Ok(x) 156 | } 157 | Err(e) => { 158 | let err = format!("Failed to get ppt fast limit: {}", e); 159 | log::error!("{}", err); 160 | Err(err) 161 | } 162 | } 163 | } 164 | 165 | /// Set the current TDP value using ryzenadj 166 | fn set_stapm_limit(&mut self, value: u32) -> Result<(), String> { 167 | log::debug!("Setting stapm limit to {}", value); 168 | match self.ryzenadj.set_stapm_limit(value) { 169 | Ok(x) => { 170 | log::debug!("Set stapm limit to {}", value); 171 | // Save the new value for APU's that can't read this attribute. 172 | if self.is_unsupported_gpu() { 173 | self.unsupported_stapm_limit = value as f32; 174 | } 175 | Ok(x) 176 | } 177 | Err(e) => { 178 | let err = format!("Failed to set stapm limit: {}", e); 179 | log::error!("{}", err); 180 | Err(err) 181 | } 182 | } 183 | } 184 | 185 | /// Returns the current TDP value using ryzenadj 186 | fn get_stapm_limit(&self) -> Result { 187 | log::debug!("Getting stapm limit"); 188 | 189 | // Return what we last set the value to for APU's that can't read this 190 | // attribute. 191 | if self.is_unsupported_gpu() { 192 | return Ok(self.unsupported_stapm_limit); 193 | } 194 | 195 | if let Err(e) = self.ryzenadj.refresh() { 196 | log::error!("Failed to refresh ryzenadj: {}", e); 197 | } 198 | 199 | // Get the value from ryzenadj 200 | match self.ryzenadj.get_stapm_limit() { 201 | Ok(x) => { 202 | log::debug!("Got stapm limit: {}", x); 203 | Ok(x) 204 | } 205 | Err(e) => { 206 | let err = format!("Failed to get stapm limit: {}", e); 207 | log::error!("{}", err); 208 | Err(err) 209 | } 210 | } 211 | } 212 | 213 | // Sets the thermal limit value using ryzenadj 214 | fn set_thm_limit(&mut self, value: u32) -> Result<(), String> { 215 | log::debug!("Setting thm limit to: {}", value); 216 | match self.ryzenadj.set_tctl_temp(value) { 217 | Ok(x) => { 218 | // Save the new value for APU's that can't read this attribute. 219 | if self.is_unsupported_gpu() { 220 | self.unsupported_thm_limit = value as f32; 221 | } 222 | Ok(x) 223 | } 224 | 225 | Err(e) => { 226 | let err = format!("Failed to set tctl limit: {}", e); 227 | log::error!("{}", err); 228 | Err(err) 229 | } 230 | } 231 | } 232 | 233 | /// Returns the current thermal limit value using ryzenadj 234 | fn get_thm_limit(&self) -> Result { 235 | log::debug!("Getting thm limit"); 236 | 237 | // Return what we last set the value to for APU's that can't read this 238 | // attribute. 239 | if self.is_unsupported_gpu() { 240 | return Ok(self.unsupported_thm_limit); 241 | } 242 | 243 | if let Err(e) = self.ryzenadj.refresh() { 244 | log::error!("Failed to refresh ryzenadj: {}", e); 245 | } 246 | 247 | // Get the value from ryzenadj 248 | match self.ryzenadj.get_tctl_temp() { 249 | Ok(x) => Ok(x), 250 | Err(e) => { 251 | let err = format!("Failed to get tctl temp: {}", e); 252 | log::error!("{}", err); 253 | Err(err) 254 | } 255 | } 256 | } 257 | 258 | /// Set the power profile to the given profile 259 | fn set_power_profile(&self, profile: String) -> Result<(), String> { 260 | log::debug!("Setting power profile"); 261 | match profile.as_str() { 262 | "power-saving" => self 263 | .ryzenadj 264 | .set_power_saving() 265 | .map_err(|err| err.to_string()), 266 | "max-performance" => self 267 | .ryzenadj 268 | .set_max_performance() 269 | .map_err(|err| err.to_string()), 270 | _ => Err(String::from( 271 | "Invalid power profile. Must be in [max-performance, power-saving]", 272 | )), 273 | } 274 | } 275 | } 276 | 277 | impl TDPDevice for RyzenAdjTdp { 278 | async fn tdp(&self) -> TDPResult { 279 | // Get the current stapm limit from ryzenadj 280 | match RyzenAdjTdp::get_stapm_limit(self) { 281 | Ok(result) => Ok(result.into()), 282 | Err(err) => Err(TDPError::FailedOperation(err.to_string())), 283 | } 284 | } 285 | 286 | async fn set_tdp(&mut self, value: f64) -> TDPResult<()> { 287 | log::debug!("Setting TDP to: {}", value); 288 | if value < 1.0 { 289 | log::warn!("Cowardly refusing to set TDP less than 1W"); 290 | return Err(TDPError::InvalidArgument(format!( 291 | "Cowardly refusing to set TDP less than 1W: provided {}W", 292 | value 293 | ))); 294 | } 295 | 296 | // Get the current boost value before updating the STAPM limit. We will 297 | // use this value to also adjust the Fast PPT Limit. 298 | let boost = match self.boost().await { 299 | Ok(boost) => boost, 300 | Err(e) => return Err(e), 301 | }; 302 | 303 | // Update the STAPM limit with the TDP value 304 | let limit: u32 = (value * 1000.0) as u32; 305 | RyzenAdjTdp::set_stapm_limit(self, limit).map_err(TDPError::FailedOperation)?; 306 | 307 | // Update the s/fppt values with the new TDP 308 | self.set_boost(boost).await?; 309 | 310 | Ok(()) 311 | } 312 | 313 | async fn boost(&self) -> TDPResult { 314 | let slow_ppt_limit = 315 | RyzenAdjTdp::get_ppt_limit_slow(self).map_err(TDPError::FailedOperation)? as f64; 316 | let stapm_limit = 317 | RyzenAdjTdp::get_stapm_limit(self).map_err(TDPError::FailedOperation)? as f64; 318 | 319 | // TODO: Is this a bug in ryzenadj? Sometimes it is ~0 320 | if slow_ppt_limit < 1.0 { 321 | log::warn!("Got a slow limit less than 1. Setting boost to 0"); 322 | return Ok(0.0); 323 | } 324 | 325 | let boost = slow_ppt_limit - stapm_limit; 326 | Ok(boost) 327 | } 328 | 329 | async fn set_boost(&mut self, value: f64) -> TDPResult<()> { 330 | log::debug!("Setting boost to: {}", value); 331 | if value < 0.0 { 332 | log::warn!("Cowardly refusing to set TDP Boost less than 0W"); 333 | return Err(TDPError::InvalidArgument(format!( 334 | "Cowardly refusing to set TDP Boost less than 0W: {}W provided", 335 | value 336 | ))); 337 | } 338 | 339 | // Get the STAPM Limit so we can calculate what S/FPPT limits to set. 340 | let stapm_limit = 341 | RyzenAdjTdp::get_stapm_limit(self).map_err(TDPError::FailedOperation)? as f64; 342 | 343 | // Set the new slow PPT limit 344 | let slow_ppt_limit = ((stapm_limit + value) * 1000.0) as u32; 345 | RyzenAdjTdp::set_ppt_limit_slow(self, slow_ppt_limit).map_err(TDPError::FailedOperation)?; 346 | 347 | // Set the new fast PPT limit 348 | let fast_ppt_limit = ((stapm_limit + value) * 1250.0) as u32; 349 | RyzenAdjTdp::set_ppt_limit_fast(self, fast_ppt_limit).map_err(TDPError::FailedOperation)?; 350 | 351 | Ok(()) 352 | } 353 | 354 | async fn thermal_throttle_limit_c(&self) -> TDPResult { 355 | let limit = RyzenAdjTdp::get_thm_limit(self) 356 | .map_err(|err| TDPError::FailedOperation(err.to_string()))?; 357 | Ok(limit.into()) 358 | } 359 | 360 | async fn set_thermal_throttle_limit_c(&mut self, limit: f64) -> TDPResult<()> { 361 | log::debug!("Setting thermal throttle limit to: {}", limit); 362 | let limit = limit as u32; 363 | RyzenAdjTdp::set_thm_limit(self, limit) 364 | .map_err(|err| TDPError::FailedOperation(err.to_string())) 365 | } 366 | 367 | async fn power_profile(&self) -> TDPResult { 368 | Ok(self.profile.clone()) 369 | } 370 | 371 | async fn set_power_profile(&mut self, profile: String) -> TDPResult<()> { 372 | log::debug!("Setting power profile to: {}", profile); 373 | RyzenAdjTdp::set_power_profile(self, profile.clone()) 374 | .map_err(|err| TDPError::FailedOperation(err.to_string()))?; 375 | self.profile = profile; 376 | Ok(()) 377 | } 378 | 379 | async fn power_profiles_available(&self) -> TDPResult> { 380 | Ok(vec![ 381 | "max-performance".to_string(), 382 | "power-saving".to_string(), 383 | ]) 384 | } 385 | } 386 | -------------------------------------------------------------------------------- /src/performance/gpu/amd/tdp.rs: -------------------------------------------------------------------------------- 1 | use crate::performance::gpu::{ 2 | acpi::firmware::Acpi, 3 | asus::asus_wmi::AsusWmi, 4 | platform::hardware::Hardware, 5 | tdp::{HardwareAccess, TDPDevice, TDPError, TDPResult}, 6 | }; 7 | 8 | #[cfg(target_arch = "x86_64")] 9 | use super::ryzenadj::RyzenAdjTdp; 10 | 11 | /// Implementation of TDP control for AMD GPUs 12 | pub struct Tdp { 13 | asus_wmi: Option, 14 | acpi: Option, 15 | #[cfg(target_arch = "x86_64")] 16 | ryzenadj: Option, 17 | hardware: Option, 18 | } 19 | 20 | // Implement HardwareAccess for Tdp 21 | impl HardwareAccess for Tdp { 22 | fn hardware(&self) -> Option<&Hardware> { 23 | self.hardware.as_ref() 24 | } 25 | } 26 | 27 | impl Tdp { 28 | pub async fn new(path: &str, device_id: &str) -> Tdp { 29 | let asus_wmi = match AsusWmi::new().await { 30 | Some(asus_wmi) => { 31 | log::info!("Found Asus WMI interface for TDP control"); 32 | Some(asus_wmi) 33 | } 34 | None => None, 35 | }; 36 | 37 | let acpi = match Acpi::new().await { 38 | Some(acpi) => { 39 | log::info!("Found ACPI interface for platform profile control"); 40 | Some(acpi) 41 | } 42 | None => None, 43 | }; 44 | 45 | #[cfg(target_arch = "x86_64")] 46 | let ryzenadj = match RyzenAdjTdp::new(path.to_string(), device_id.to_string()) { 47 | Ok(ryzenadj) => { 48 | log::info!("Found RyzenAdj interface for TDP control"); 49 | Some(ryzenadj) 50 | } 51 | Err(e) => { 52 | log::warn!("Failed to create Ryzenadj Instance: {e:?}"); 53 | None 54 | } 55 | }; 56 | 57 | let hardware = match Hardware::new() { 58 | Some(hardware) => { 59 | log::info!("Found Hardware interface for TDP control"); 60 | Some(hardware) 61 | } 62 | None => None, 63 | }; 64 | 65 | Tdp { 66 | asus_wmi, 67 | acpi, 68 | #[cfg(target_arch = "x86_64")] 69 | ryzenadj, 70 | hardware, 71 | } 72 | } 73 | } 74 | 75 | impl TDPDevice for Tdp { 76 | async fn tdp(&self) -> TDPResult { 77 | log::info!("Get TDP"); 78 | 79 | // TODO: set platform profile based on % of max TDP. 80 | if self.asus_wmi.is_some() { 81 | let asus_wmi = self.asus_wmi.as_ref().unwrap(); 82 | match asus_wmi.tdp().await { 83 | Ok(tdp) => { 84 | log::info!("TDP is currently {tdp}"); 85 | return Ok(tdp); 86 | } 87 | Err(e) => { 88 | log::warn!("Failed to read current TDP using Asus WMI: {e:?}"); 89 | } 90 | }; 91 | }; 92 | // TODO: set platform profile based on % of max TDP. 93 | #[cfg(target_arch = "x86_64")] 94 | if self.ryzenadj.is_some() { 95 | let ryzenadj = self.ryzenadj.as_ref().unwrap(); 96 | match ryzenadj.tdp().await { 97 | Ok(tdp) => { 98 | log::info!("TDP is currently {tdp}"); 99 | return Ok(tdp); 100 | } 101 | Err(e) => { 102 | log::warn!("Failed to read current TDP using RyzenAdj: {e:?}"); 103 | } 104 | }; 105 | }; 106 | Err(TDPError::FailedOperation( 107 | "No TDP Interface available to read TDP.".into(), 108 | )) 109 | } 110 | 111 | async fn set_tdp(&mut self, value: f64) -> TDPResult<()> { 112 | log::info!("Set TDP"); 113 | if self.asus_wmi.is_some() { 114 | let asus_wmi = self.asus_wmi.as_mut().unwrap(); 115 | match asus_wmi.set_tdp(value).await { 116 | Ok(_) => { 117 | log::info!("TDP set to {value}"); 118 | return Ok(()); 119 | } 120 | Err(e) => { 121 | log::warn!("Failed to set TDP using Asus WMI: {e:?}"); 122 | } 123 | }; 124 | }; 125 | #[cfg(target_arch = "x86_64")] 126 | if self.ryzenadj.is_some() { 127 | let ryzenadj = self.ryzenadj.as_mut().unwrap(); 128 | match ryzenadj.set_tdp(value).await { 129 | Ok(_) => { 130 | log::info!("TDP set to {value}"); 131 | return Ok(()); 132 | } 133 | Err(e) => { 134 | log::warn!("Failed to set TDP using RyzenAdj: {e:?}"); 135 | } 136 | }; 137 | }; 138 | Err(TDPError::FailedOperation( 139 | "No TDP Interface available to set TDP.".into(), 140 | )) 141 | } 142 | 143 | async fn boost(&self) -> TDPResult { 144 | log::info!("Get TDP Boost"); 145 | if self.asus_wmi.is_some() { 146 | let asus_wmi = self.asus_wmi.as_ref().unwrap(); 147 | match asus_wmi.boost().await { 148 | Ok(boost) => { 149 | log::info!("Boost is currently {boost}"); 150 | return Ok(boost); 151 | } 152 | Err(e) => { 153 | log::warn!("Failed to read current boost using Asus WMI: {e:?}"); 154 | } 155 | }; 156 | }; 157 | #[cfg(target_arch = "x86_64")] 158 | if self.ryzenadj.is_some() { 159 | let ryzenadj = self.ryzenadj.as_ref().unwrap(); 160 | match ryzenadj.boost().await { 161 | Ok(boost) => { 162 | log::info!("Boost is currently {boost}"); 163 | return Ok(boost); 164 | } 165 | Err(e) => { 166 | log::warn!("Failed to read current boost using RyzenAdj: {e:?}"); 167 | } 168 | }; 169 | }; 170 | Err(TDPError::FailedOperation( 171 | "No TDP Interface available to read boost.".into(), 172 | )) 173 | } 174 | 175 | async fn set_boost(&mut self, value: f64) -> TDPResult<()> { 176 | log::info!("Set TDP Boost"); 177 | if self.asus_wmi.is_some() { 178 | let asus_wmi = self.asus_wmi.as_mut().unwrap(); 179 | match asus_wmi.set_boost(value).await { 180 | Ok(_) => { 181 | log::info!("Boost set to {value}"); 182 | return Ok(()); 183 | } 184 | Err(e) => { 185 | log::warn!("Failed to set boost using Asus WMI: {e:?}"); 186 | } 187 | }; 188 | }; 189 | #[cfg(target_arch = "x86_64")] 190 | if self.ryzenadj.is_some() { 191 | let ryzenadj = self.ryzenadj.as_mut().unwrap(); 192 | match ryzenadj.set_boost(value).await { 193 | Ok(_) => { 194 | log::info!("Boost set to {value}"); 195 | return Ok(()); 196 | } 197 | Err(e) => { 198 | log::warn!("Failed to set boost using RyzenAdj: {e:?}"); 199 | } 200 | }; 201 | }; 202 | Err(TDPError::FailedOperation( 203 | "No TDP Interface available to set boost.".into(), 204 | )) 205 | } 206 | 207 | async fn thermal_throttle_limit_c(&self) -> TDPResult { 208 | log::info!("Get tctl limit"); 209 | #[cfg(target_arch = "x86_64")] 210 | if self.ryzenadj.is_some() { 211 | let ryzenadj = self.ryzenadj.as_ref().unwrap(); 212 | match ryzenadj.thermal_throttle_limit_c().await { 213 | Ok(limit) => { 214 | log::info!("Thermal throttle limit is currently {limit}"); 215 | return Ok(limit); 216 | } 217 | Err(e) => { 218 | log::warn!("Failed to read thermal trottle limit using RyzenAdj: {e:?}"); 219 | } 220 | }; 221 | }; 222 | Err(TDPError::FailedOperation( 223 | "No TDP Interface available to read thermal throttle limit.".into(), 224 | )) 225 | } 226 | 227 | async fn set_thermal_throttle_limit_c(&mut self, limit: f64) -> TDPResult<()> { 228 | log::info!("Set tctl limit"); 229 | #[cfg(target_arch = "x86_64")] 230 | if self.ryzenadj.is_some() { 231 | let ryzenadj = self.ryzenadj.as_mut().unwrap(); 232 | match ryzenadj.set_thermal_throttle_limit_c(limit).await { 233 | Ok(_) => { 234 | log::info!("Thermal throttle limit was set to {:e}", limit as i32); 235 | return Ok(()); 236 | } 237 | Err(e) => { 238 | log::warn!("Failed to set thermal trottle limit using RyzenAdj: {e:?}"); 239 | } 240 | }; 241 | }; 242 | Err(TDPError::FailedOperation( 243 | "No TDP Interface available to set thermal throttle limit.".into(), 244 | )) 245 | } 246 | 247 | async fn power_profile(&self) -> TDPResult { 248 | log::info!("Get power_profile"); 249 | if self.acpi.is_some() { 250 | let acpi = self.acpi.as_ref().unwrap(); 251 | match acpi.power_profile().await { 252 | Ok(profile) => { 253 | log::info!("Power profile is currently {profile}"); 254 | return Ok(profile); 255 | } 256 | Err(e) => { 257 | log::warn!("Failed to read power profile using ACPI: {e:?}"); 258 | } 259 | }; 260 | }; 261 | 262 | #[cfg(target_arch = "x86_64")] 263 | if self.ryzenadj.is_some() { 264 | let ryzenadj = self.ryzenadj.as_ref().unwrap(); 265 | match ryzenadj.power_profile().await { 266 | Ok(profile) => { 267 | log::info!("Power profile is currently {profile}"); 268 | return Ok(profile); 269 | } 270 | Err(e) => { 271 | log::warn!("Failed to read power profile using RyzenAdj: {e:?}"); 272 | } 273 | }; 274 | }; 275 | Err(TDPError::FailedOperation( 276 | "No TDP Interface available to read power profile.".into(), 277 | )) 278 | } 279 | 280 | async fn set_power_profile(&mut self, profile: String) -> TDPResult<()> { 281 | log::info!("Set power_profile"); 282 | if self.acpi.is_some() { 283 | let acpi = self.acpi.as_mut().unwrap(); 284 | match acpi.set_power_profile(profile.clone()).await { 285 | Ok(_) => { 286 | log::info!("Power profile was set to {profile}"); 287 | return Ok(()); 288 | } 289 | Err(e) => { 290 | log::warn!("Failed to set power profile using ACPI: {e:?}"); 291 | } 292 | }; 293 | }; 294 | 295 | #[cfg(target_arch = "x86_64")] 296 | if self.ryzenadj.is_some() { 297 | let ryzenadj = self.ryzenadj.as_mut().unwrap(); 298 | match ryzenadj.set_power_profile(profile.clone()).await { 299 | Ok(_) => { 300 | log::info!("Power profile was set to {profile}"); 301 | return Ok(()); 302 | } 303 | Err(e) => { 304 | log::warn!("Failed to set power profile using RyzenAdj: {e:?}"); 305 | } 306 | }; 307 | }; 308 | Err(TDPError::FailedOperation( 309 | "No TDP Interface available to set power profile.".into(), 310 | )) 311 | } 312 | 313 | async fn power_profiles_available(&self) -> TDPResult> { 314 | if self.acpi.is_some() { 315 | let acpi = self.acpi.as_ref().unwrap(); 316 | match acpi.power_profiles_available().await { 317 | Ok(profiles) => { 318 | log::info!("Available power profiles are {profiles:?}"); 319 | return Ok(profiles); 320 | } 321 | Err(e) => { 322 | log::warn!("Failed to read available power profiles using ACPI: {e:?}"); 323 | } 324 | }; 325 | }; 326 | #[cfg(target_arch = "x86_64")] 327 | if self.ryzenadj.is_some() { 328 | let ryzenadj = self.ryzenadj.as_ref().unwrap(); 329 | match ryzenadj.power_profiles_available().await { 330 | Ok(profiles) => { 331 | log::info!("Available power profiles are {profiles:?}"); 332 | return Ok(profiles); 333 | } 334 | Err(e) => { 335 | log::warn!("Failed to read available power profiles using RyzenAdj: {e:?}"); 336 | } 337 | }; 338 | }; 339 | Err(TDPError::FailedOperation( 340 | "No TDP Interface available to list available power profiles.".into(), 341 | )) 342 | } 343 | } 344 | -------------------------------------------------------------------------------- /src/performance/gpu/asus/asus_wmi.rs: -------------------------------------------------------------------------------- 1 | use crate::performance::gpu::tdp::{TDPError, TDPResult}; 2 | 3 | use rog_platform::{ 4 | error::PlatformError, 5 | firmware_attributes::{AttrValue, FirmwareAttributes}, 6 | platform::RogPlatform, 7 | }; 8 | 9 | impl From for TDPError { 10 | fn from(value: PlatformError) -> Self { 11 | Self::FailedOperation(value.to_string()) 12 | } 13 | } 14 | 15 | /// Implementation of asus-wmi sysfs 16 | /// See https://www.kernel.org/doc/html/v6.8-rc4/admin-guide/abi-testing.html#abi-sys-devices-platform-platform-ppt-apu-sppt 17 | pub struct AsusWmi { 18 | attributes: FirmwareAttributes, 19 | platform: RogPlatform, 20 | } 21 | 22 | impl AsusWmi { 23 | /// test if we are in an asus system with asus-wmi loaded 24 | pub async fn new() -> Option { 25 | match RogPlatform::new() { 26 | Ok(platform) => { 27 | log::info!("Module asus-wmi found"); 28 | let attributes = FirmwareAttributes::new(); 29 | Some(Self { 30 | attributes, 31 | platform, 32 | }) 33 | } 34 | Err(err) => { 35 | log::info!("Module asus-wmi not found: {err:?}"); 36 | None 37 | } 38 | } 39 | } 40 | 41 | /// Returns the currently set STAPM value 42 | pub async fn tdp(&self) -> TDPResult { 43 | let attr = self 44 | .attributes 45 | .attributes() 46 | .iter() 47 | .find(|a| a.name() == "ppt_pl1_spl"); 48 | let Some(attr) = attr else { 49 | return Ok(self.platform.get_ppt_pl1_spl()? as f64); 50 | }; 51 | 52 | match attr.current_value() { 53 | Ok(attr_value) => match attr_value { 54 | AttrValue::Integer(value) => Ok(value as f64), 55 | _ => Err(TDPError::FailedOperation("Failed to read SPL.".to_string())), 56 | }, 57 | Err(e) => { 58 | let err = format!("Failed to read SPL: {e:?}"); 59 | Err(TDPError::FailedOperation(err)) 60 | } 61 | } 62 | } 63 | 64 | /// Sets STAPM to the given value and adjusts the SPPT/FPPT to maintaing the current boost 65 | /// ratio 66 | pub async fn set_tdp(&mut self, value: f64) -> TDPResult<()> { 67 | if value < 1.0 || value > u8::MAX as f64 { 68 | return Err(TDPError::InvalidArgument( 69 | "Value must be between 1 and 255".to_string(), 70 | )); 71 | } 72 | 73 | // Get the current Boost value 74 | let boost = self.boost().await?; 75 | 76 | // Set the STAPM value to the given TDP value 77 | let attr = self 78 | .attributes 79 | .attributes() 80 | .iter() 81 | .find(|a| a.name() == "ppt_pl1_spl"); 82 | 83 | if let Some(attr) = attr { 84 | let val = AttrValue::Integer(value as i32); 85 | match attr.set_current_value(val) { 86 | Ok(_) => { 87 | log::info!("Set SPL to {value}"); 88 | } 89 | Err(e) => { 90 | return Err(TDPError::FailedOperation(format!( 91 | "Failed to set SPL: {e:?}" 92 | ))); 93 | } 94 | } 95 | } else { 96 | self.platform.set_ppt_pl1_spl(value as u8)?; 97 | } 98 | 99 | // Set the boost back to the expected value with the new TDP 100 | self.set_boost(boost).await?; 101 | 102 | Ok(()) 103 | } 104 | 105 | /// Returns the current difference between STAPM and SPPT 106 | pub async fn boost(&self) -> TDPResult { 107 | let stapm = match self.tdp().await { 108 | Ok(val) => val, 109 | Err(e) => { 110 | return Err(e); 111 | } 112 | }; 113 | 114 | let attr = self 115 | .attributes 116 | .attributes() 117 | .iter() 118 | .find(|a| a.name() == "ppt_platform_sppt"); 119 | 120 | let slow_ppt = { 121 | if let Some(attr) = attr { 122 | match attr.current_value() { 123 | Ok(attr_value) => match attr_value { 124 | AttrValue::Integer(value) => value as f64, 125 | _ => { 126 | return Err(TDPError::FailedOperation( 127 | "Failed to read SPPT.".to_string(), 128 | )) 129 | } 130 | }, 131 | Err(_) => { 132 | return Err(TDPError::FailedOperation( 133 | "Failed to read SPPT.".to_string(), 134 | )) 135 | } 136 | } 137 | // 138 | } else { 139 | self.platform.get_ppt_platform_sppt()? as f64 140 | } 141 | }; 142 | 143 | let boost = slow_ppt - stapm; 144 | log::info!("Found current boost: {boost}"); 145 | Ok(boost) 146 | } 147 | 148 | /// Sets SPPT and FPPT to the current STAPM plus the given value 149 | pub async fn set_boost(&mut self, value: f64) -> TDPResult<()> { 150 | let stapm = match self.tdp().await { 151 | Ok(val) => val, 152 | Err(e) => { 153 | return Err(e); 154 | } 155 | }; 156 | if (stapm + value) < 1.0 || (stapm + value) > u8::MAX as f64 { 157 | return Err(TDPError::InvalidArgument( 158 | "Combined TDP + Boost value must be between 1 and 255".to_string(), 159 | )); 160 | } 161 | let sppt_val = (stapm + value) as i32; 162 | 163 | // ppt_platform_sppt will set sppt to value and fppt to value + 25% 164 | let attr = self 165 | .attributes 166 | .attributes() 167 | .iter() 168 | .find(|a| a.name() == "ppt_platform_sppt"); 169 | let Some(attr) = attr else { 170 | return Ok(self.platform.set_ppt_platform_sppt(sppt_val as u8)?); 171 | }; 172 | 173 | let boost = AttrValue::Integer(sppt_val); 174 | match attr.set_current_value(boost) { 175 | Ok(_) => { 176 | log::info!("Set SPPT to {value}"); 177 | } 178 | Err(e) => { 179 | return Err(TDPError::FailedOperation(format!( 180 | "Failed to set SPPT: {e:?}" 181 | ))); 182 | } 183 | } 184 | 185 | Ok(()) 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /src/performance/gpu/asus/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod asus_wmi; 2 | -------------------------------------------------------------------------------- /src/performance/gpu/connector.rs: -------------------------------------------------------------------------------- 1 | use std::fs; 2 | use zbus::fdo; 3 | use zbus_macros::dbus_interface; 4 | 5 | /// Represents the data contained in /sys/class/drm/cardX-YYYY 6 | pub struct Connector { 7 | pub name: String, 8 | pub path: String, 9 | } 10 | 11 | #[dbus_interface(name = "org.shadowblip.GPU.Card.Connector")] 12 | impl Connector { 13 | #[dbus_interface(property)] 14 | fn name(&self) -> String { 15 | self.name.clone() 16 | } 17 | 18 | #[dbus_interface(property)] 19 | fn path(&self) -> String { 20 | self.path.clone() 21 | } 22 | 23 | #[dbus_interface(property)] 24 | fn id(&self) -> fdo::Result { 25 | let path = format!("{0}/{1}", self.path(), "connector_id"); 26 | let result = fs::read_to_string(path); 27 | let result = result 28 | // convert the std::io::Error to a zbus::fdo::Error 29 | .map_err(|err| fdo::Error::IOError(err.to_string()))?; 30 | let id = result.trim(); 31 | 32 | // Convert the ID to an integer 33 | let id = id 34 | .parse::() 35 | // convert the ParseIntError to a zbus::fdo::Error 36 | .map_err(|err| fdo::Error::Failed(err.to_string()))?; 37 | 38 | Ok(id) 39 | } 40 | 41 | #[dbus_interface(property)] 42 | fn enabled(&self) -> fdo::Result { 43 | let path = format!("{0}/{1}", self.path(), "enabled"); 44 | let result = fs::read_to_string(path); 45 | let status = result 46 | // convert the std::io::Error to a zbus::fdo::Error 47 | .map_err(|err| fdo::Error::IOError(err.to_string()))? 48 | .trim() 49 | .to_lowercase(); 50 | 51 | Ok(status == "enabled") 52 | } 53 | 54 | #[dbus_interface(property)] 55 | fn modes(&self) -> fdo::Result> { 56 | let mut modes: Vec = Vec::new(); 57 | let path = format!("{0}/{1}", self.path(), "modes"); 58 | let result = fs::read_to_string(path); 59 | let lines = result.map_err(|err| fdo::Error::IOError(err.to_string()))?; 60 | let lines = lines.split("\n"); 61 | 62 | // Add each available mode to the list of modes 63 | for line in lines { 64 | let mode = line.trim().to_string(); 65 | if mode.is_empty() { 66 | continue; 67 | } 68 | modes.push(mode); 69 | } 70 | 71 | Ok(modes) 72 | } 73 | 74 | #[dbus_interface(property)] 75 | fn status(&self) -> fdo::Result { 76 | let path = format!("{0}/{1}", self.path(), "status"); 77 | let result = fs::read_to_string(path); 78 | let status = result 79 | // convert the std::io::Error to a zbus::fdo::Error 80 | .map_err(|err| fdo::Error::IOError(err.to_string()))? 81 | .trim() 82 | .to_lowercase(); 83 | 84 | Ok(status) 85 | } 86 | 87 | #[dbus_interface(property, name = "DPMS")] 88 | fn dpms(&self) -> fdo::Result { 89 | let path = format!("{0}/{1}", self.path(), "dpms"); 90 | let result = fs::read_to_string(path); 91 | let status = result 92 | // convert the std::io::Error to a zbus::fdo::Error 93 | .map_err(|err| fdo::Error::IOError(err.to_string()))? 94 | .trim() 95 | .to_lowercase(); 96 | 97 | Ok(status == "on") 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/performance/gpu/dbus/devices.rs: -------------------------------------------------------------------------------- 1 | use std::sync::Arc; 2 | 3 | use tokio::sync::Mutex; 4 | 5 | use crate::performance::gpu::{ 6 | amd, intel, 7 | interface::{GPUDevice, GPUResult}, 8 | tdp::{TDPDevice, TDPResult}, 9 | }; 10 | 11 | pub enum TDPDevices { 12 | Amd(amd::tdp::Tdp), 13 | Intel(intel::tdp::Tdp), 14 | } 15 | 16 | impl TDPDevices { 17 | pub async fn tdp(&self) -> TDPResult { 18 | match self { 19 | Self::Amd(dev) => dev.tdp().await, 20 | Self::Intel(dev) => dev.tdp().await, 21 | } 22 | } 23 | 24 | pub async fn min_tdp(&self) -> TDPResult { 25 | match self { 26 | Self::Amd(dev) => dev.min_tdp().await, 27 | Self::Intel(dev) => dev.min_tdp().await, 28 | } 29 | } 30 | 31 | pub async fn max_tdp(&self) -> TDPResult { 32 | match self { 33 | Self::Amd(dev) => dev.max_tdp().await, 34 | Self::Intel(dev) => dev.max_tdp().await, 35 | } 36 | } 37 | 38 | pub async fn set_tdp(&mut self, value: f64) -> TDPResult<()> { 39 | match self { 40 | Self::Amd(dev) => dev.set_tdp(value).await, 41 | Self::Intel(dev) => dev.set_tdp(value).await, 42 | } 43 | } 44 | 45 | pub async fn boost(&self) -> TDPResult { 46 | match self { 47 | Self::Amd(dev) => dev.boost().await, 48 | Self::Intel(dev) => dev.boost().await, 49 | } 50 | } 51 | 52 | pub async fn max_boost(&self) -> TDPResult { 53 | match self { 54 | Self::Amd(dev) => dev.max_boost().await, 55 | Self::Intel(dev) => dev.max_boost().await, 56 | } 57 | } 58 | 59 | pub async fn set_boost(&mut self, value: f64) -> TDPResult<()> { 60 | match self { 61 | Self::Amd(dev) => dev.set_boost(value).await, 62 | Self::Intel(dev) => dev.set_boost(value).await, 63 | } 64 | } 65 | 66 | pub async fn thermal_throttle_limit_c(&self) -> TDPResult { 67 | match self { 68 | Self::Amd(dev) => dev.thermal_throttle_limit_c().await, 69 | Self::Intel(dev) => dev.thermal_throttle_limit_c().await, 70 | } 71 | } 72 | 73 | pub async fn set_thermal_throttle_limit_c(&mut self, limit: f64) -> TDPResult<()> { 74 | match self { 75 | Self::Amd(dev) => dev.set_thermal_throttle_limit_c(limit).await, 76 | Self::Intel(dev) => dev.set_thermal_throttle_limit_c(limit).await, 77 | } 78 | } 79 | 80 | //TODO: Deprecate the power_profile functions and set them automatically with TDP. 81 | pub async fn power_profile(&self) -> TDPResult { 82 | match self { 83 | Self::Amd(dev) => dev.power_profile().await, 84 | Self::Intel(dev) => dev.power_profile().await, 85 | } 86 | } 87 | 88 | pub async fn set_power_profile(&mut self, profile: String) -> TDPResult<()> { 89 | match self { 90 | Self::Amd(dev) => dev.set_power_profile(profile).await, 91 | Self::Intel(dev) => dev.set_power_profile(profile).await, 92 | } 93 | } 94 | 95 | pub async fn power_profiles_available(&self) -> TDPResult> { 96 | match self { 97 | Self::Amd(dev) => dev.power_profiles_available().await, 98 | Self::Intel(dev) => dev.power_profiles_available().await, 99 | } 100 | } 101 | } 102 | 103 | pub enum GPUDevices { 104 | AmdGpu(amd::amdgpu::AmdGpu), 105 | IntelGpu(intel::intelgpu::IntelGPU), 106 | } 107 | 108 | impl GPUDevices { 109 | pub async fn get_tdp_interface(&self) -> Option>> { 110 | match self { 111 | Self::AmdGpu(dev) => dev.get_tdp_interface().await, 112 | Self::IntelGpu(dev) => dev.get_tdp_interface().await, 113 | } 114 | } 115 | 116 | pub async fn get_gpu_path(&self) -> String { 117 | match self { 118 | Self::AmdGpu(dev) => dev.get_gpu_path().await, 119 | Self::IntelGpu(dev) => dev.get_gpu_path().await, 120 | } 121 | } 122 | 123 | pub async fn name(&self) -> String { 124 | match self { 125 | Self::AmdGpu(dev) => dev.name().await, 126 | Self::IntelGpu(dev) => dev.name().await, 127 | } 128 | } 129 | 130 | pub async fn path(&self) -> String { 131 | match self { 132 | Self::AmdGpu(dev) => dev.path().await, 133 | Self::IntelGpu(dev) => dev.path().await, 134 | } 135 | } 136 | 137 | pub async fn class(&self) -> String { 138 | match self { 139 | Self::AmdGpu(dev) => dev.class().await, 140 | Self::IntelGpu(dev) => dev.class().await, 141 | } 142 | } 143 | 144 | pub async fn class_id(&self) -> String { 145 | match self { 146 | Self::AmdGpu(dev) => dev.class_id().await, 147 | Self::IntelGpu(dev) => dev.class_id().await, 148 | } 149 | } 150 | 151 | pub async fn vendor(&self) -> String { 152 | match self { 153 | Self::AmdGpu(dev) => dev.vendor().await, 154 | Self::IntelGpu(dev) => dev.vendor().await, 155 | } 156 | } 157 | 158 | pub async fn vendor_id(&self) -> String { 159 | match self { 160 | Self::AmdGpu(dev) => dev.vendor_id().await, 161 | Self::IntelGpu(dev) => dev.vendor_id().await, 162 | } 163 | } 164 | 165 | pub async fn device(&self) -> String { 166 | match self { 167 | Self::AmdGpu(dev) => dev.device().await, 168 | Self::IntelGpu(dev) => dev.device().await, 169 | } 170 | } 171 | 172 | pub async fn device_id(&self) -> String { 173 | match self { 174 | Self::AmdGpu(dev) => dev.device_id().await, 175 | Self::IntelGpu(dev) => dev.device_id().await, 176 | } 177 | } 178 | 179 | pub async fn subdevice(&self) -> String { 180 | match self { 181 | Self::AmdGpu(dev) => dev.subdevice().await, 182 | Self::IntelGpu(dev) => dev.subdevice().await, 183 | } 184 | } 185 | 186 | pub async fn subdevice_id(&self) -> String { 187 | match self { 188 | Self::AmdGpu(dev) => dev.subdevice_id().await, 189 | Self::IntelGpu(dev) => dev.subdevice_id().await, 190 | } 191 | } 192 | 193 | pub async fn subvendor_id(&self) -> String { 194 | match self { 195 | Self::AmdGpu(dev) => dev.subvendor_id().await, 196 | Self::IntelGpu(dev) => dev.subvendor_id().await, 197 | } 198 | } 199 | 200 | pub async fn revision_id(&self) -> String { 201 | match self { 202 | Self::AmdGpu(dev) => dev.revision_id().await, 203 | Self::IntelGpu(dev) => dev.revision_id().await, 204 | } 205 | } 206 | 207 | pub async fn clock_limit_mhz_min(&self) -> GPUResult { 208 | match self { 209 | Self::AmdGpu(dev) => dev.clock_limit_mhz_min().await, 210 | Self::IntelGpu(dev) => dev.clock_limit_mhz_min().await, 211 | } 212 | } 213 | 214 | pub async fn clock_limit_mhz_max(&self) -> GPUResult { 215 | match self { 216 | Self::AmdGpu(dev) => dev.clock_limit_mhz_max().await, 217 | Self::IntelGpu(dev) => dev.clock_limit_mhz_max().await, 218 | } 219 | } 220 | 221 | pub async fn clock_value_mhz_min(&self) -> GPUResult { 222 | match self { 223 | Self::AmdGpu(dev) => dev.clock_value_mhz_min().await, 224 | Self::IntelGpu(dev) => dev.clock_value_mhz_min().await, 225 | } 226 | } 227 | 228 | pub async fn set_clock_value_mhz_min(&mut self, value: f64) -> GPUResult<()> { 229 | match self { 230 | Self::AmdGpu(dev) => dev.set_clock_value_mhz_min(value).await, 231 | Self::IntelGpu(dev) => dev.set_clock_value_mhz_min(value).await, 232 | } 233 | } 234 | 235 | pub async fn clock_value_mhz_max(&self) -> GPUResult { 236 | match self { 237 | Self::AmdGpu(dev) => dev.clock_value_mhz_max().await, 238 | Self::IntelGpu(dev) => dev.clock_value_mhz_max().await, 239 | } 240 | } 241 | 242 | pub async fn set_clock_value_mhz_max(&mut self, value: f64) -> GPUResult<()> { 243 | match self { 244 | Self::AmdGpu(dev) => dev.set_clock_value_mhz_max(value).await, 245 | Self::IntelGpu(dev) => dev.set_clock_value_mhz_max(value).await, 246 | } 247 | } 248 | 249 | pub async fn manual_clock(&self) -> GPUResult { 250 | match self { 251 | Self::AmdGpu(dev) => dev.manual_clock().await, 252 | Self::IntelGpu(dev) => dev.manual_clock().await, 253 | } 254 | } 255 | 256 | pub async fn set_manual_clock(&mut self, enabled: bool) -> GPUResult<()> { 257 | match self { 258 | Self::AmdGpu(dev) => dev.set_manual_clock(enabled).await, 259 | Self::IntelGpu(dev) => dev.set_manual_clock(enabled).await, 260 | } 261 | } 262 | } 263 | -------------------------------------------------------------------------------- /src/performance/gpu/dbus/gpu.rs: -------------------------------------------------------------------------------- 1 | use std::fs::{self, File}; 2 | use std::io::{prelude::*, BufReader}; 3 | use std::path::PathBuf; 4 | use std::sync::Arc; 5 | use zbus::fdo; 6 | use zbus::zvariant::ObjectPath; 7 | use zbus_macros::dbus_interface; 8 | 9 | use tokio::sync::Mutex; 10 | 11 | use crate::performance::gpu::amd::amdgpu::AmdGpu; 12 | use crate::performance::gpu::connector::Connector; 13 | use crate::performance::gpu::dbus::devices::GPUDevices; 14 | use crate::performance::gpu::dbus::tdp::GPUTDPDBusIface; 15 | use crate::performance::gpu::intel::intelgpu::IntelGPU; 16 | use crate::performance::gpu::interface::GPUError; 17 | 18 | const DRM_PATH: &str = "/sys/class/drm"; 19 | const PCI_IDS_PATH: &str = "/usr/share/hwdata/pci.ids"; 20 | 21 | impl From for fdo::Error { 22 | fn from(val: GPUError) -> Self { 23 | match &val { 24 | GPUError::FailedOperation(err) => fdo::Error::Failed(err.to_string()), 25 | //Self::FeatureUnsupported => fdo::Error::Failed(String::from("Unsupported feature")), 26 | GPUError::InvalidArgument(err) => fdo::Error::Failed(err.to_string()), 27 | GPUError::IOError(err) => fdo::Error::IOError(err.to_string()), 28 | } 29 | } 30 | } 31 | 32 | /// Represents the DBus for GPUs in the system 33 | #[derive(Clone)] 34 | pub struct GPUDBusInterface { 35 | connector_paths: Vec, 36 | gpu_obj: Arc>, 37 | } 38 | 39 | impl GPUDBusInterface { 40 | pub async fn new(gpu: Arc>) -> Self { 41 | Self { 42 | gpu_obj: gpu, 43 | connector_paths: vec![], 44 | } 45 | } 46 | 47 | pub async fn gpu_path(&self) -> String { 48 | self.gpu_obj.lock().await.get_gpu_path().await 49 | } 50 | 51 | pub async fn set_connector_paths(&mut self, connector_paths: Vec) { 52 | self.connector_paths = connector_paths 53 | } 54 | 55 | pub async fn get_tdp_interface(&self) -> Option { 56 | self.gpu_obj 57 | .lock() 58 | .await 59 | .get_tdp_interface() 60 | .await 61 | .map(GPUTDPDBusIface::new) 62 | } 63 | } 64 | 65 | #[dbus_interface(name = "org.shadowblip.GPU.Card")] 66 | impl GPUDBusInterface { 67 | /// Returns a list of DBus paths to all connectors 68 | pub fn enumerate_connectors(&self) -> fdo::Result> { 69 | Ok(self 70 | .connector_paths 71 | .iter() 72 | .map(|path| ObjectPath::from_string_unchecked(path.clone())) 73 | .collect()) 74 | } 75 | 76 | #[dbus_interface(property)] 77 | pub async fn name(&self) -> String { 78 | self.gpu_obj.lock().await.name().await 79 | } 80 | 81 | #[dbus_interface(property)] 82 | async fn path(&self) -> String { 83 | self.gpu_obj.lock().await.path().await 84 | } 85 | 86 | #[dbus_interface(property)] 87 | async fn class(&self) -> String { 88 | self.gpu_obj.lock().await.class().await 89 | } 90 | 91 | #[dbus_interface(property)] 92 | async fn class_id(&self) -> String { 93 | self.gpu_obj.lock().await.class_id().await 94 | } 95 | 96 | #[dbus_interface(property)] 97 | async fn vendor(&self) -> String { 98 | self.gpu_obj.lock().await.vendor().await 99 | } 100 | 101 | #[dbus_interface(property)] 102 | async fn vendor_id(&self) -> String { 103 | self.gpu_obj.lock().await.vendor_id().await 104 | } 105 | 106 | #[dbus_interface(property)] 107 | async fn device(&self) -> String { 108 | self.gpu_obj.lock().await.device().await 109 | } 110 | 111 | #[dbus_interface(property)] 112 | async fn device_id(&self) -> String { 113 | self.gpu_obj.lock().await.device_id().await 114 | } 115 | 116 | #[dbus_interface(property)] 117 | async fn subdevice(&self) -> String { 118 | self.gpu_obj.lock().await.subdevice().await 119 | } 120 | 121 | #[dbus_interface(property)] 122 | async fn subdevice_id(&self) -> String { 123 | self.gpu_obj.lock().await.subdevice_id().await 124 | } 125 | 126 | #[dbus_interface(property)] 127 | async fn subvendor_id(&self) -> String { 128 | self.gpu_obj.lock().await.subvendor_id().await 129 | } 130 | 131 | #[dbus_interface(property)] 132 | async fn revision_id(&self) -> String { 133 | self.gpu_obj.lock().await.revision_id().await 134 | } 135 | 136 | #[dbus_interface(property)] 137 | async fn clock_limit_mhz_min(&self) -> fdo::Result { 138 | self.gpu_obj 139 | .lock() 140 | .await 141 | .clock_limit_mhz_min() 142 | .await 143 | .map_err(|err| err.into()) 144 | } 145 | 146 | #[dbus_interface(property)] 147 | async fn clock_limit_mhz_max(&self) -> fdo::Result { 148 | self.gpu_obj 149 | .lock() 150 | .await 151 | .clock_limit_mhz_max() 152 | .await 153 | .map_err(|err| err.into()) 154 | } 155 | 156 | #[dbus_interface(property)] 157 | async fn clock_value_mhz_min(&self) -> fdo::Result { 158 | self.gpu_obj 159 | .lock() 160 | .await 161 | .clock_value_mhz_min() 162 | .await 163 | .map_err(|err| err.into()) 164 | } 165 | 166 | #[dbus_interface(property)] 167 | async fn set_clock_value_mhz_min(&mut self, value: f64) -> fdo::Result<()> { 168 | self.gpu_obj 169 | .lock() 170 | .await 171 | .set_clock_value_mhz_min(value) 172 | .await 173 | .map_err(|err| err.into()) 174 | } 175 | 176 | #[dbus_interface(property)] 177 | async fn clock_value_mhz_max(&self) -> fdo::Result { 178 | self.gpu_obj 179 | .lock() 180 | .await 181 | .clock_value_mhz_max() 182 | .await 183 | .map_err(|err| err.into()) 184 | } 185 | 186 | #[dbus_interface(property)] 187 | async fn set_clock_value_mhz_max(&mut self, value: f64) -> fdo::Result<()> { 188 | self.gpu_obj 189 | .lock() 190 | .await 191 | .set_clock_value_mhz_max(value) 192 | .await 193 | .map_err(|err| err.into()) 194 | } 195 | 196 | #[dbus_interface(property)] 197 | async fn manual_clock(&self) -> fdo::Result { 198 | self.gpu_obj 199 | .lock() 200 | .await 201 | .manual_clock() 202 | .await 203 | .map_err(|err| err.into()) 204 | } 205 | 206 | #[dbus_interface(property)] 207 | async fn set_manual_clock(&mut self, enabled: bool) -> fdo::Result<()> { 208 | self.gpu_obj 209 | .lock() 210 | .await 211 | .set_manual_clock(enabled) 212 | .await 213 | .map_err(|err| err.into()) 214 | } 215 | } 216 | 217 | /// Used to enumerate all GPU cards over DBus 218 | pub struct GPUBus { 219 | gpu_object_paths: Vec, 220 | } 221 | 222 | impl GPUBus { 223 | /// Return a new instance of the GPU Bus 224 | pub fn new(gpu_paths: Vec) -> GPUBus { 225 | GPUBus { 226 | gpu_object_paths: gpu_paths, 227 | } 228 | } 229 | } 230 | 231 | #[dbus_interface(name = "org.shadowblip.GPU")] 232 | impl GPUBus { 233 | /// Returns a list of DBus paths to all GPU cards 234 | pub async fn enumerate_cards(&self) -> fdo::Result> { 235 | let mut paths: Vec = Vec::new(); 236 | 237 | for item in &self.gpu_object_paths { 238 | let path = ObjectPath::from_string_unchecked(item.clone()); 239 | paths.push(path); 240 | } 241 | 242 | Ok(paths) 243 | } 244 | } 245 | 246 | /// Returns a list of all detected gpu devices 247 | pub async fn get_gpus() -> Vec { 248 | let mut gpus = vec![]; 249 | let paths = fs::read_dir(DRM_PATH).unwrap(); 250 | for path in paths { 251 | let path = path.unwrap(); 252 | let filename = path.file_name().to_str().unwrap().to_string(); 253 | let file_path = path.path().to_str().unwrap().to_string(); 254 | 255 | if !filename.starts_with("card") { 256 | continue; 257 | } 258 | if filename.contains("-") { 259 | continue; 260 | } 261 | 262 | log::info!("Discovered gpu: {}", file_path); 263 | match get_gpu(file_path).await { 264 | Ok(gpu) => gpus.push(gpu), 265 | Err(err) => { 266 | log::error!("Error in get_gpu: {}", err); 267 | continue; 268 | } 269 | } 270 | } 271 | 272 | gpus 273 | } 274 | 275 | /// Returns the GPU instance for the given path in /sys/class/drm 276 | pub async fn get_gpu(path: String) -> Result { 277 | let filename = path.split("/").last().unwrap(); 278 | let file_prefix = format!("{0}/{1}", path, "device"); 279 | let class_id = fs::read_to_string(format!("{0}/{1}", file_prefix, "class"))? 280 | .trim() 281 | .replace("0x", "") 282 | .to_lowercase(); 283 | let vendor_id = fs::read_to_string(format!("{0}/{1}", file_prefix, "vendor"))? 284 | .trim() 285 | .replace("0x", "") 286 | .to_lowercase(); 287 | let device_id = fs::read_to_string(format!("{0}/{1}", file_prefix, "device"))? 288 | .trim() 289 | .replace("0x", "") 290 | .to_lowercase(); 291 | let revision_id = fs::read_to_string(format!("{0}/{1}", file_prefix, "revision"))? 292 | .trim() 293 | .replace("0x", "") 294 | .to_lowercase(); 295 | let subvendor_id = fs::read_to_string(format!("{0}/{1}", file_prefix, "subsystem_vendor"))? 296 | .trim() 297 | .replace("0x", "") 298 | .to_lowercase(); 299 | let subdevice_id = fs::read_to_string(format!("{0}/{1}", file_prefix, "subsystem_device"))? 300 | .trim() 301 | .replace("0x", "") 302 | .to_lowercase(); 303 | 304 | // Open the file that contains hardware ID mappings 305 | let hw_ids_file = File::open(get_pci_ids_path())?; 306 | let reader = BufReader::new(hw_ids_file); 307 | 308 | // Set the class based on class ID 309 | let class = match class_id.as_str() { 310 | "030000" => "integrated", 311 | "038000" => "dedicated", 312 | _ => "unknown", 313 | }; 314 | 315 | // Lookup the card details by parsing the lines of the file 316 | let mut vendor: Option = None; 317 | let mut device: Option = None; 318 | let mut subdevice: Option = None; 319 | log::debug!( 320 | "Getting device info from: {} {} {} {}", 321 | vendor_id, 322 | device_id, 323 | revision_id, 324 | subvendor_id 325 | ); 326 | for line in reader.lines() { 327 | let line = line?; 328 | let line_clean = line.trim(); 329 | 330 | if line.starts_with("\t") && vendor.is_none() { 331 | continue; 332 | } 333 | if line.starts_with(&vendor_id) { 334 | vendor = Some( 335 | line.clone() 336 | .trim_start_matches(&vendor_id) 337 | .trim() 338 | .to_string(), 339 | ); 340 | log::debug!("Found vendor: {}", vendor.clone().unwrap()); 341 | continue; 342 | } 343 | if vendor.is_some() && !line.starts_with("\t") { 344 | if line.starts_with("#") { 345 | continue; 346 | } 347 | log::debug!("Got to end of vendor list. Device not found."); 348 | break; 349 | } 350 | 351 | if line.starts_with("\t\t") && device.is_none() { 352 | continue; 353 | } 354 | 355 | if line_clean.starts_with(&device_id) { 356 | device = Some(line_clean.trim_start_matches(&device_id).trim().to_string()); 357 | log::debug!("Found device name: {}", device.clone().unwrap()); 358 | } 359 | 360 | if device.is_some() && !line.starts_with("\t\t") { 361 | log::debug!("Got to end of device list. Subdevice not found"); 362 | break; 363 | } 364 | 365 | let prefix = format!("{0} {1}", subvendor_id, subdevice_id); 366 | if line_clean.starts_with(&prefix) { 367 | subdevice = Some(line_clean.trim_start_matches(&prefix).trim().to_string()); 368 | log::debug!("Found subdevice name: {}", subdevice.clone().unwrap()); 369 | break; 370 | } 371 | } 372 | 373 | // Return an error if no vendor was found 374 | if vendor.is_none() { 375 | return Err(std::io::Error::new( 376 | std::io::ErrorKind::NotFound, 377 | "No vendor found", 378 | )); 379 | } 380 | 381 | // Sanitize the vendor strings so they are standard 382 | match vendor.unwrap().as_str() { 383 | // AMD Implementation 384 | "AMD" 385 | | "AuthenticAMD" 386 | | "AuthenticAMD Advanced Micro Devices, Inc." 387 | | "Advanced Micro Devices, Inc. [AMD/ATI]" => Ok(GPUDBusInterface::new(Arc::new( 388 | Mutex::new(GPUDevices::AmdGpu(AmdGpu { 389 | name: filename.to_string(), 390 | path: path.clone(), 391 | class: class.to_string(), 392 | class_id, 393 | vendor: "AMD".to_string(), 394 | vendor_id, 395 | device: device.unwrap_or("".to_string()), 396 | device_id, 397 | //device_type: "".to_string(), 398 | subdevice: subdevice.unwrap_or("".to_string()), 399 | subdevice_id, 400 | subvendor_id, 401 | revision_id, 402 | })), 403 | )) 404 | .await), 405 | // Intel Implementation 406 | "Intel" | "GenuineIntel" | "Intel Corporation" => Ok(GPUDBusInterface::new(Arc::new( 407 | Mutex::new(GPUDevices::IntelGpu(IntelGPU { 408 | name: filename.to_string(), 409 | path: path.clone(), 410 | class: class.to_string(), 411 | class_id, 412 | vendor: "Intel".to_string(), 413 | vendor_id, 414 | device: device.unwrap_or("".to_string()), 415 | device_id, 416 | //device_type: "".to_string(), 417 | subdevice: subdevice.unwrap_or("".to_string()), 418 | subdevice_id, 419 | subvendor_id, 420 | revision_id, 421 | manual_clock: true, 422 | })), 423 | )) 424 | .await), 425 | _ => Err(std::io::Error::new( 426 | std::io::ErrorKind::Unsupported, 427 | "Unsupported vendor", 428 | )), 429 | } 430 | } 431 | 432 | /// Returns a [Connector] instance that represents the given path in /sys/class/drm 433 | pub fn get_connector(gpu_name: String, path: String) -> Connector { 434 | let prefix = format!("{}-", &gpu_name); 435 | let name = path.trim_start_matches(&prefix); 436 | 437 | Connector { 438 | name: name.to_string(), 439 | path: format!("{0}/{1}", DRM_PATH, path), 440 | } 441 | } 442 | 443 | /// Returns a list of [Connector] instances for the given graphics card name. 444 | /// E.g. `"card1"` 445 | pub fn get_connectors(gpu_name: String) -> Vec { 446 | log::debug!("Discovering connectors for GPU: {}", gpu_name); 447 | let mut connectors: Vec = Vec::new(); 448 | let paths = fs::read_dir(DRM_PATH).unwrap(); 449 | for path in paths { 450 | let path = path.unwrap(); 451 | let filename = path.file_name().to_str().unwrap().to_string(); 452 | 453 | // Skip paths that do not contain the gpu name 454 | if !filename.starts_with(&gpu_name) { 455 | continue; 456 | } 457 | if filename == gpu_name { 458 | continue; 459 | } 460 | 461 | let connector = get_connector(gpu_name.clone(), filename); 462 | connectors.push(connector); 463 | } 464 | 465 | log::debug!("Finished finding connectors"); 466 | connectors 467 | } 468 | 469 | /// Returns the path to the PCI id's file from hwdata 470 | fn get_pci_ids_path() -> PathBuf { 471 | let Ok(base_dirs) = xdg::BaseDirectories::with_prefix("hwdata") else { 472 | log::warn!("Unable to determine config base path. Using fallback path."); 473 | return PathBuf::from(PCI_IDS_PATH); 474 | }; 475 | 476 | // Get the data directories in preference order 477 | let data_dirs = base_dirs.get_data_dirs(); 478 | for dir in data_dirs { 479 | if dir.exists() { 480 | let mut path = dir.into_os_string(); 481 | path.push("/pci.ids"); 482 | return path.into(); 483 | } 484 | } 485 | 486 | log::warn!("Config base path not found. Using fallback path."); 487 | PathBuf::from(PCI_IDS_PATH) 488 | } 489 | -------------------------------------------------------------------------------- /src/performance/gpu/dbus/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod devices; 2 | pub mod gpu; 3 | pub mod tdp; 4 | -------------------------------------------------------------------------------- /src/performance/gpu/dbus/tdp.rs: -------------------------------------------------------------------------------- 1 | use std::sync::Arc; 2 | use zbus::fdo; 3 | use zbus_macros::dbus_interface; 4 | 5 | use tokio::sync::Mutex; 6 | 7 | use crate::performance::gpu::dbus::devices::TDPDevices; 8 | use crate::performance::gpu::tdp::TDPError; 9 | use crate::performance::gpu::tdp::TDPResult; 10 | 11 | pub struct GPUTDPDBusIface { 12 | dev: Arc>, 13 | } 14 | 15 | impl From for fdo::Error { 16 | fn from(val: TDPError) -> Self { 17 | match &val { 18 | TDPError::FailedOperation(err) => fdo::Error::Failed(err.to_string()), 19 | TDPError::FeatureUnsupported => fdo::Error::Failed(String::from("Unsupported feature")), 20 | TDPError::InvalidArgument(err) => fdo::Error::Failed(err.to_string()), 21 | TDPError::IOError(err) => fdo::Error::IOError(err.to_string()), 22 | } 23 | } 24 | } 25 | 26 | impl GPUTDPDBusIface { 27 | pub fn new(dev: Arc>) -> GPUTDPDBusIface { 28 | GPUTDPDBusIface { dev } 29 | } 30 | } 31 | 32 | #[dbus_interface(name = "org.shadowblip.GPU.Card.TDP")] 33 | impl GPUTDPDBusIface { 34 | /// Get the currently set TDP value 35 | #[dbus_interface(property, name = "TDP")] 36 | async fn tdp(&self) -> fdo::Result { 37 | match self.dev.lock().await.tdp().await { 38 | TDPResult::Ok(result) => Ok(result), 39 | TDPResult::Err(err) => Err(err.into()), 40 | } 41 | } 42 | 43 | /// Sets the given TDP value 44 | #[dbus_interface(property, name = "TDP")] 45 | async fn set_tdp(&mut self, value: f64) -> fdo::Result<()> { 46 | match self.dev.lock().await.set_tdp(value).await { 47 | TDPResult::Ok(result) => Ok(result), 48 | TDPResult::Err(err) => Err(err.into()), 49 | } 50 | } 51 | 52 | #[dbus_interface(property)] 53 | async fn min_tdp(&self) -> fdo::Result { 54 | match self.dev.lock().await.min_tdp().await { 55 | TDPResult::Ok(result) => Ok(result), 56 | TDPResult::Err(err) => Err(err.into()), 57 | } 58 | } 59 | 60 | #[dbus_interface(property)] 61 | async fn max_tdp(&self) -> fdo::Result { 62 | match self.dev.lock().await.max_tdp().await { 63 | TDPResult::Ok(result) => Ok(result), 64 | TDPResult::Err(err) => Err(err.into()), 65 | } 66 | } 67 | 68 | /// The TDP boost for AMD is the total difference between the Fast PPT Limit 69 | /// and the STAPM limit. 70 | #[dbus_interface(property)] 71 | async fn boost(&self) -> fdo::Result { 72 | match self.dev.lock().await.boost().await { 73 | TDPResult::Ok(result) => Ok(result), 74 | TDPResult::Err(err) => Err(err.into()), 75 | } 76 | } 77 | 78 | #[dbus_interface(property)] 79 | async fn max_boost(&self) -> fdo::Result { 80 | match self.dev.lock().await.max_boost().await { 81 | TDPResult::Ok(result) => Ok(result), 82 | TDPResult::Err(err) => Err(err.into()), 83 | } 84 | } 85 | 86 | #[dbus_interface(property)] 87 | async fn set_boost(&mut self, value: f64) -> fdo::Result<()> { 88 | match self.dev.lock().await.set_boost(value).await { 89 | TDPResult::Ok(result) => Ok(result), 90 | TDPResult::Err(err) => Err(err.into()), 91 | } 92 | } 93 | 94 | #[dbus_interface(property)] 95 | async fn thermal_throttle_limit_c(&self) -> fdo::Result { 96 | match self.dev.lock().await.thermal_throttle_limit_c().await { 97 | TDPResult::Ok(result) => Ok(result), 98 | TDPResult::Err(err) => Err(err.into()), 99 | } 100 | } 101 | 102 | #[dbus_interface(property)] 103 | async fn set_thermal_throttle_limit_c(&mut self, limit: f64) -> fdo::Result<()> { 104 | match self 105 | .dev 106 | .lock() 107 | .await 108 | .set_thermal_throttle_limit_c(limit) 109 | .await 110 | { 111 | TDPResult::Ok(result) => Ok(result), 112 | TDPResult::Err(err) => Err(err.into()), 113 | } 114 | } 115 | 116 | #[dbus_interface(property)] 117 | async fn power_profile(&self) -> fdo::Result { 118 | match self.dev.lock().await.power_profile().await { 119 | TDPResult::Ok(result) => Ok(result), 120 | TDPResult::Err(err) => Err(err.into()), 121 | } 122 | } 123 | 124 | #[dbus_interface(property)] 125 | async fn set_power_profile(&mut self, profile: String) -> fdo::Result<()> { 126 | match self.dev.lock().await.set_power_profile(profile).await { 127 | TDPResult::Ok(result) => Ok(result), 128 | TDPResult::Err(err) => Err(err.into()), 129 | } 130 | } 131 | 132 | #[dbus_interface(property)] 133 | async fn power_profiles_available(&self) -> fdo::Result> { 134 | match self.dev.lock().await.power_profiles_available().await { 135 | TDPResult::Ok(result) => Ok(result), 136 | TDPResult::Err(err) => Err(err.into()), 137 | } 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /src/performance/gpu/intel/intelgpu.rs: -------------------------------------------------------------------------------- 1 | use std::{ 2 | fs::{self, OpenOptions}, 3 | io::Write, 4 | sync::Arc, 5 | }; 6 | 7 | use tokio::sync::Mutex; 8 | 9 | use crate::constants::PREFIX; 10 | use crate::performance::gpu::{ 11 | dbus::devices::TDPDevices, 12 | interface::{GPUDevice, GPUError, GPUResult}, 13 | }; 14 | 15 | use super::tdp::Tdp; 16 | 17 | #[derive(Debug, Clone)] 18 | pub struct IntelGPU { 19 | pub name: String, 20 | pub path: String, 21 | pub class: String, 22 | pub class_id: String, 23 | pub vendor: String, 24 | pub vendor_id: String, 25 | pub device: String, 26 | pub device_id: String, 27 | //pub device_type: String, 28 | pub subdevice: String, 29 | pub subdevice_id: String, 30 | pub subvendor_id: String, 31 | pub revision_id: String, 32 | pub manual_clock: bool, 33 | } 34 | 35 | impl GPUDevice for IntelGPU { 36 | async fn get_gpu_path(&self) -> String { 37 | format!("{0}/GPU/{1}", PREFIX, self.name().await) 38 | } 39 | 40 | /// Returns the TDP DBus interface for this GPU 41 | async fn get_tdp_interface(&self) -> Option>> { 42 | match self.class.as_str() { 43 | "integrated" => Some(Arc::new(Mutex::new(TDPDevices::Intel(Tdp::new( 44 | self.path.clone(), 45 | ))))), 46 | _ => None, 47 | } 48 | } 49 | 50 | async fn name(&self) -> String { 51 | self.name.clone() 52 | } 53 | 54 | async fn path(&self) -> String { 55 | self.path.clone() 56 | } 57 | 58 | async fn class(&self) -> String { 59 | self.class.clone() 60 | } 61 | 62 | async fn class_id(&self) -> String { 63 | self.class_id.clone() 64 | } 65 | 66 | async fn vendor(&self) -> String { 67 | self.vendor.clone() 68 | } 69 | 70 | async fn vendor_id(&self) -> String { 71 | self.vendor_id.clone() 72 | } 73 | 74 | async fn device(&self) -> String { 75 | self.device.clone() 76 | } 77 | 78 | async fn device_id(&self) -> String { 79 | self.device_id.clone() 80 | } 81 | 82 | async fn subdevice(&self) -> String { 83 | self.subdevice.clone() 84 | } 85 | 86 | async fn subdevice_id(&self) -> String { 87 | self.subdevice_id.clone() 88 | } 89 | 90 | async fn subvendor_id(&self) -> String { 91 | self.subvendor_id.clone() 92 | } 93 | 94 | async fn revision_id(&self) -> String { 95 | self.revision_id.clone() 96 | } 97 | 98 | async fn clock_limit_mhz_min(&self) -> GPUResult { 99 | let path = format!("{0}/{1}", self.path().await, "gt_RPn_freq_mhz"); 100 | let result = fs::read_to_string(path); 101 | let limit = result 102 | .map_err(|err| GPUError::IOError(err.to_string()))? 103 | .trim() 104 | .parse::() 105 | .map_err(|err| GPUError::FailedOperation(err.to_string()))?; 106 | 107 | Ok(limit) 108 | } 109 | 110 | async fn clock_limit_mhz_max(&self) -> GPUResult { 111 | let path = format!("{0}/{1}", self.path().await, "gt_RP0_freq_mhz"); 112 | let limit = fs::read_to_string(path) 113 | .map_err(|err| GPUError::IOError(err.to_string()))? 114 | .trim() 115 | .parse::() 116 | .map_err(|err| GPUError::FailedOperation(err.to_string()))?; 117 | 118 | Ok(limit) 119 | } 120 | 121 | async fn clock_value_mhz_min(&self) -> GPUResult { 122 | let path = format!("{0}/{1}", self.path().await, "gt_min_freq_mhz"); 123 | let result = fs::read_to_string(path); 124 | let value = result 125 | .map_err(|err| GPUError::IOError(err.to_string()))? 126 | .trim() 127 | .parse::() 128 | .map_err(|err| GPUError::FailedOperation(err.to_string()))?; 129 | 130 | Ok(value) 131 | } 132 | 133 | async fn set_clock_value_mhz_min(&mut self, value: f64) -> GPUResult<()> { 134 | if value == 0.0 { 135 | return Err(GPUError::InvalidArgument( 136 | "Cowardly refusing to set clock to 0MHz".to_string(), 137 | )); 138 | } 139 | 140 | // Open the sysfs file to write to 141 | let path = format!("{0}/{1}", self.path().await, "gt_min_freq_mhz"); 142 | let file = OpenOptions::new().write(true).open(path); 143 | 144 | // Write the value 145 | file.map_err(|err| GPUError::FailedOperation(err.to_string()))? 146 | .write_all(value.to_string().as_bytes()) 147 | .map_err(|err| GPUError::IOError(err.to_string()))?; 148 | 149 | Ok(()) 150 | } 151 | 152 | async fn clock_value_mhz_max(&self) -> GPUResult { 153 | let path = format!("{0}/{1}", self.path().await, "gt_max_freq_mhz"); 154 | let result = fs::read_to_string(path); 155 | let value = result 156 | .map_err(|err| GPUError::IOError(err.to_string()))? 157 | .trim() 158 | .parse::() 159 | .map_err(|err| GPUError::FailedOperation(err.to_string()))?; 160 | 161 | Ok(value) 162 | } 163 | 164 | async fn set_clock_value_mhz_max(&mut self, value: f64) -> GPUResult<()> { 165 | if value == 0.0 { 166 | return Err(GPUError::InvalidArgument( 167 | "Cowardly refusing to set clock to 0MHz".to_string(), 168 | )); 169 | } 170 | 171 | // Open the sysfs file to write to 172 | let path = format!("{0}/{1}", self.path().await, "gt_max_freq_mhz"); 173 | let file = OpenOptions::new().write(true).open(path); 174 | 175 | // Write the value 176 | file.map_err(|err| GPUError::FailedOperation(err.to_string()))? 177 | .write_all(value.to_string().as_bytes()) 178 | .map_err(|err| GPUError::IOError(err.to_string()))?; 179 | 180 | Ok(()) 181 | } 182 | 183 | async fn manual_clock(&self) -> GPUResult { 184 | Ok(self.manual_clock) 185 | } 186 | 187 | async fn set_manual_clock(&mut self, enabled: bool) -> GPUResult<()> { 188 | self.manual_clock = enabled; 189 | Ok(()) 190 | } 191 | } 192 | -------------------------------------------------------------------------------- /src/performance/gpu/intel/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod intelgpu; 2 | pub mod tdp; 3 | -------------------------------------------------------------------------------- /src/performance/gpu/intel/tdp.rs: -------------------------------------------------------------------------------- 1 | use std::fs::{self, OpenOptions}; 2 | use std::io::Write; 3 | 4 | use crate::performance::gpu::{ 5 | platform::hardware::Hardware, 6 | tdp::{HardwareAccess, TDPDevice, TDPError, TDPResult}, 7 | }; 8 | 9 | /// Implementation of TDP control for Intel GPUs 10 | pub struct Tdp { 11 | //pub path: String, 12 | hardware: Option, 13 | } 14 | 15 | impl HardwareAccess for Tdp { 16 | fn hardware(&self) -> Option<&Hardware> { 17 | self.hardware.as_ref() 18 | } 19 | } 20 | 21 | impl Tdp { 22 | pub fn new(_path: String) -> Tdp { 23 | let hardware = match Hardware::new() { 24 | Some(hardware) => { 25 | log::info!("Found Hardware interface for TDP control"); 26 | Some(hardware) 27 | } 28 | None => None, 29 | }; 30 | 31 | Tdp { hardware } 32 | } 33 | } 34 | 35 | impl TDPDevice for Tdp { 36 | async fn tdp(&self) -> TDPResult { 37 | let path = "/sys/class/powercap/intel-rapl/intel-rapl:0/constraint_0_power_limit_uw"; 38 | let result = fs::read_to_string(path); 39 | let content = result.map_err(|err| TDPError::IOError(err.to_string()))?; 40 | let content = content.trim(); 41 | 42 | // Parse the output to get the long TDP 43 | let long_tdp = match content.parse::() { 44 | Ok(v) => v, 45 | Err(e) => { 46 | log::error!("{}", e); 47 | return Err(TDPError::FailedOperation(e.to_string())); 48 | } 49 | }; 50 | 51 | Ok(long_tdp / 1000000.0) 52 | } 53 | 54 | async fn set_tdp(&mut self, value: f64) -> TDPResult<()> { 55 | if value < 1.0 { 56 | let err = "Cowardly refusing to set TDP less than 1"; 57 | log::warn!("{}", err); 58 | return Err(TDPError::InvalidArgument(String::from(err))); 59 | } 60 | 61 | // Get the current boost value so the peak tdp can be set *boost* 62 | // distance away. 63 | let mut boost = self.boost().await?; 64 | if boost < 0.0 { 65 | log::warn!("Boost is less than 0, setting to 0"); 66 | boost = 0.0; 67 | } 68 | 69 | // Open the sysfs file to write to 70 | let path = "/sys/class/powercap/intel-rapl/intel-rapl:0/constraint_0_power_limit_uw"; 71 | let file = OpenOptions::new().write(true).open(path); 72 | 73 | // Convert the value to a writable string 74 | let value = format!("{}", value * 1000000.0); 75 | 76 | // Write the value 77 | file.map_err(|err| TDPError::FailedOperation(err.to_string()))? 78 | .write_all(value.as_bytes()) 79 | .map_err(|err| TDPError::IOError(err.to_string()))?; 80 | 81 | // Update the boost value 82 | self.set_boost(boost).await 83 | } 84 | 85 | async fn boost(&self) -> TDPResult { 86 | let path = "/sys/class/powercap/intel-rapl/intel-rapl:0/constraint_1_power_limit_uw"; 87 | let result = fs::read_to_string(path); 88 | let content = result.map_err(|err| TDPError::IOError(err.to_string()))?; 89 | let content = content.trim(); 90 | 91 | // Parse the output to get the peak TDP 92 | let peak_tdp = match content.parse::() { 93 | Ok(v) => v, 94 | Err(e) => { 95 | log::error!("{}", e); 96 | return Err(TDPError::FailedOperation(e.to_string())); 97 | } 98 | }; 99 | 100 | let tdp = self.tdp().await?; 101 | Ok((peak_tdp / 1000000.0) - tdp) 102 | } 103 | 104 | async fn set_boost(&mut self, value: f64) -> TDPResult<()> { 105 | log::debug!("Setting Boost: {}", value); 106 | if value < 0.0 { 107 | let err = "Cowardly refusing to set TDP Boost less than 0"; 108 | log::warn!("{}", err); 109 | return Err(TDPError::InvalidArgument(String::from(err))); 110 | } 111 | 112 | let tdp = self.tdp().await?; 113 | let boost = value; 114 | let short_tdp = if boost > 0.0 { 115 | (boost + tdp) * 1000000.0 116 | } else { 117 | tdp * 1000000.0 118 | }; 119 | 120 | // Write the short tdp 121 | let path = "/sys/class/powercap/intel-rapl/intel-rapl:0/constraint_1_power_limit_uw"; 122 | let file = OpenOptions::new().write(true).open(path); 123 | let value = format!("{}", short_tdp); 124 | file.map_err(|err| TDPError::FailedOperation(err.to_string()))? 125 | .write_all(value.as_bytes()) 126 | .map_err(|err| TDPError::IOError(err.to_string())) 127 | } 128 | 129 | async fn thermal_throttle_limit_c(&self) -> TDPResult { 130 | log::error!("Thermal throttling not supported on intel gpu"); 131 | Err(TDPError::FeatureUnsupported) 132 | } 133 | 134 | async fn set_thermal_throttle_limit_c(&mut self, _limit: f64) -> TDPResult<()> { 135 | log::error!("Thermal throttling not supported on intel gpu"); 136 | Err(TDPError::FeatureUnsupported) 137 | } 138 | 139 | async fn power_profile(&self) -> TDPResult { 140 | log::error!("Power profiles not supported on intel gpu"); 141 | Err(TDPError::FeatureUnsupported) 142 | } 143 | 144 | async fn set_power_profile(&mut self, _profile: String) -> TDPResult<()> { 145 | log::error!("Power profiles not supported on intel gpu"); 146 | Err(TDPError::FeatureUnsupported) 147 | } 148 | 149 | async fn power_profiles_available(&self) -> TDPResult> { 150 | log::error!("Power profiles not supported on intel gpu"); 151 | Err(TDPError::FeatureUnsupported) 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /src/performance/gpu/interface.rs: -------------------------------------------------------------------------------- 1 | use std::sync::Arc; 2 | 3 | use tokio::sync::Mutex; 4 | 5 | use crate::performance::gpu::dbus::devices::TDPDevices; 6 | 7 | pub enum GPUError { 8 | //FeatureUnsupported, 9 | FailedOperation(String), 10 | InvalidArgument(String), 11 | IOError(String), 12 | } 13 | 14 | impl From for String { 15 | fn from(_val: GPUError) -> Self { 16 | todo!() 17 | } 18 | } 19 | 20 | pub type GPUResult = Result; 21 | 22 | /// Represents the data contained in /sys/class/drm/cardX 23 | pub trait GPUDevice: Send + Sync { 24 | async fn get_tdp_interface(&self) -> Option>>; 25 | async fn get_gpu_path(&self) -> String; 26 | async fn name(&self) -> String; 27 | async fn path(&self) -> String; 28 | async fn class(&self) -> String; 29 | async fn class_id(&self) -> String; 30 | async fn vendor(&self) -> String; 31 | async fn vendor_id(&self) -> String; 32 | async fn device(&self) -> String; 33 | async fn device_id(&self) -> String; 34 | async fn subdevice(&self) -> String; 35 | async fn subdevice_id(&self) -> String; 36 | async fn subvendor_id(&self) -> String; 37 | async fn revision_id(&self) -> String; 38 | async fn clock_limit_mhz_min(&self) -> GPUResult; 39 | async fn clock_limit_mhz_max(&self) -> GPUResult; 40 | async fn clock_value_mhz_min(&self) -> GPUResult; 41 | async fn set_clock_value_mhz_min(&mut self, value: f64) -> GPUResult<()>; 42 | async fn clock_value_mhz_max(&self) -> GPUResult; 43 | async fn set_clock_value_mhz_max(&mut self, value: f64) -> GPUResult<()>; 44 | async fn manual_clock(&self) -> GPUResult; 45 | async fn set_manual_clock(&mut self, enabled: bool) -> GPUResult<()>; 46 | } 47 | -------------------------------------------------------------------------------- /src/performance/gpu/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod acpi; 2 | pub mod amd; 3 | pub mod asus; 4 | pub mod connector; 5 | pub mod dbus; 6 | pub mod intel; 7 | pub mod interface; 8 | pub mod platform; 9 | pub mod tdp; 10 | -------------------------------------------------------------------------------- /src/performance/gpu/platform/hardware.rs: -------------------------------------------------------------------------------- 1 | use crate::performance::gpu::platform::model_config::{Config, ModelConfig}; 2 | use std::collections::HashMap; 3 | use std::fs; 4 | use std::io::ErrorKind; 5 | use std::path::Path; 6 | 7 | pub struct Hardware { 8 | pub min_tdp: f64, 9 | pub max_tdp: f64, 10 | pub max_boost: f64, 11 | } 12 | 13 | impl Hardware { 14 | pub const PLATFORM_DIR: &str = "/usr/share/powerstation/platform"; 15 | pub const AMD_APU_DATABASE: &str = "amd_apu_database.toml"; 16 | pub const INTEL_APU_DATABASE: &str = "intel_apu_database.toml"; 17 | pub const DMI_OVERRIDES_APU_DATABASE: &str = "dmi_overrides_apu_database.toml"; 18 | 19 | // Enhanced new method that loads and parses configurations 20 | pub fn new() -> Option { 21 | match Self::load_and_apply_configs() { 22 | Ok(hardware) => Some(hardware), 23 | Err(e) => { 24 | log::error!("Failed to load hardware configuration: {}", e); 25 | Some(Self { 26 | min_tdp: 0.0, 27 | max_tdp: 0.0, 28 | max_boost: 0.0, 29 | }) 30 | } 31 | } 32 | } 33 | 34 | // Internal method to load and apply configurations 35 | fn load_and_apply_configs() -> Result> { 36 | let platform_dir = Path::new(Self::PLATFORM_DIR); 37 | 38 | // Read each database file 39 | let amd_db_path = platform_dir.join(Self::AMD_APU_DATABASE); 40 | let intel_db_path = platform_dir.join(Self::INTEL_APU_DATABASE); 41 | let dmi_overrides_path = platform_dir.join(Self::DMI_OVERRIDES_APU_DATABASE); 42 | 43 | let amd_config = Self::load_config_file(&amd_db_path)?; 44 | let intel_config = Self::load_config_file(&intel_db_path)?; 45 | let dmi_overrides_config = Self::load_config_file(&dmi_overrides_path)?; 46 | 47 | // Merge configurations with priority: DMI overrides > Intel > AMD 48 | let merged_config = 49 | Self::merge_configs(vec![amd_config, intel_config, dmi_overrides_config]); 50 | 51 | log::info!( 52 | "Merged configuration contains {} model configs", 53 | merged_config.models.len() 54 | ); 55 | 56 | // Create default configuration 57 | let mut hardware = Self { 58 | min_tdp: 0.0, 59 | max_tdp: 0.0, 60 | max_boost: 0.0, 61 | }; 62 | 63 | // Get current model with two-level matching strategy 64 | let current_model = Self::get_current_model()?; 65 | 66 | // Find matching model in merged configuration 67 | for model in &merged_config.models { 68 | if model.model_name == current_model { 69 | hardware.min_tdp = model.min_tdp; 70 | hardware.max_tdp = model.max_tdp; 71 | hardware.max_boost = model.max_boost; 72 | log::info!( 73 | "Applied configuration for model {}: min_tdp={}, max_tdp={}, max_boost={}", 74 | model.model_name, 75 | model.min_tdp, 76 | model.max_tdp, 77 | model.max_boost 78 | ); 79 | break; 80 | } 81 | } 82 | 83 | Ok(hardware) 84 | } 85 | 86 | // Read and parse a single TOML file 87 | fn load_config_file(file_path: &Path) -> Result> { 88 | match fs::read_to_string(file_path) { 89 | Ok(config_str) => { 90 | let config: Config = toml::from_str(&config_str)?; 91 | log::info!( 92 | "Loaded configuration file: {:?}, containing {} models", 93 | file_path, 94 | config.models.len() 95 | ); 96 | Ok(config) 97 | } 98 | Err(e) if e.kind() == ErrorKind::NotFound => { 99 | log::warn!("Configuration file does not exist: {:?}", file_path); 100 | Ok(Config { models: vec![] }) 101 | } 102 | Err(e) => Err(Box::new(e)), 103 | } 104 | } 105 | 106 | // Merge multiple Config objects 107 | fn merge_configs(configs: Vec) -> Config { 108 | let mut merged_models: HashMap = HashMap::new(); 109 | 110 | // Merge in order, later models with same name will override earlier ones 111 | for config in configs { 112 | for model in config.models { 113 | merged_models.insert(model.model_name.clone(), model); 114 | } 115 | } 116 | 117 | Config { 118 | models: merged_models.into_values().collect(), 119 | } 120 | } 121 | 122 | // Get current device model with two-level matching strategy 123 | fn get_current_model() -> Result> { 124 | // First try: Match by product_name 125 | let product_name_path = Path::new("/sys/class/dmi/id/product_name"); 126 | if product_name_path.exists() { 127 | let model = fs::read_to_string(product_name_path)?.trim().to_string(); 128 | 129 | log::info!("Found product name: {}", model); 130 | 131 | // Check if this model exists in our merged configs 132 | if Self::check_model_exists(&model)? { 133 | log::info!("Found matching configuration for product name: {}", model); 134 | return Ok(model); 135 | } else { 136 | log::info!( 137 | "No matching configuration found for product name: {}", 138 | model 139 | ); 140 | } 141 | } 142 | 143 | // Second try: Match by CPU model 144 | let cpu_info_path = Path::new("/proc/cpuinfo"); 145 | if cpu_info_path.exists() { 146 | let cpu_info = fs::read_to_string(cpu_info_path)?; 147 | 148 | // Extract CPU model name 149 | for line in cpu_info.lines() { 150 | if line.starts_with("model name") { 151 | if let Some(model) = line.split(':').nth(1) { 152 | let model = model.trim().to_string(); 153 | log::info!("Found CPU model: {}", model); 154 | 155 | // Check if this CPU model exists in our merged configs 156 | if Self::check_model_exists(&model)? { 157 | log::info!("Found matching configuration for CPU model: {}", model); 158 | return Ok(model); 159 | } else { 160 | log::info!("No matching configuration found for CPU model: {}", model); 161 | } 162 | } 163 | break; 164 | } 165 | } 166 | } 167 | 168 | // If we reach here, no matching configuration was found 169 | // Return the product name as fallback if available 170 | if product_name_path.exists() { 171 | let model = fs::read_to_string(product_name_path)?.trim().to_string(); 172 | log::warn!( 173 | "No matching configuration found, using product name as fallback: {}", 174 | model 175 | ); 176 | return Ok(model); 177 | } 178 | 179 | // Last resort fallback 180 | log::warn!("Could not determine model name, using default"); 181 | Ok("Unknown Model".to_string()) 182 | } 183 | 184 | // Helper method to check if a model exists in our configuration 185 | fn check_model_exists(model: &str) -> Result> { 186 | let platform_dir = Path::new(Self::PLATFORM_DIR); 187 | 188 | // Read each database file 189 | let amd_db_path = platform_dir.join(Self::AMD_APU_DATABASE); 190 | let intel_db_path = platform_dir.join(Self::INTEL_APU_DATABASE); 191 | let dmi_overrides_path = platform_dir.join(Self::DMI_OVERRIDES_APU_DATABASE); 192 | 193 | let amd_config = Self::load_config_file(&amd_db_path)?; 194 | let intel_config = Self::load_config_file(&intel_db_path)?; 195 | let dmi_overrides_config = Self::load_config_file(&dmi_overrides_path)?; 196 | 197 | // Merge configurations 198 | let merged_config = 199 | Self::merge_configs(vec![amd_config, intel_config, dmi_overrides_config]); 200 | 201 | // Check if model exists in merged config 202 | for config_model in &merged_config.models { 203 | if config_model.model_name == model { 204 | return Ok(true); 205 | } 206 | } 207 | 208 | Ok(false) 209 | } 210 | 211 | pub fn min_tdp(&self) -> f64 { 212 | self.min_tdp 213 | } 214 | 215 | pub fn max_tdp(&self) -> f64 { 216 | self.max_tdp 217 | } 218 | 219 | pub fn max_boost(&self) -> f64 { 220 | self.max_boost 221 | } 222 | } 223 | -------------------------------------------------------------------------------- /src/performance/gpu/platform/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod hardware; 2 | pub mod model_config; 3 | -------------------------------------------------------------------------------- /src/performance/gpu/platform/model_config.rs: -------------------------------------------------------------------------------- 1 | use serde::{Deserialize, Serialize}; 2 | 3 | #[derive(Deserialize, Serialize, Debug, Clone)] 4 | pub struct Config { 5 | pub models: Vec, 6 | } 7 | 8 | #[derive(Deserialize, Serialize, Debug, Clone)] 9 | pub struct ModelConfig { 10 | pub model_name: String, 11 | pub min_tdp: f64, 12 | pub max_tdp: f64, 13 | pub max_boost: f64, 14 | } 15 | -------------------------------------------------------------------------------- /src/performance/gpu/tdp.rs: -------------------------------------------------------------------------------- 1 | #[derive(Debug)] 2 | pub enum TDPError { 3 | FeatureUnsupported, 4 | FailedOperation(String), 5 | InvalidArgument(String), 6 | IOError(String), 7 | } 8 | 9 | impl From for String { 10 | fn from(_val: TDPError) -> Self { 11 | todo!() 12 | } 13 | } 14 | 15 | pub type TDPResult = Result; 16 | 17 | // Helper trait to simplify access to hardware information 18 | pub trait HardwareAccess { 19 | fn hardware(&self) -> Option<&crate::performance::gpu::platform::hardware::Hardware>; 20 | } 21 | 22 | pub trait TDPDevice: Sync + Send + HardwareAccess { 23 | async fn tdp(&self) -> TDPResult; 24 | async fn set_tdp(&mut self, value: f64) -> TDPResult<()>; 25 | async fn boost(&self) -> TDPResult; 26 | async fn set_boost(&mut self, value: f64) -> TDPResult<()>; 27 | async fn thermal_throttle_limit_c(&self) -> TDPResult; 28 | async fn set_thermal_throttle_limit_c(&mut self, limit: f64) -> TDPResult<()>; 29 | async fn power_profile(&self) -> TDPResult; 30 | async fn power_profiles_available(&self) -> TDPResult>; 31 | async fn set_power_profile(&mut self, profile: String) -> TDPResult<()>; 32 | 33 | // Default implementations for hardware-based methods 34 | async fn min_tdp(&self) -> TDPResult { 35 | log::info!("Get TDP Min"); 36 | if let Some(hardware) = self.hardware() { 37 | return Ok(hardware.min_tdp()); 38 | } 39 | Err(TDPError::FailedOperation( 40 | "No Hardware interface available to read min TDP.".into(), 41 | )) 42 | } 43 | 44 | async fn max_tdp(&self) -> TDPResult { 45 | log::info!("Get TDP Max"); 46 | if let Some(hardware) = self.hardware() { 47 | return Ok(hardware.max_tdp()); 48 | } 49 | Err(TDPError::FailedOperation( 50 | "No Hardware interface available to read max TDP.".into(), 51 | )) 52 | } 53 | 54 | async fn max_boost(&self) -> TDPResult { 55 | log::info!("Get TDP Max Boost"); 56 | if let Some(hardware) = self.hardware() { 57 | return Ok(hardware.max_boost()); 58 | } 59 | Err(TDPError::FailedOperation( 60 | "No Hardware interface available to read max boost.".into(), 61 | )) 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/performance/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod cpu; 2 | pub mod gpu; 3 | --------------------------------------------------------------------------------