summaryrefslogtreecommitdiff
path: root/usr/src/uts/intel/io/amdnbtemp/amdnbtemp.c
blob: 17934520fdcd8d7ac7adf21d491d7f8f8314383a (plain)
1
2
3
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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2019 Robert Mustacchi
 * Copyright 2020 Oxide Computer Company
 */

/*
 * AMD Northbridge CPU Temperature Driver
 *
 * The AMD northbridge CPU temperature driver supports the temperature sensor
 * that was found on the AMD northbridge on AMD CPUs from approximately AMD
 * Family 10h to Family 16h. For Zen and newer processors (Family 17h+) see the
 * 'amdf17nbdf' driver.
 *
 * The temperature is stored on the 'miscellaneous' device on the northbridge.
 * This is always found at PCI Device 18h, Function 3h. When there is more than
 * one 'node' (see cpuid.c for the AMD parlance), then the node id is added to
 * the device to create a unique device. This allows us to map the given PCI
 * device we find back to the corresponding CPU.
 *
 * While all family 10h, 11h, 12h, 14h, and 16h CPUs are supported, not all
 * family 15h CPUs are. Models 60h+ require the SMN interface, which this does
 * not know how to consume.
 */

#include <sys/modctl.h>
#include <sys/conf.h>
#include <sys/devops.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/cmn_err.h>
#include <sys/pci.h>
#include <sys/stddef.h>
#include <sys/cpuvar.h>
#include <sys/x86_archext.h>
#include <sys/list.h>
#include <sys/bitset.h>
#include <sys/sensors.h>

/*
 * This register offset, in PCI config space, has the current temperature of the
 * device.
 */
#define	AMDNBTEMP_TEMPREG	0xa4
#define	AMDNBTEMP_TEMPREG_CURTMP(x)	BITX(x, 31, 21)
#define	AMDNBTEMP_TEMPREG_TJSEL(x)	BITX(x, 17, 16)

/*
 * Each bit in the temperature range represents 1/8th of a degree C.
 */
#define	AMDNBTEMP_GRANULARITY	8
#define	AMDNBTEMP_GSHIFT	3

/*
 * If the value of the current CurTmpTjSel is set to three, then the range that
 * the data is in is shifted by -49 degrees. In this mode, the bottom two bits
 * always read as zero.
 */
#define	AMDNBTEMP_TJSEL_ADJUST	0x3
#define	AMDNBTEMP_TEMP_ADJUST	(49 << AMDNBTEMP_GSHIFT)

/*
 * There are a variable number of northbridges that exist in the system. The AMD
 * BIOS and Kernel Developer's Guide (BKDG) says that for these families, the
 * first node has a device of 0x18. This means that node 7, the maximum, has a
 * device of 0x1f.
 */
#define	AMDNBTEMP_FIRST_DEV	0x18

typedef enum andnbtemp_state {
	AMDNBTEMP_S_CFGSPACE	= 1 << 0,
	AMDNBTEMP_S_MUTEX	= 1 << 1,
	AMDNBTMEP_S_KSENSOR	= 1 << 2
} amdnbtemp_state_t;

typedef struct amdnbtemp {
	amdnbtemp_state_t	at_state;
	dev_info_t		*at_dip;
	ddi_acc_handle_t	at_cfgspace;
	uint_t			at_bus;
	uint_t			at_dev;
	uint_t			at_func;
	id_t			at_ksensor;
	minor_t			at_minor;
	boolean_t		at_tjsel;
	kmutex_t		at_mutex;
	uint32_t		at_raw;
	int64_t			at_temp;
} amdnbtemp_t;

static void *amdnbtemp_state;

static int
amdnbtemp_read(void *arg, sensor_ioctl_scalar_t *scalar)
{
	amdnbtemp_t *at = arg;

	mutex_enter(&at->at_mutex);
	at->at_raw = pci_config_get32(at->at_cfgspace, AMDNBTEMP_TEMPREG);
	if (at->at_raw == PCI_EINVAL32) {
		mutex_exit(&at->at_mutex);
		return (EIO);
	}

	at->at_temp = AMDNBTEMP_TEMPREG_CURTMP(at->at_raw);
	if (at->at_tjsel &&
	    AMDNBTEMP_TEMPREG_TJSEL(at->at_raw) == AMDNBTEMP_TJSEL_ADJUST) {
		at->at_temp -= AMDNBTEMP_TEMP_ADJUST;
	}

	scalar->sis_unit = SENSOR_UNIT_CELSIUS;
	scalar->sis_gran = AMDNBTEMP_GRANULARITY;
	scalar->sis_value = at->at_temp;
	mutex_exit(&at->at_mutex);

	return (0);
}

static const ksensor_ops_t amdnbtemp_temp_ops = {
	.kso_kind = ksensor_kind_temperature,
	.kso_scalar = amdnbtemp_read
};

static void
amdnbtemp_cleanup(amdnbtemp_t *at)
{
	int inst;
	inst = ddi_get_instance(at->at_dip);

	if ((at->at_state & AMDNBTMEP_S_KSENSOR) != 0) {
		(void) ksensor_remove(at->at_dip, KSENSOR_ALL_IDS);
		at->at_state &= ~AMDNBTMEP_S_KSENSOR;
	}

	if ((at->at_state & AMDNBTEMP_S_MUTEX) != 0) {
		mutex_destroy(&at->at_mutex);
		at->at_state &= ~AMDNBTEMP_S_MUTEX;
	}

	if ((at->at_state & AMDNBTEMP_S_CFGSPACE) != 0) {
		pci_config_teardown(&at->at_cfgspace);
		at->at_state &= ~AMDNBTEMP_S_CFGSPACE;
	}

	ASSERT0(at->at_state);
	ddi_soft_state_free(amdnbtemp_state, inst);
}

/*
 * For several family 10h processors, certain models have an erratum which says
 * that temperature information is unreliable. If we're on a platform that is
 * subject to this erratum, do not attach to the device.
 */
static boolean_t
amdnbtemp_erratum_319(void)
{
	uint32_t socket;

	if (cpuid_getfamily(CPU) != 0x10) {
		return (B_FALSE);
	}

	/*
	 * All Family 10h socket F parts are impacted. Socket AM2 parts are all
	 * impacted. The family 10h socket bits in cpuid share the same bit for
	 * socket AM2 and AM3. If you look at the erratum description, they use
	 * information about the memory controller to do DDR2/DDR3
	 * disambiguation to determine whether it's socket AM2 or AM3. Our cpuid
	 * subroutines already do the DDR2/DDR3 disambiguation so we can just
	 * check the socket type as the disambiguation has already been done.
	 */
	socket = cpuid_getsockettype(CPU);
	if (socket == X86_SOCKET_F1207 || socket == X86_SOCKET_AM2R2) {
		return (B_TRUE);
	}

	return (B_FALSE);
}

static int
amdnbtemp_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
	int inst, *regs, ret;
	amdnbtemp_t *at;
	uint_t nregs, id;
	char buf[128];

	switch (cmd) {
	case DDI_RESUME:
		return (DDI_SUCCESS);
	case DDI_ATTACH:
		break;
	default:
		return (DDI_FAILURE);
	}

	inst = ddi_get_instance(dip);
	if (ddi_soft_state_zalloc(amdnbtemp_state, inst) != DDI_SUCCESS) {
		dev_err(dip, CE_WARN, "failed to allocate soft state entry %d",
		    inst);
		return (DDI_FAILURE);
	}

	at = ddi_get_soft_state(amdnbtemp_state, inst);
	if (at == NULL) {
		dev_err(dip, CE_WARN, "failed to retrieve soft state entry %d",
		    inst);
		return (DDI_FAILURE);
	}

	at->at_dip = dip;

	if (pci_config_setup(dip, &at->at_cfgspace) != DDI_SUCCESS) {
		dev_err(dip, CE_WARN, "failed to set up PCI config space");
		goto err;
	}
	at->at_state |= AMDNBTEMP_S_CFGSPACE;

	if (amdnbtemp_erratum_319()) {
		dev_err(dip, CE_WARN, "!device subject to AMD Erratum 319, "
		    "not attaching to unreliable sensor");
		goto err;
	}

	mutex_init(&at->at_mutex, NULL, MUTEX_DRIVER, NULL);
	at->at_state |= AMDNBTEMP_S_MUTEX;

	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, 0, "reg",
	    &regs, &nregs) != DDI_PROP_SUCCESS) {
		dev_err(dip, CE_WARN, "failed to get pci 'reg' property");
		goto err;
	}

	if (nregs < 1) {
		dev_err(dip, CE_WARN, "'reg' property missing PCI b/d/f");
		ddi_prop_free(regs);
		goto err;
	}

	at->at_bus = PCI_REG_BUS_G(regs[0]);
	at->at_dev = PCI_REG_DEV_G(regs[0]);
	at->at_func = PCI_REG_DEV_G(regs[0]);
	ddi_prop_free(regs);

	if (at->at_dev < AMDNBTEMP_FIRST_DEV) {
		dev_err(dip, CE_WARN, "Invalid pci b/d/f device, found 0x%x",
		    at->at_dev);
		goto err;
	}

	id = at->at_dev - AMDNBTEMP_FIRST_DEV;
	if (snprintf(buf, sizeof (buf), "procnode.%u", id) >= sizeof (buf)) {
		dev_err(dip, CE_WARN, "unexpected buffer name overrun "
		    "constructing sensor %u", id);
		goto err;
	}

	/*
	 * On families 15h and 16h the BKDG documents that the CurTmpTjSel bits
	 * of the temperature register dictate how the temperature reading
	 * should be interpreted. Capture that now.
	 */
	if (cpuid_getfamily(CPU) >= 0x15) {
		at->at_tjsel = B_TRUE;
	}

	if ((ret = ksensor_create(dip, &amdnbtemp_temp_ops, at, buf,
	    DDI_NT_SENSOR_TEMP_CPU, &at->at_ksensor)) != 0) {
		dev_err(dip, CE_WARN, "failed to create ksensor for %s: %d",
		    buf, ret);
		goto err;
	}
	at->at_state |= AMDNBTMEP_S_KSENSOR;

	return (DDI_SUCCESS);

err:
	amdnbtemp_cleanup(at);
	return (DDI_FAILURE);
}

static int
amdnbtemp_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
	int inst;
	amdnbtemp_t *at;

	switch (cmd) {
	case DDI_DETACH:
		break;
	case DDI_SUSPEND:
		return (DDI_SUCCESS);
	default:
		return (DDI_FAILURE);
	}

	inst = ddi_get_instance(dip);
	at = ddi_get_soft_state(amdnbtemp_state, inst);
	if (at == NULL) {
		dev_err(dip, CE_WARN, "asked to detach instance %d, but it is "
		    "missing from the soft state", inst);
		return (DDI_FAILURE);
	}

	amdnbtemp_cleanup(at);
	return (DDI_SUCCESS);
}

static struct dev_ops amdnbtemp_dev_ops = {
	.devo_rev = DEVO_REV,
	.devo_refcnt = 0,
	.devo_getinfo = nodev,
	.devo_identify = nulldev,
	.devo_probe = nulldev,
	.devo_attach = amdnbtemp_attach,
	.devo_detach = amdnbtemp_detach,
	.devo_reset = nodev,
	.devo_quiesce = ddi_quiesce_not_needed
};

static struct modldrv amdnbtemp_modldrv = {
	.drv_modops = &mod_driverops,
	.drv_linkinfo = "AMD NB Temp Driver",
	.drv_dev_ops = &amdnbtemp_dev_ops
};

static struct modlinkage amdnbtemp_modlinkage = {
	.ml_rev = MODREV_1,
	.ml_linkage = { &amdnbtemp_modldrv, NULL }
};

int
_init(void)
{
	int ret;

	if (ddi_soft_state_init(&amdnbtemp_state, sizeof (amdnbtemp_t), 2) !=
	    DDI_SUCCESS) {
		return (ENOMEM);
	}

	if ((ret = mod_install(&amdnbtemp_modlinkage)) != 0) {
		ddi_soft_state_fini(&amdnbtemp_state);
		return (ret);
	}

	return (ret);
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&amdnbtemp_modlinkage, modinfop));
}

int
_fini(void)
{
	int ret;

	if ((ret = mod_remove(&amdnbtemp_modlinkage)) != 0) {
		return (ret);
	}

	ddi_soft_state_fini(&amdnbtemp_state);
	return (ret);
}