blob: 521534d5c1e5f786668a33b9deb28718d76d99b0 [file] [log] [blame]
Thomas Gleixner6e7c1092019-05-20 09:18:57 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Andreas Herrmann512d1022011-05-25 20:43:31 +02002/*
3 * fam15h_power.c - AMD Family 15h processor power monitoring
4 *
Huang Ruia6e232f2016-04-06 15:44:14 +08005 * Copyright (c) 2011-2016 Advanced Micro Devices, Inc.
Andreas Herrmannd034fbf2012-10-29 18:50:47 +01006 * Author: Andreas Herrmann <herrmann.der.user@googlemail.com>
Andreas Herrmann512d1022011-05-25 20:43:31 +02007 */
8
9#include <linux/err.h>
10#include <linux/hwmon.h>
11#include <linux/hwmon-sysfs.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/pci.h>
15#include <linux/bitops.h>
Huang Ruifa7943442016-04-06 15:44:11 +080016#include <linux/cpu.h>
17#include <linux/cpumask.h>
Huang Rui11bf0d72016-04-06 15:44:13 +080018#include <linux/time.h>
19#include <linux/sched.h>
Andreas Herrmann512d1022011-05-25 20:43:31 +020020#include <asm/processor.h>
Huang Rui3b5ea472015-10-30 17:56:57 +080021#include <asm/msr.h>
Andreas Herrmann512d1022011-05-25 20:43:31 +020022
23MODULE_DESCRIPTION("AMD Family 15h CPU processor power monitor");
Andreas Herrmannd034fbf2012-10-29 18:50:47 +010024MODULE_AUTHOR("Andreas Herrmann <herrmann.der.user@googlemail.com>");
Andreas Herrmann512d1022011-05-25 20:43:31 +020025MODULE_LICENSE("GPL");
26
27/* D18F3 */
28#define REG_NORTHBRIDGE_CAP 0xe8
29
30/* D18F4 */
31#define REG_PROCESSOR_TDP 0x1b8
32
33/* D18F5 */
34#define REG_TDP_RUNNING_AVERAGE 0xe0
35#define REG_TDP_LIMIT3 0xe8
36
Huang Rui7deb14b2015-10-30 17:56:55 +080037#define FAM15H_MIN_NUM_ATTRS 2
38#define FAM15H_NUM_GROUPS 2
Huang Ruifa7943442016-04-06 15:44:11 +080039#define MAX_CUS 8
Huang Rui7deb14b2015-10-30 17:56:55 +080040
Huang Rui11bf0d72016-04-06 15:44:13 +080041/* set maximum interval as 1 second */
42#define MAX_INTERVAL 1000
43
Huang Ruieff2a942015-12-10 11:56:10 +080044#define PCI_DEVICE_ID_AMD_15H_M70H_NB_F4 0x15b4
45
Andreas Herrmann512d1022011-05-25 20:43:31 +020046struct fam15h_power_data {
Axel Lin562dc972014-06-19 23:29:11 +080047 struct pci_dev *pdev;
Andreas Herrmann512d1022011-05-25 20:43:31 +020048 unsigned int tdp_to_watts;
49 unsigned int base_tdp;
50 unsigned int processor_pwr_watts;
Huang Rui1ed32162015-08-27 16:07:38 +080051 unsigned int cpu_pwr_sample_ratio;
Huang Rui7deb14b2015-10-30 17:56:55 +080052 const struct attribute_group *groups[FAM15H_NUM_GROUPS];
53 struct attribute_group group;
Huang Rui3b5ea472015-10-30 17:56:57 +080054 /* maximum accumulated power of a compute unit */
55 u64 max_cu_acc_power;
Huang Ruifa7943442016-04-06 15:44:11 +080056 /* accumulated power of the compute units */
57 u64 cu_acc_power[MAX_CUS];
Huang Ruicdb9e112016-04-06 15:44:12 +080058 /* performance timestamp counter */
59 u64 cpu_sw_pwr_ptsc[MAX_CUS];
Huang Rui11bf0d72016-04-06 15:44:13 +080060 /* online/offline status of current compute unit */
61 int cu_on[MAX_CUS];
62 unsigned long power_period;
Andreas Herrmann512d1022011-05-25 20:43:31 +020063};
64
Huang Rui1d28e012016-04-06 15:44:15 +080065static bool is_carrizo_or_later(void)
66{
67 return boot_cpu_data.x86 == 0x15 && boot_cpu_data.x86_model >= 0x60;
68}
69
Julia Lawalld013f7f2016-12-22 13:05:34 +010070static ssize_t power1_input_show(struct device *dev,
71 struct device_attribute *attr, char *buf)
Andreas Herrmann512d1022011-05-25 20:43:31 +020072{
73 u32 val, tdp_limit, running_avg_range;
74 s32 running_avg_capture;
75 u64 curr_pwr_watts;
Andreas Herrmann512d1022011-05-25 20:43:31 +020076 struct fam15h_power_data *data = dev_get_drvdata(dev);
Axel Lin562dc972014-06-19 23:29:11 +080077 struct pci_dev *f4 = data->pdev;
Andreas Herrmann512d1022011-05-25 20:43:31 +020078
79 pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
80 REG_TDP_RUNNING_AVERAGE, &val);
Huang Ruie9cd4d552015-08-27 16:07:35 +080081
82 /*
83 * On Carrizo and later platforms, TdpRunAvgAccCap bit field
84 * is extended to 4:31 from 4:25.
85 */
Huang Rui1d28e012016-04-06 15:44:15 +080086 if (is_carrizo_or_later()) {
Huang Ruie9cd4d552015-08-27 16:07:35 +080087 running_avg_capture = val >> 4;
88 running_avg_capture = sign_extend32(running_avg_capture, 27);
89 } else {
90 running_avg_capture = (val >> 4) & 0x3fffff;
91 running_avg_capture = sign_extend32(running_avg_capture, 21);
92 }
93
Andre Przywara941a9562012-03-23 10:02:17 +010094 running_avg_range = (val & 0xf) + 1;
Andreas Herrmann512d1022011-05-25 20:43:31 +020095
96 pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
97 REG_TDP_LIMIT3, &val);
98
Gioh Kim60dee3c2016-01-27 12:02:09 +010099 /*
100 * On Carrizo and later platforms, ApmTdpLimit bit field
101 * is extended to 16:31 from 16:28.
102 */
Huang Rui1d28e012016-04-06 15:44:15 +0800103 if (is_carrizo_or_later())
Gioh Kim60dee3c2016-01-27 12:02:09 +0100104 tdp_limit = val >> 16;
105 else
106 tdp_limit = (val >> 16) & 0x1fff;
107
Guenter Roeck62867d42012-06-21 06:26:12 -0700108 curr_pwr_watts = ((u64)(tdp_limit +
109 data->base_tdp)) << running_avg_range;
Andre Przywara941a9562012-03-23 10:02:17 +0100110 curr_pwr_watts -= running_avg_capture;
Andreas Herrmann512d1022011-05-25 20:43:31 +0200111 curr_pwr_watts *= data->tdp_to_watts;
112
113 /*
114 * Convert to microWatt
115 *
116 * power is in Watt provided as fixed point integer with
117 * scaling factor 1/(2^16). For conversion we use
118 * (10^6)/(2^16) = 15625/(2^10)
119 */
Andre Przywara941a9562012-03-23 10:02:17 +0100120 curr_pwr_watts = (curr_pwr_watts * 15625) >> (10 + running_avg_range);
Andreas Herrmann512d1022011-05-25 20:43:31 +0200121 return sprintf(buf, "%u\n", (unsigned int) curr_pwr_watts);
122}
Julia Lawalld013f7f2016-12-22 13:05:34 +0100123static DEVICE_ATTR_RO(power1_input);
Andreas Herrmann512d1022011-05-25 20:43:31 +0200124
Julia Lawalld013f7f2016-12-22 13:05:34 +0100125static ssize_t power1_crit_show(struct device *dev,
126 struct device_attribute *attr, char *buf)
Andreas Herrmann512d1022011-05-25 20:43:31 +0200127{
128 struct fam15h_power_data *data = dev_get_drvdata(dev);
129
130 return sprintf(buf, "%u\n", data->processor_pwr_watts);
131}
Julia Lawalld013f7f2016-12-22 13:05:34 +0100132static DEVICE_ATTR_RO(power1_crit);
Andreas Herrmann512d1022011-05-25 20:43:31 +0200133
Huang Ruifa7943442016-04-06 15:44:11 +0800134static void do_read_registers_on_cu(void *_data)
135{
136 struct fam15h_power_data *data = _data;
137 int cpu, cu;
138
139 cpu = smp_processor_id();
140
141 /*
142 * With the new x86 topology modelling, cpu core id actually
143 * is compute unit id.
144 */
145 cu = cpu_data(cpu).cpu_core_id;
146
147 rdmsrl_safe(MSR_F15H_CU_PWR_ACCUMULATOR, &data->cu_acc_power[cu]);
Huang Ruicdb9e112016-04-06 15:44:12 +0800148 rdmsrl_safe(MSR_F15H_PTSC, &data->cpu_sw_pwr_ptsc[cu]);
Huang Rui11bf0d72016-04-06 15:44:13 +0800149
150 data->cu_on[cu] = 1;
Huang Ruifa7943442016-04-06 15:44:11 +0800151}
152
153/*
154 * This function is only able to be called when CPUID
155 * Fn8000_0007:EDX[12] is set.
156 */
157static int read_registers(struct fam15h_power_data *data)
158{
Huang Ruifa7943442016-04-06 15:44:11 +0800159 int core, this_core;
160 cpumask_var_t mask;
Borislav Petkov7be48812016-06-01 11:36:13 +0200161 int ret, cpu;
Huang Ruifa7943442016-04-06 15:44:11 +0800162
163 ret = zalloc_cpumask_var(&mask, GFP_KERNEL);
164 if (!ret)
165 return -ENOMEM;
166
Huang Rui11bf0d72016-04-06 15:44:13 +0800167 memset(data->cu_on, 0, sizeof(int) * MAX_CUS);
168
Sebastian Andrzej Siewiore104d532021-08-03 16:15:56 +0200169 cpus_read_lock();
Huang Ruifa7943442016-04-06 15:44:11 +0800170
171 /*
172 * Choose the first online core of each compute unit, and then
173 * read their MSR value of power and ptsc in a single IPI,
174 * because the MSR value of CPU core represent the compute
175 * unit's.
176 */
177 core = -1;
178
179 for_each_online_cpu(cpu) {
180 this_core = topology_core_id(cpu);
181
182 if (this_core == core)
183 continue;
184
185 core = this_core;
186
187 /* get any CPU on this compute unit */
188 cpumask_set_cpu(cpumask_any(topology_sibling_cpumask(cpu)), mask);
189 }
190
Borislav Petkov7be48812016-06-01 11:36:13 +0200191 on_each_cpu_mask(mask, do_read_registers_on_cu, data, true);
Huang Ruifa7943442016-04-06 15:44:11 +0800192
Sebastian Andrzej Siewiore104d532021-08-03 16:15:56 +0200193 cpus_read_unlock();
Huang Ruifa7943442016-04-06 15:44:11 +0800194 free_cpumask_var(mask);
195
196 return 0;
197}
198
Julia Lawalld013f7f2016-12-22 13:05:34 +0100199static ssize_t power1_average_show(struct device *dev,
200 struct device_attribute *attr, char *buf)
Huang Rui11bf0d72016-04-06 15:44:13 +0800201{
202 struct fam15h_power_data *data = dev_get_drvdata(dev);
203 u64 prev_cu_acc_power[MAX_CUS], prev_ptsc[MAX_CUS],
204 jdelta[MAX_CUS];
205 u64 tdelta, avg_acc;
206 int cu, cu_num, ret;
207 signed long leftover;
208
209 /*
210 * With the new x86 topology modelling, x86_max_cores is the
211 * compute unit number.
212 */
213 cu_num = boot_cpu_data.x86_max_cores;
214
215 ret = read_registers(data);
216 if (ret)
217 return 0;
218
219 for (cu = 0; cu < cu_num; cu++) {
220 prev_cu_acc_power[cu] = data->cu_acc_power[cu];
221 prev_ptsc[cu] = data->cpu_sw_pwr_ptsc[cu];
222 }
223
224 leftover = schedule_timeout_interruptible(msecs_to_jiffies(data->power_period));
225 if (leftover)
226 return 0;
227
228 ret = read_registers(data);
229 if (ret)
230 return 0;
231
232 for (cu = 0, avg_acc = 0; cu < cu_num; cu++) {
233 /* check if current compute unit is online */
234 if (data->cu_on[cu] == 0)
235 continue;
236
237 if (data->cu_acc_power[cu] < prev_cu_acc_power[cu]) {
238 jdelta[cu] = data->max_cu_acc_power + data->cu_acc_power[cu];
239 jdelta[cu] -= prev_cu_acc_power[cu];
240 } else {
241 jdelta[cu] = data->cu_acc_power[cu] - prev_cu_acc_power[cu];
242 }
243 tdelta = data->cpu_sw_pwr_ptsc[cu] - prev_ptsc[cu];
244 jdelta[cu] *= data->cpu_pwr_sample_ratio * 1000;
245 do_div(jdelta[cu], tdelta);
246
247 /* the unit is microWatt */
248 avg_acc += jdelta[cu];
249 }
250
251 return sprintf(buf, "%llu\n", (unsigned long long)avg_acc);
252}
Julia Lawalld013f7f2016-12-22 13:05:34 +0100253static DEVICE_ATTR_RO(power1_average);
Huang Rui11bf0d72016-04-06 15:44:13 +0800254
Julia Lawalld013f7f2016-12-22 13:05:34 +0100255static ssize_t power1_average_interval_show(struct device *dev,
256 struct device_attribute *attr,
257 char *buf)
Huang Rui11bf0d72016-04-06 15:44:13 +0800258{
259 struct fam15h_power_data *data = dev_get_drvdata(dev);
260
261 return sprintf(buf, "%lu\n", data->power_period);
262}
263
Julia Lawalld013f7f2016-12-22 13:05:34 +0100264static ssize_t power1_average_interval_store(struct device *dev,
265 struct device_attribute *attr,
266 const char *buf, size_t count)
Huang Rui11bf0d72016-04-06 15:44:13 +0800267{
268 struct fam15h_power_data *data = dev_get_drvdata(dev);
269 unsigned long temp;
270 int ret;
271
272 ret = kstrtoul(buf, 10, &temp);
273 if (ret)
274 return ret;
275
276 if (temp > MAX_INTERVAL)
277 return -EINVAL;
278
279 /* the interval value should be greater than 0 */
280 if (temp <= 0)
281 return -EINVAL;
282
283 data->power_period = temp;
284
285 return count;
286}
Julia Lawalld013f7f2016-12-22 13:05:34 +0100287static DEVICE_ATTR_RW(power1_average_interval);
Huang Rui11bf0d72016-04-06 15:44:13 +0800288
Huang Rui7deb14b2015-10-30 17:56:55 +0800289static int fam15h_power_init_attrs(struct pci_dev *pdev,
290 struct fam15h_power_data *data)
Aravind Gopalakrishnan961a2372014-09-16 14:58:04 -0500291{
Huang Rui7deb14b2015-10-30 17:56:55 +0800292 int n = FAM15H_MIN_NUM_ATTRS;
293 struct attribute **fam15h_power_attrs;
Huang Rui46f29c2b2015-10-30 17:56:56 +0800294 struct cpuinfo_x86 *c = &boot_cpu_data;
Aravind Gopalakrishnan961a2372014-09-16 14:58:04 -0500295
Huang Rui46f29c2b2015-10-30 17:56:56 +0800296 if (c->x86 == 0x15 &&
297 (c->x86_model <= 0xf ||
Huang Ruieff2a942015-12-10 11:56:10 +0800298 (c->x86_model >= 0x60 && c->x86_model <= 0x7f)))
Huang Rui7deb14b2015-10-30 17:56:55 +0800299 n += 1;
300
Huang Rui11bf0d72016-04-06 15:44:13 +0800301 /* check if processor supports accumulated power */
302 if (boot_cpu_has(X86_FEATURE_ACC_POWER))
303 n += 2;
304
Huang Rui7deb14b2015-10-30 17:56:55 +0800305 fam15h_power_attrs = devm_kcalloc(&pdev->dev, n,
306 sizeof(*fam15h_power_attrs),
307 GFP_KERNEL);
308
309 if (!fam15h_power_attrs)
310 return -ENOMEM;
311
312 n = 0;
313 fam15h_power_attrs[n++] = &dev_attr_power1_crit.attr;
Huang Rui46f29c2b2015-10-30 17:56:56 +0800314 if (c->x86 == 0x15 &&
315 (c->x86_model <= 0xf ||
Huang Ruieff2a942015-12-10 11:56:10 +0800316 (c->x86_model >= 0x60 && c->x86_model <= 0x7f)))
Huang Rui7deb14b2015-10-30 17:56:55 +0800317 fam15h_power_attrs[n++] = &dev_attr_power1_input.attr;
318
Huang Rui11bf0d72016-04-06 15:44:13 +0800319 if (boot_cpu_has(X86_FEATURE_ACC_POWER)) {
320 fam15h_power_attrs[n++] = &dev_attr_power1_average.attr;
321 fam15h_power_attrs[n++] = &dev_attr_power1_average_interval.attr;
322 }
323
Huang Rui7deb14b2015-10-30 17:56:55 +0800324 data->group.attrs = fam15h_power_attrs;
325
326 return 0;
Aravind Gopalakrishnan961a2372014-09-16 14:58:04 -0500327}
328
Huang Ruid83e92b2015-08-27 16:07:33 +0800329static bool should_load_on_this_node(struct pci_dev *f4)
Andreas Herrmann512d1022011-05-25 20:43:31 +0200330{
331 u32 val;
332
333 pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 3),
334 REG_NORTHBRIDGE_CAP, &val);
335 if ((val & BIT(29)) && ((val >> 30) & 3))
336 return false;
337
338 return true;
339}
340
Andre Przywara00250ec2012-04-09 18:16:34 -0400341/*
342 * Newer BKDG versions have an updated recommendation on how to properly
343 * initialize the running average range (was: 0xE, now: 0x9). This avoids
344 * counter saturations resulting in bogus power readings.
345 * We correct this value ourselves to cope with older BIOSes.
346 */
Andreas Herrmann5f0ecb92012-09-23 20:27:32 +0200347static const struct pci_device_id affected_device[] = {
Guenter Roeckc3e40a92012-04-25 13:44:20 -0700348 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) },
349 { 0 }
350};
351
Andreas Herrmann5f0ecb92012-09-23 20:27:32 +0200352static void tweak_runavg_range(struct pci_dev *pdev)
Andre Przywara00250ec2012-04-09 18:16:34 -0400353{
354 u32 val;
Andre Przywara00250ec2012-04-09 18:16:34 -0400355
356 /*
357 * let this quirk apply only to the current version of the
358 * northbridge, since future versions may change the behavior
359 */
Guenter Roeckc3e40a92012-04-25 13:44:20 -0700360 if (!pci_match_id(affected_device, pdev))
Andre Przywara00250ec2012-04-09 18:16:34 -0400361 return;
362
363 pci_bus_read_config_dword(pdev->bus,
364 PCI_DEVFN(PCI_SLOT(pdev->devfn), 5),
365 REG_TDP_RUNNING_AVERAGE, &val);
366 if ((val & 0xf) != 0xe)
367 return;
368
369 val &= ~0xf;
370 val |= 0x9;
371 pci_bus_write_config_dword(pdev->bus,
372 PCI_DEVFN(PCI_SLOT(pdev->devfn), 5),
373 REG_TDP_RUNNING_AVERAGE, val);
374}
375
Andreas Herrmann5f0ecb92012-09-23 20:27:32 +0200376#ifdef CONFIG_PM
377static int fam15h_power_resume(struct pci_dev *pdev)
378{
379 tweak_runavg_range(pdev);
380 return 0;
381}
382#else
383#define fam15h_power_resume NULL
384#endif
385
Huang Rui7deb14b2015-10-30 17:56:55 +0800386static int fam15h_power_init_data(struct pci_dev *f4,
387 struct fam15h_power_data *data)
Andreas Herrmann512d1022011-05-25 20:43:31 +0200388{
Huang Rui11bf0d72016-04-06 15:44:13 +0800389 u32 val;
Andreas Herrmann512d1022011-05-25 20:43:31 +0200390 u64 tmp;
Huang Rui7deb14b2015-10-30 17:56:55 +0800391 int ret;
Andreas Herrmann512d1022011-05-25 20:43:31 +0200392
393 pci_read_config_dword(f4, REG_PROCESSOR_TDP, &val);
394 data->base_tdp = val >> 16;
395 tmp = val & 0xffff;
396
397 pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
398 REG_TDP_LIMIT3, &val);
399
400 data->tdp_to_watts = ((val & 0x3ff) << 6) | ((val >> 10) & 0x3f);
401 tmp *= data->tdp_to_watts;
402
403 /* result not allowed to be >= 256W */
404 if ((tmp >> 16) >= 256)
Guenter Roeckb55f3752013-01-10 10:01:24 -0800405 dev_warn(&f4->dev,
406 "Bogus value for ProcessorPwrWatts (processor_pwr_watts>=%u)\n",
Andreas Herrmann512d1022011-05-25 20:43:31 +0200407 (unsigned int) (tmp >> 16));
408
409 /* convert to microWatt */
410 data->processor_pwr_watts = (tmp * 15625) >> 10;
Huang Rui1ed32162015-08-27 16:07:38 +0800411
Huang Rui7deb14b2015-10-30 17:56:55 +0800412 ret = fam15h_power_init_attrs(f4, data);
413 if (ret)
414 return ret;
415
Huang Rui1ed32162015-08-27 16:07:38 +0800416
417 /* CPUID Fn8000_0007:EDX[12] indicates to support accumulated power */
Huang Rui11bf0d72016-04-06 15:44:13 +0800418 if (!boot_cpu_has(X86_FEATURE_ACC_POWER))
Huang Rui7deb14b2015-10-30 17:56:55 +0800419 return 0;
Huang Rui1ed32162015-08-27 16:07:38 +0800420
421 /*
422 * determine the ratio of the compute unit power accumulator
423 * sample period to the PTSC counter period by executing CPUID
424 * Fn8000_0007:ECX
425 */
Huang Rui11bf0d72016-04-06 15:44:13 +0800426 data->cpu_pwr_sample_ratio = cpuid_ecx(0x80000007);
Huang Rui7deb14b2015-10-30 17:56:55 +0800427
Huang Rui3b5ea472015-10-30 17:56:57 +0800428 if (rdmsrl_safe(MSR_F15H_CU_MAX_PWR_ACCUMULATOR, &tmp)) {
429 pr_err("Failed to read max compute unit power accumulator MSR\n");
430 return -ENODEV;
431 }
432
433 data->max_cu_acc_power = tmp;
434
Huang Rui11bf0d72016-04-06 15:44:13 +0800435 /*
436 * Milliseconds are a reasonable interval for the measurement.
437 * But it shouldn't set too long here, because several seconds
438 * would cause the read function to hang. So set default
439 * interval as 10 ms.
440 */
441 data->power_period = 10;
442
Huang Ruifa7943442016-04-06 15:44:11 +0800443 return read_registers(data);
Andreas Herrmann512d1022011-05-25 20:43:31 +0200444}
445
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500446static int fam15h_power_probe(struct pci_dev *pdev,
Huang Rui7deb14b2015-10-30 17:56:55 +0800447 const struct pci_device_id *id)
Andreas Herrmann512d1022011-05-25 20:43:31 +0200448{
449 struct fam15h_power_data *data;
Guenter Roeck87432a22012-06-02 09:58:06 -0700450 struct device *dev = &pdev->dev;
Axel Lin562dc972014-06-19 23:29:11 +0800451 struct device *hwmon_dev;
Huang Rui7deb14b2015-10-30 17:56:55 +0800452 int ret;
Andreas Herrmann512d1022011-05-25 20:43:31 +0200453
Andre Przywara00250ec2012-04-09 18:16:34 -0400454 /*
455 * though we ignore every other northbridge, we still have to
456 * do the tweaking on _each_ node in MCM processors as the counters
457 * are working hand-in-hand
458 */
459 tweak_runavg_range(pdev);
460
Huang Ruid83e92b2015-08-27 16:07:33 +0800461 if (!should_load_on_this_node(pdev))
Guenter Roeck87432a22012-06-02 09:58:06 -0700462 return -ENODEV;
Andreas Herrmann512d1022011-05-25 20:43:31 +0200463
Guenter Roeck87432a22012-06-02 09:58:06 -0700464 data = devm_kzalloc(dev, sizeof(struct fam15h_power_data), GFP_KERNEL);
465 if (!data)
466 return -ENOMEM;
467
Huang Rui7deb14b2015-10-30 17:56:55 +0800468 ret = fam15h_power_init_data(pdev, data);
469 if (ret)
470 return ret;
471
Axel Lin562dc972014-06-19 23:29:11 +0800472 data->pdev = pdev;
Andreas Herrmann512d1022011-05-25 20:43:31 +0200473
Huang Rui7deb14b2015-10-30 17:56:55 +0800474 data->groups[0] = &data->group;
475
Axel Lin562dc972014-06-19 23:29:11 +0800476 hwmon_dev = devm_hwmon_device_register_with_groups(dev, "fam15h_power",
477 data,
Huang Rui7deb14b2015-10-30 17:56:55 +0800478 &data->groups[0]);
Axel Lin562dc972014-06-19 23:29:11 +0800479 return PTR_ERR_OR_ZERO(hwmon_dev);
Andreas Herrmann512d1022011-05-25 20:43:31 +0200480}
481
Jingoo Hancd9bb052013-12-03 07:10:29 +0000482static const struct pci_device_id fam15h_power_id_table[] = {
Andreas Herrmann512d1022011-05-25 20:43:31 +0200483 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) },
Aravind Gopalakrishnan0a0039a2014-09-16 14:58:16 -0500484 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M30H_NB_F4) },
Huang Rui5dc08722015-08-27 16:07:32 +0800485 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M60H_NB_F4) },
Huang Ruieff2a942015-12-10 11:56:10 +0800486 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M70H_NB_F4) },
Boris Ostrovsky22e32f42012-12-05 06:12:42 -0500487 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_NB_F4) },
Aravind Gopalakrishnan0bd52942014-11-04 11:49:02 -0600488 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_M30H_NB_F4) },
Andreas Herrmann512d1022011-05-25 20:43:31 +0200489 {}
490};
491MODULE_DEVICE_TABLE(pci, fam15h_power_id_table);
492
493static struct pci_driver fam15h_power_driver = {
494 .name = "fam15h_power",
495 .id_table = fam15h_power_id_table,
496 .probe = fam15h_power_probe,
Andreas Herrmann5f0ecb92012-09-23 20:27:32 +0200497 .resume = fam15h_power_resume,
Andreas Herrmann512d1022011-05-25 20:43:31 +0200498};
499
Axel Linf71f5a52012-04-02 21:25:46 -0400500module_pci_driver(fam15h_power_driver);