^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2020 Advanced Micro Devices, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <asm/cpu_device_id.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/bits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/cpumask.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/topology.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define DRVNAME "amd_energy"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define ENERGY_PWR_UNIT_MSR 0xC0010299
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define ENERGY_CORE_MSR 0xC001029A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define ENERGY_PKG_MSR 0xC001029B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define AMD_ENERGY_UNIT_MASK 0x01F00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define AMD_ENERGY_MASK 0xFFFFFFFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct sensor_accumulator {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) u64 energy_ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) u64 prev_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct amd_energy_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct hwmon_channel_info energy_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) const struct hwmon_channel_info *info[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct hwmon_chip_info chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct task_struct *wrap_accumulate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* Lock around the accumulator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* An accumulator for each core and socket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct sensor_accumulator *accums;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) unsigned int timeout_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* Energy Status Units */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int energy_units;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int nr_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int nr_socks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int core_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) char (*label)[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static int amd_energy_read_labels(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) enum hwmon_sensor_types type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) u32 attr, int channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) const char **str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct amd_energy_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *str = data->label[channel];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static void get_energy_units(struct amd_energy_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) u64 rapl_units;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) rdmsrl_safe(ENERGY_PWR_UNIT_MSR, &rapl_units);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) data->energy_units = (rapl_units & AMD_ENERGY_UNIT_MASK) >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static void accumulate_delta(struct amd_energy_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int channel, int cpu, u32 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct sensor_accumulator *accum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) u64 input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) rdmsrl_safe_on_cpu(cpu, reg, &input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) input &= AMD_ENERGY_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) accum = &data->accums[channel];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (input >= accum->prev_value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) accum->energy_ctr +=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) input - accum->prev_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) accum->energy_ctr += UINT_MAX -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) accum->prev_value + input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) accum->prev_value = input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static void read_accumulate(struct amd_energy_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int sock, scpu, cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) for (sock = 0; sock < data->nr_socks; sock++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) scpu = cpumask_first_and(cpu_online_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) cpumask_of_node(sock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) accumulate_delta(data, data->nr_cpus + sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) scpu, ENERGY_PKG_MSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (data->core_id >= data->nr_cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) data->core_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) cpu = data->core_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (cpu_online(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) accumulate_delta(data, cpu, cpu, ENERGY_CORE_MSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) data->core_id++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static void amd_add_delta(struct amd_energy_data *data, int ch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) int cpu, long *val, u32 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct sensor_accumulator *accum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) u64 input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) mutex_lock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) rdmsrl_safe_on_cpu(cpu, reg, &input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) input &= AMD_ENERGY_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) accum = &data->accums[ch];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (input >= accum->prev_value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) input += accum->energy_ctr -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) accum->prev_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) input += UINT_MAX - accum->prev_value +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) accum->energy_ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* Energy consumed = (1/(2^ESU) * RAW * 1000000UL) μJoules */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) *val = div64_ul(input * 1000000UL, BIT(data->energy_units));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) mutex_unlock(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int amd_energy_read(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) enum hwmon_sensor_types type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) u32 attr, int channel, long *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct amd_energy_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) u32 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (channel >= data->nr_cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) cpu = cpumask_first_and(cpu_online_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) cpumask_of_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) (channel - data->nr_cpus));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) reg = ENERGY_PKG_MSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) cpu = channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!cpu_online(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) reg = ENERGY_CORE_MSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) amd_add_delta(data, channel, cpu, val, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static umode_t amd_energy_is_visible(const void *_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) enum hwmon_sensor_types type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) u32 attr, int channel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return 0440;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static int energy_accumulator(void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct amd_energy_data *data = (struct amd_energy_data *)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) unsigned int timeout = data->timeout_ms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) while (!kthread_should_stop()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * Ignoring the conditions such as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * cpu being offline or rdmsr failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) read_accumulate(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) set_current_state(TASK_INTERRUPTIBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (kthread_should_stop())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) schedule_timeout(msecs_to_jiffies(timeout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static const struct hwmon_ops amd_energy_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) .is_visible = amd_energy_is_visible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) .read = amd_energy_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) .read_string = amd_energy_read_labels,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static int amd_create_sensor(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) struct amd_energy_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) enum hwmon_sensor_types type, u32 config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct hwmon_channel_info *info = &data->energy_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct sensor_accumulator *accums;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) int i, num_siblings, cpus, sockets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) u32 *s_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) char (*label_l)[10];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /* Identify the number of siblings per core */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) num_siblings = ((cpuid_ebx(0x8000001e) >> 8) & 0xff) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) sockets = num_possible_nodes();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * Energy counter register is accessed at core level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * Hence, filterout the siblings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) cpus = num_present_cpus() / num_siblings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) s_config = devm_kcalloc(dev, cpus + sockets + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) sizeof(u32), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (!s_config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) accums = devm_kcalloc(dev, cpus + sockets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) sizeof(struct sensor_accumulator),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (!accums)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) label_l = devm_kcalloc(dev, cpus + sockets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) sizeof(*label_l), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (!label_l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) info->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) info->config = s_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) data->nr_cpus = cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) data->nr_socks = sockets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) data->accums = accums;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) data->label = label_l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) for (i = 0; i < cpus + sockets; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) s_config[i] = config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (i < cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) scnprintf(label_l[i], 10, "Ecore%03u", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) scnprintf(label_l[i], 10, "Esocket%u", (i - cpus));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) s_config[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static int amd_energy_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct amd_energy_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) data = devm_kzalloc(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) sizeof(struct amd_energy_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) data->chip.ops = &amd_energy_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) data->chip.info = data->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) dev_set_drvdata(dev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) /* Populate per-core energy reporting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) data->info[0] = &data->energy_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) ret = amd_create_sensor(dev, data, hwmon_energy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) HWMON_E_INPUT | HWMON_E_LABEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) mutex_init(&data->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) get_energy_units(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) hwmon_dev = devm_hwmon_device_register_with_info(dev, DRVNAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) &data->chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (IS_ERR(hwmon_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return PTR_ERR(hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * On a system with peak wattage of 250W
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * timeout = 2 ^ 32 / 2 ^ energy_units / 250 secs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) data->timeout_ms = 1000 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) BIT(min(28, 31 - data->energy_units)) / 250;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) data->wrap_accumulate = kthread_run(energy_accumulator, data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) "%s", dev_name(hwmon_dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return PTR_ERR_OR_ZERO(data->wrap_accumulate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static int amd_energy_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) struct amd_energy_data *data = dev_get_drvdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (data && data->wrap_accumulate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) kthread_stop(data->wrap_accumulate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static const struct platform_device_id amd_energy_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) { .name = DRVNAME, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) MODULE_DEVICE_TABLE(platform, amd_energy_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static struct platform_driver amd_energy_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) .probe = amd_energy_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) .remove = amd_energy_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) .id_table = amd_energy_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) .name = DRVNAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) static struct platform_device *amd_energy_platdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) static const struct x86_cpu_id cpu_ids[] __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) X86_MATCH_VENDOR_FAM_MODEL(AMD, 0x17, 0x31, NULL),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) MODULE_DEVICE_TABLE(x86cpu, cpu_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static int __init amd_energy_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (!x86_match_cpu(cpu_ids))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) ret = platform_driver_register(&amd_energy_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) amd_energy_platdev = platform_device_alloc(DRVNAME, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (!amd_energy_platdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) platform_driver_unregister(&amd_energy_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) ret = platform_device_add(amd_energy_platdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) platform_device_put(amd_energy_platdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) platform_driver_unregister(&amd_energy_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static void __exit amd_energy_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) platform_device_unregister(amd_energy_platdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) platform_driver_unregister(&amd_energy_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) module_init(amd_energy_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) module_exit(amd_energy_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) MODULE_DESCRIPTION("Driver for AMD Energy reporting from RAPL MSR via HWMON interface");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) MODULE_AUTHOR("Naveen Krishna Chatradhi <nchatrad@amd.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) MODULE_LICENSE("GPL");