omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / research / m15 / spbm / aien_spbm_readonly.c
| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* Machine-pinned GB10 SPBM measurement reader. |
| 3 | * The ACPI discovery protocol and milli-unit convention were documented by |
| 4 | * antheas/spark_hwmon (GPL-2.0). This reader checks the local firmware's |
| 5 | * contract, exclusively claims its resource, and exposes no writes. |
| 6 | * It never accesses the firmware's CLEAR_OVERFLOW registers. |
| 7 | */ |
| 8 | #include <linux/acpi.h> |
| 9 | #include <linux/hwmon.h> |
| 10 | #include <linux/hwmon-sysfs.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include "contract.h" |
| 16 | |
| 17 | struct aien_spbm { void __iomem *base; }; |
| 18 | static const char * const energy_labels[] = { |
| 19 | "pkg", "cpu_e", "cpu_p", "gpc_unverified", "gpm" |
| 20 | }; |
| 21 | static const char * const power_labels[] = { |
| 22 | "sys_total", "soc_pkg", "cpu_e", "cpu_p", "gpu" |
| 23 | }; |
| 24 | static const guid_t mtel_guid = GUID_INIT(0x12345678, 0x1234, 0x1234, |
| 25 | 0x12, 0x34, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc); |
| 26 | |
| 27 | static int spbm_resource_index(acpi_handle handle) |
| 28 | { |
| 29 | union acpi_object *names; |
| 30 | unsigned int i; |
| 31 | int index = -ENODEV; |
| 32 | names = acpi_evaluate_dsm(handle, &mtel_guid, 0, 1, NULL); |
| 33 | if (!names) |
| 34 | return -ENODEV; |
| 35 | if (names->type != ACPI_TYPE_PACKAGE) |
| 36 | goto out; |
| 37 | for (i = 0; i < names->package.count; ++i) { |
| 38 | union acpi_object *name = &names->package.elements[i]; |
| 39 | if (name->type != ACPI_TYPE_STRING || name->string.length != 4 || |
| 40 | memcmp(name->string.pointer, "SPBM", 4)) |
| 41 | continue; |
| 42 | if (index >= 0) { |
| 43 | index = -EINVAL; |
| 44 | break; |
| 45 | } |
| 46 | index = i; |
| 47 | } |
| 48 | out: |
| 49 | ACPI_FREE(names); |
| 50 | return index; |
| 51 | } |
| 52 | |
| 53 | static int spbm_check_registers(acpi_handle handle, unsigned int index) |
| 54 | { |
| 55 | union acpi_object arg = { .type = ACPI_TYPE_INTEGER }; |
| 56 | union acpi_object args = { .type = ACPI_TYPE_PACKAGE }; |
| 57 | union acpi_object *map; |
| 58 | unsigned long seen = 0; |
| 59 | unsigned int i, j; |
| 60 | int ret = -EINVAL; |
| 61 | arg.integer.value = index; |
| 62 | args.package.count = 1; |
| 63 | args.package.elements = &arg; |
| 64 | map = acpi_evaluate_dsm(handle, &mtel_guid, 0, 2, &args); |
| 65 | if (!map) |
| 66 | return -ENODEV; |
| 67 | if (map->type != ACPI_TYPE_PACKAGE) |
| 68 | goto out; |
| 69 | for (i = 0; i < map->package.count; ++i) { |
| 70 | union acpi_object *group = &map->package.elements[i]; |
| 71 | union acpi_object *entry; |
| 72 | if (group->type != ACPI_TYPE_PACKAGE || !group->package.count) |
| 73 | goto out; |
| 74 | entry = group->package.elements; |
| 75 | if (entry[0].type != ACPI_TYPE_INTEGER || |
| 76 | (group->package.count - 1) % 2 || |
| 77 | entry[0].integer.value != (group->package.count - 1) / 2) |
| 78 | goto out; |
| 79 | for (j = 1; j < group->package.count; j += 2) { |
| 80 | if (entry[j].type != ACPI_TYPE_STRING || |
| 81 | entry[j + 1].type != ACPI_TYPE_INTEGER || |
| 82 | spbm_contract_add(&seen, entry[j].string.pointer, |
| 83 | entry[j].string.length, entry[j + 1].integer.value) < 0) |
| 84 | goto out; |
| 85 | } |
| 86 | } |
| 87 | if (seen == SPBM_ALL_REGS) |
| 88 | ret = 0; |
| 89 | out: |
| 90 | ACPI_FREE(map); |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | static u32 spbm_read_reg(struct aien_spbm *spbm, unsigned int index) |
| 95 | { |
| 96 | return readl(spbm->base + spbm_registers[index].offset); |
| 97 | } |
| 98 | |
| 99 | static umode_t aien_spbm_visible(const void *data, |
| 100 | enum hwmon_sensor_types type, u32 attr, int channel) |
| 101 | { |
| 102 | if (channel < 0) |
| 103 | return 0; |
| 104 | if (type == hwmon_energy && channel < SPBM_N_ENERGY && |
| 105 | (attr == hwmon_energy_input || attr == hwmon_energy_label)) |
| 106 | return 0444; |
| 107 | if (type == hwmon_power && channel < SPBM_N_POWER && |
| 108 | (attr == hwmon_power_input || attr == hwmon_power_label)) |
| 109 | return 0444; |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int aien_spbm_read(struct device *dev, enum hwmon_sensor_types type, |
| 114 | u32 attr, int channel, long *value) |
| 115 | { |
| 116 | struct aien_spbm *spbm = dev_get_drvdata(dev); |
| 117 | u32 raw; |
| 118 | if (channel < 0) |
| 119 | return -EOPNOTSUPP; |
| 120 | if (type == hwmon_energy && attr == hwmon_energy_input && |
| 121 | channel < SPBM_N_ENERGY) { |
| 122 | /* Overflow semantics are undocumented. Never silently unwrap it. */ |
| 123 | if (spbm_read_reg(spbm, SPBM_OVERFLOW_FIRST + channel)) |
| 124 | return -EOVERFLOW; |
| 125 | raw = spbm_read_reg(spbm, channel); |
| 126 | if (spbm_read_reg(spbm, SPBM_OVERFLOW_FIRST + channel)) |
| 127 | return -EOVERFLOW; |
| 128 | } else if (type == hwmon_power && attr == hwmon_power_input && |
| 129 | channel < SPBM_N_POWER) { |
| 130 | raw = spbm_read_reg(spbm, SPBM_POWER_FIRST + channel); |
| 131 | } else { |
| 132 | return -EOPNOTSUPP; |
| 133 | } |
| 134 | *value = (long)raw * 1000L; |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int aien_spbm_read_string(struct device *dev, |
| 139 | enum hwmon_sensor_types type, u32 attr, |
| 140 | int channel, const char **value) |
| 141 | { |
| 142 | if (channel < 0) |
| 143 | return -EOPNOTSUPP; |
| 144 | if (type == hwmon_energy && attr == hwmon_energy_label && |
| 145 | channel < SPBM_N_ENERGY) |
| 146 | *value = energy_labels[channel]; |
| 147 | else if (type == hwmon_power && attr == hwmon_power_label && |
| 148 | channel < SPBM_N_POWER) |
| 149 | *value = power_labels[channel]; |
| 150 | else |
| 151 | return -EOPNOTSUPP; |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static ssize_t overflow_show(struct device *dev, struct device_attribute *attr, |
| 156 | char *buf) |
| 157 | { |
| 158 | struct aien_spbm *spbm = dev_get_drvdata(dev); |
| 159 | unsigned int index = to_sensor_dev_attr(attr)->index; |
| 160 | return sysfs_emit(buf, "%u\n", spbm_read_reg(spbm, SPBM_OVERFLOW_FIRST + index)); |
| 161 | } |
| 162 | static SENSOR_DEVICE_ATTR_RO(energy1_overflow_raw, overflow, 0); |
| 163 | static SENSOR_DEVICE_ATTR_RO(energy2_overflow_raw, overflow, 1); |
| 164 | static SENSOR_DEVICE_ATTR_RO(energy3_overflow_raw, overflow, 2); |
| 165 | static SENSOR_DEVICE_ATTR_RO(energy4_overflow_raw, overflow, 3); |
| 166 | static SENSOR_DEVICE_ATTR_RO(energy5_overflow_raw, overflow, 4); |
| 167 | static struct attribute *aien_spbm_attrs[] = { |
| 168 | &sensor_dev_attr_energy1_overflow_raw.dev_attr.attr, |
| 169 | &sensor_dev_attr_energy2_overflow_raw.dev_attr.attr, |
| 170 | &sensor_dev_attr_energy3_overflow_raw.dev_attr.attr, |
| 171 | &sensor_dev_attr_energy4_overflow_raw.dev_attr.attr, |
| 172 | &sensor_dev_attr_energy5_overflow_raw.dev_attr.attr, NULL |
| 173 | }; |
| 174 | ATTRIBUTE_GROUPS(aien_spbm); |
| 175 | static const struct hwmon_ops aien_spbm_ops = { |
| 176 | .is_visible = aien_spbm_visible, .read = aien_spbm_read, |
| 177 | .read_string = aien_spbm_read_string, |
| 178 | }; |
| 179 | static const struct hwmon_channel_info * const aien_spbm_info[] = { |
| 180 | HWMON_CHANNEL_INFO(energy, |
| 181 | HWMON_E_INPUT | HWMON_E_LABEL, HWMON_E_INPUT | HWMON_E_LABEL, |
| 182 | HWMON_E_INPUT | HWMON_E_LABEL, HWMON_E_INPUT | HWMON_E_LABEL, |
| 183 | HWMON_E_INPUT | HWMON_E_LABEL), |
| 184 | HWMON_CHANNEL_INFO(power, |
| 185 | HWMON_P_INPUT | HWMON_P_LABEL, HWMON_P_INPUT | HWMON_P_LABEL, |
| 186 | HWMON_P_INPUT | HWMON_P_LABEL, HWMON_P_INPUT | HWMON_P_LABEL, |
| 187 | HWMON_P_INPUT | HWMON_P_LABEL), NULL, |
| 188 | }; |
| 189 | static const struct hwmon_chip_info aien_spbm_chip_info = { |
| 190 | .ops = &aien_spbm_ops, .info = aien_spbm_info, |
| 191 | }; |
| 192 | |
| 193 | static int aien_spbm_probe(struct platform_device *pdev) |
| 194 | { |
| 195 | struct device *dev = &pdev->dev; |
| 196 | struct aien_spbm *spbm; |
| 197 | struct resource *res; |
| 198 | struct device *hwmon; |
| 199 | int index, ret; |
| 200 | BUILD_BUG_ON(sizeof(long) < 8); |
| 201 | if (!ACPI_HANDLE(dev)) |
| 202 | return -ENODEV; |
| 203 | index = spbm_resource_index(ACPI_HANDLE(dev)); |
| 204 | if (index < 0) |
| 205 | return index; |
| 206 | res = platform_get_resource(pdev, IORESOURCE_MEM, index); |
| 207 | if (!res || res->start != SPBM_PHYS || resource_size(res) != SPBM_SIZE) |
| 208 | return dev_err_probe(dev, -ENODEV, "SPBM resource contract mismatch\n"); |
| 209 | ret = spbm_check_registers(ACPI_HANDLE(dev), index); |
| 210 | if (ret) |
| 211 | return dev_err_probe(dev, ret, "SPBM register contract mismatch\n"); |
| 212 | spbm = devm_kzalloc(dev, sizeof(*spbm), GFP_KERNEL); |
| 213 | if (!spbm) |
| 214 | return -ENOMEM; |
| 215 | spbm->base = devm_ioremap_resource(dev, res); |
| 216 | if (IS_ERR(spbm->base)) |
| 217 | return PTR_ERR(spbm->base); |
| 218 | hwmon = devm_hwmon_device_register_with_info(dev, "aien_spbm", spbm, |
| 219 | &aien_spbm_chip_info, aien_spbm_groups); |
| 220 | if (IS_ERR(hwmon)) |
| 221 | return PTR_ERR(hwmon); |
| 222 | dev_info(dev, "firmware contract verified; read-only SPBM telemetry ready\n"); |
| 223 | return 0; |
| 224 | } |
| 225 | static const struct acpi_device_id aien_spbm_ids[] = { |
| 226 | { "NVDA8800", 0 }, { } |
| 227 | }; |
| 228 | MODULE_DEVICE_TABLE(acpi, aien_spbm_ids); |
| 229 | static struct platform_driver aien_spbm_driver = { |
| 230 | .probe = aien_spbm_probe, |
| 231 | .driver = { |
| 232 | .name = "aien_spbm_readonly", .acpi_match_table = aien_spbm_ids, |
| 233 | }, |
| 234 | }; |
| 235 | module_platform_driver(aien_spbm_driver); |
| 236 | MODULE_DESCRIPTION("Read-only, firmware-checked GB10 SPBM telemetry for R15"); |
| 237 | MODULE_LICENSE("GPL"); |