1 // SPDX-License-Identifier: GPL-2.0-only
3 * intel_rapl_tpmi: Intel RAPL driver via TPMI interface
5 * Copyright (c) 2023, Intel Corporation.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/auxiliary_bus.h>
13 #include <linux/intel_tpmi.h>
14 #include <linux/intel_rapl.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
18 #define TPMI_RAPL_VERSION 1
20 /* 1 header + 10 registers + 5 reserved. 8 bytes for each. */
21 #define TPMI_RAPL_DOMAIN_SIZE 128
23 enum tpmi_rapl_domain_type {
24 TPMI_RAPL_DOMAIN_INVALID,
25 TPMI_RAPL_DOMAIN_SYSTEM,
26 TPMI_RAPL_DOMAIN_PACKAGE,
27 TPMI_RAPL_DOMAIN_RESERVED,
28 TPMI_RAPL_DOMAIN_MEMORY,
32 enum tpmi_rapl_register {
39 TPMI_RAPL_REG_RESERVED,
40 TPMI_RAPL_REG_ENERGY_STATUS,
41 TPMI_RAPL_REG_PERF_STATUS,
42 TPMI_RAPL_REG_POWER_INFO,
43 TPMI_RAPL_REG_INTERRUPT,
44 TPMI_RAPL_REG_MAX = 15,
47 struct tpmi_rapl_package {
48 struct rapl_if_priv priv;
49 struct intel_tpmi_plat_info *tpmi_info;
50 struct rapl_package *rp;
52 struct list_head node;
55 static LIST_HEAD(tpmi_rapl_packages);
56 static DEFINE_MUTEX(tpmi_rapl_lock);
58 static struct powercap_control_type *tpmi_control_type;
60 static int tpmi_rapl_read_raw(int id, struct reg_action *ra)
65 ra->value = readq(ra->reg.mmio);
67 ra->value &= ra->mask;
71 static int tpmi_rapl_write_raw(int id, struct reg_action *ra)
78 val = readq(ra->reg.mmio);
83 writeq(val, ra->reg.mmio);
87 static struct tpmi_rapl_package *trp_alloc(int pkg_id)
89 struct tpmi_rapl_package *trp;
92 mutex_lock(&tpmi_rapl_lock);
94 if (list_empty(&tpmi_rapl_packages)) {
95 tpmi_control_type = powercap_register_control_type(NULL, "intel-rapl", NULL);
96 if (IS_ERR(tpmi_control_type)) {
97 ret = PTR_ERR(tpmi_control_type);
102 trp = kzalloc(sizeof(*trp), GFP_KERNEL);
105 goto err_del_powercap;
108 list_add(&trp->node, &tpmi_rapl_packages);
110 mutex_unlock(&tpmi_rapl_lock);
114 if (list_empty(&tpmi_rapl_packages))
115 powercap_unregister_control_type(tpmi_control_type);
117 mutex_unlock(&tpmi_rapl_lock);
121 static void trp_release(struct tpmi_rapl_package *trp)
123 mutex_lock(&tpmi_rapl_lock);
124 list_del(&trp->node);
126 if (list_empty(&tpmi_rapl_packages))
127 powercap_unregister_control_type(tpmi_control_type);
130 mutex_unlock(&tpmi_rapl_lock);
133 static int parse_one_domain(struct tpmi_rapl_package *trp, u32 offset)
135 u8 tpmi_domain_version;
136 enum rapl_domain_type domain_type;
137 enum tpmi_rapl_domain_type tpmi_domain_type;
138 enum tpmi_rapl_register reg_index;
139 enum rapl_domain_reg_id reg_id;
140 int tpmi_domain_size, tpmi_domain_flags;
141 u64 tpmi_domain_header = readq(trp->base + offset);
143 /* Domain Parent bits are ignored for now */
144 tpmi_domain_version = tpmi_domain_header & 0xff;
145 tpmi_domain_type = tpmi_domain_header >> 8 & 0xff;
146 tpmi_domain_size = tpmi_domain_header >> 16 & 0xff;
147 tpmi_domain_flags = tpmi_domain_header >> 32 & 0xffff;
149 if (tpmi_domain_version != TPMI_RAPL_VERSION) {
150 pr_warn(FW_BUG "Unsupported version:%d\n", tpmi_domain_version);
154 /* Domain size: in unit of 128 Bytes */
155 if (tpmi_domain_size != 1) {
156 pr_warn(FW_BUG "Invalid Domain size %d\n", tpmi_domain_size);
160 /* Unit register and Energy Status register are mandatory for each domain */
161 if (!(tpmi_domain_flags & BIT(TPMI_RAPL_REG_UNIT)) ||
162 !(tpmi_domain_flags & BIT(TPMI_RAPL_REG_ENERGY_STATUS))) {
163 pr_warn(FW_BUG "Invalid Domain flag 0x%x\n", tpmi_domain_flags);
167 switch (tpmi_domain_type) {
168 case TPMI_RAPL_DOMAIN_PACKAGE:
169 domain_type = RAPL_DOMAIN_PACKAGE;
171 case TPMI_RAPL_DOMAIN_SYSTEM:
172 domain_type = RAPL_DOMAIN_PLATFORM;
174 case TPMI_RAPL_DOMAIN_MEMORY:
175 domain_type = RAPL_DOMAIN_DRAM;
178 pr_warn(FW_BUG "Unsupported Domain type %d\n", tpmi_domain_type);
182 if (trp->priv.regs[domain_type][RAPL_DOMAIN_REG_UNIT].mmio) {
183 pr_warn(FW_BUG "Duplicate Domain type %d\n", tpmi_domain_type);
187 reg_index = TPMI_RAPL_REG_HEADER;
188 while (++reg_index != TPMI_RAPL_REG_MAX) {
189 if (!(tpmi_domain_flags & BIT(reg_index)))
193 case TPMI_RAPL_REG_UNIT:
194 reg_id = RAPL_DOMAIN_REG_UNIT;
196 case TPMI_RAPL_REG_PL1:
197 reg_id = RAPL_DOMAIN_REG_LIMIT;
198 trp->priv.limits[domain_type] |= BIT(POWER_LIMIT1);
200 case TPMI_RAPL_REG_PL2:
201 reg_id = RAPL_DOMAIN_REG_PL2;
202 trp->priv.limits[domain_type] |= BIT(POWER_LIMIT2);
204 case TPMI_RAPL_REG_PL4:
205 reg_id = RAPL_DOMAIN_REG_PL4;
206 trp->priv.limits[domain_type] |= BIT(POWER_LIMIT4);
208 case TPMI_RAPL_REG_ENERGY_STATUS:
209 reg_id = RAPL_DOMAIN_REG_STATUS;
211 case TPMI_RAPL_REG_PERF_STATUS:
212 reg_id = RAPL_DOMAIN_REG_PERF;
214 case TPMI_RAPL_REG_POWER_INFO:
215 reg_id = RAPL_DOMAIN_REG_INFO;
220 trp->priv.regs[domain_type][reg_id].mmio = trp->base + offset + reg_index * 8;
226 static int intel_rapl_tpmi_probe(struct auxiliary_device *auxdev,
227 const struct auxiliary_device_id *id)
229 struct tpmi_rapl_package *trp;
230 struct intel_tpmi_plat_info *info;
231 struct resource *res;
235 info = tpmi_get_platform_data(auxdev);
239 trp = trp_alloc(info->package_id);
243 if (tpmi_get_resource_count(auxdev) > 1) {
244 dev_err(&auxdev->dev, "does not support multiple resources\n");
249 res = tpmi_get_resource_at_index(auxdev, 0);
251 dev_err(&auxdev->dev, "can't fetch device resource info\n");
256 trp->base = devm_ioremap_resource(&auxdev->dev, res);
257 if (IS_ERR(trp->base)) {
258 ret = PTR_ERR(trp->base);
262 for (offset = 0; offset < resource_size(res); offset += TPMI_RAPL_DOMAIN_SIZE) {
263 ret = parse_one_domain(trp, offset);
268 trp->tpmi_info = info;
269 trp->priv.type = RAPL_IF_TPMI;
270 trp->priv.read_raw = tpmi_rapl_read_raw;
271 trp->priv.write_raw = tpmi_rapl_write_raw;
272 trp->priv.control_type = tpmi_control_type;
274 /* RAPL TPMI I/F is per physical package */
275 trp->rp = rapl_find_package_domain(info->package_id, &trp->priv, false);
277 dev_err(&auxdev->dev, "Domain for Package%d already exists\n", info->package_id);
282 trp->rp = rapl_add_package(info->package_id, &trp->priv, false);
283 if (IS_ERR(trp->rp)) {
284 dev_err(&auxdev->dev, "Failed to add RAPL Domain for Package%d, %ld\n",
285 info->package_id, PTR_ERR(trp->rp));
286 ret = PTR_ERR(trp->rp);
290 auxiliary_set_drvdata(auxdev, trp);
298 static void intel_rapl_tpmi_remove(struct auxiliary_device *auxdev)
300 struct tpmi_rapl_package *trp = auxiliary_get_drvdata(auxdev);
302 rapl_remove_package(trp->rp);
306 static const struct auxiliary_device_id intel_rapl_tpmi_ids[] = {
307 {.name = "intel_vsec.tpmi-rapl" },
311 MODULE_DEVICE_TABLE(auxiliary, intel_rapl_tpmi_ids);
313 static struct auxiliary_driver intel_rapl_tpmi_driver = {
314 .probe = intel_rapl_tpmi_probe,
315 .remove = intel_rapl_tpmi_remove,
316 .id_table = intel_rapl_tpmi_ids,
319 module_auxiliary_driver(intel_rapl_tpmi_driver)
321 MODULE_IMPORT_NS(INTEL_TPMI);
323 MODULE_DESCRIPTION("Intel RAPL TPMI Driver");
324 MODULE_LICENSE("GPL");