Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*      $NetBSD: amdtemp.c,v 1.23 2018/12/30 15:43:43 is Exp $ */
/*      $OpenBSD: kate.c,v 1.2 2008/03/27 04:52:03 cnst Exp $   */

/*
 * Copyright (c) 2008 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Christoph Egger.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Copyright (c) 2008 Constantine A. Murenin <cnst+openbsd@bugmail.mojo.ru>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: amdtemp.c,v 1.23 2018/12/30 15:43:43 is Exp $ ");

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kmem.h>
#include <sys/module.h>

#include <machine/specialreg.h>

#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>

#include <dev/sysmon/sysmonvar.h>

/*
 * AMD K8:
 * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/32559.pdf
 * AMD K8 Errata: #141
 * http://support.amd.com/us/Processor_TechDocs/33610_PUB_Rev3%2042v3.pdf
 *
 * Family10h:
 * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/31116.PDF
 * Family10h Errata: #319
 * http://support.amd.com/de/Processor_TechDocs/41322.pdf
 *
 * Family11h:
 * http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/41256.pdf
 */

/* AMD Processors, Function 3 -- Miscellaneous Control
 */

/* Function 3 Registers */
#define THERMTRIP_STAT_R      0xe4
#define NORTHBRIDGE_CAP_R     0xe8
#define CPUID_FAMILY_MODEL_R  0xfc

/*
 * AMD NPT Family 0Fh Processors, Function 3 -- Miscellaneous Control
 */

/* Bits within Thermtrip Status Register */
#define K8_THERM_SENSE_SEL       (1 << 6)
#define K8_THERM_SENSE_CORE_SEL  (1 << 2)

/* Flip core and sensor selection bits */
#define K8_T_SEL_C0(v)           (v |= K8_THERM_SENSE_CORE_SEL)
#define K8_T_SEL_C1(v)           (v &= ~(K8_THERM_SENSE_CORE_SEL))
#define K8_T_SEL_S0(v)           (v &= ~(K8_THERM_SENSE_SEL))
#define K8_T_SEL_S1(v)           (v |= K8_THERM_SENSE_SEL)

/*
 * AMD Family 10h Processors, Function 3 -- Miscellaneous Control
 */

/* Function 3 Registers */
#define F10_TEMPERATURE_CTL_R	0xa4
#define 	F10_TEMP_CURTMP		__BITS(31,21)

/*
 * Revision Guide for AMD NPT Family 0Fh Processors,
 * Publication # 33610, Revision 3.30, February 2008
 */
#define K8_SOCKET_F	1	/* Server */
#define K8_SOCKET_AM2	2	/* Desktop */
#define K8_SOCKET_S1	3	/* Laptop */

static const struct {
	const char rev[5];
	const struct {
		const pcireg_t cpuid;
		const uint8_t socket;
	} cpu[5];
} amdtemp_core[] = {
	{ "BH-F", { { 0x00040FB0, K8_SOCKET_AM2 },	/* F2 */
		  { 0x00040F80, K8_SOCKET_S1 },		/* F2 */
		  { 0, 0 }, { 0, 0 }, { 0, 0 } } },
	{ "DH-F", { { 0x00040FF0, K8_SOCKET_AM2 },	/* F2 */
		  { 0x00040FC0, K8_SOCKET_S1 },		/* F2 */
		  { 0x00050FF0, K8_SOCKET_AM2 },	/* F2, F3 */
		  { 0, 0 }, { 0, 0 } } },
	{ "JH-F", { { 0x00040F10, K8_SOCKET_F },	/* F2, F3 */
		  { 0x00040F30, K8_SOCKET_AM2 },	/* F2, F3 */
		  { 0x000C0F10, K8_SOCKET_F },		/* F3 */
		  { 0, 0 }, { 0, 0 } } },
	{ "BH-G", { { 0x00060FB0, K8_SOCKET_AM2 },	/* G1, G2 */
		  { 0x00060F80, K8_SOCKET_S1 },		/* G1, G2 */
		  { 0, 0 }, { 0, 0 }, { 0, 0 } } },
	{ "DH-G", { { 0x00060FF0, K8_SOCKET_AM2 },	/* G1, G2 */
		  { 0x00060FC0, K8_SOCKET_S1 },		/* G2 */
		  { 0x00070FF0, K8_SOCKET_AM2 },	/* G1, G2 */
		  { 0x00070FC0, K8_SOCKET_S1 },		/* G2 */
		  { 0, 0 } } }
};

struct amdtemp_softc {
	pci_chipset_tag_t sc_pc;
	pcitag_t sc_pcitag;

	struct sysmon_envsys *sc_sme;
	envsys_data_t *sc_sensor;
	size_t sc_sensor_len;

	char sc_rev;
	int8_t sc_numsensors;
	uint32_t sc_family;
	int32_t sc_adjustment;
};

static int  amdtemp_match(device_t, cfdata_t, void *);
static void amdtemp_attach(device_t, device_t, void *);
static int  amdtemp_detach(device_t, int);

static void amdtemp_k8_init(struct amdtemp_softc *, pcireg_t);
static void amdtemp_k8_setup_sensors(struct amdtemp_softc *, int);
static void amdtemp_k8_refresh(struct sysmon_envsys *, envsys_data_t *);

static void amdtemp_family10_init(struct amdtemp_softc *);
static void amdtemp_family10_setup_sensors(struct amdtemp_softc *, int);
static void amdtemp_family10_refresh(struct sysmon_envsys *, envsys_data_t *);

CFATTACH_DECL_NEW(amdtemp, sizeof(struct amdtemp_softc),
    amdtemp_match, amdtemp_attach, amdtemp_detach, NULL);

static int
amdtemp_match(device_t parent, cfdata_t match, void *aux)
{
	struct pci_attach_args *pa = aux;
	pcireg_t cpu_signature;
	uint32_t family;

	KASSERT(PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD);

	cpu_signature = pci_conf_read(pa->pa_pc,
	    pa->pa_tag, CPUID_FAMILY_MODEL_R);

	/*
	 * This CPUID northbridge register has been introduced in
	 * Revision F.
	 */
	if (cpu_signature == 0x0)
		return 0;

	family = CPUID_TO_FAMILY(cpu_signature);

	/* Errata #319: This has been fixed in Revision C2. */
	if (family == 0x10) {
		if (CPUID_TO_BASEMODEL(cpu_signature) < 4)
			return 0;
		if (CPUID_TO_BASEMODEL(cpu_signature) == 4 &&
		    CPUID_TO_STEPPING(cpu_signature) < 2)
			return 0;
	}

	/* Not yet supported CPUs. */
	if (family > 0x16)
		return 0;

	return 1;
}

static void
amdtemp_attach(device_t parent, device_t self, void *aux)
{
	struct amdtemp_softc *sc = device_private(self);
	struct pci_attach_args *pa = aux;
	pcireg_t cpu_signature;
	int error;
	uint8_t i;

	aprint_naive("\n");
	aprint_normal(": AMD CPU Temperature Sensors");

	cpu_signature = pci_conf_read(pa->pa_pc,
	    pa->pa_tag, CPUID_FAMILY_MODEL_R);

	/* If we hit this, then match routine is wrong. */
	KASSERT(cpu_signature != 0x0);

	sc->sc_family = CPUID_TO_FAMILY(cpu_signature);

	KASSERT(sc->sc_family >= 0xf);

	sc->sc_sme = NULL;
	sc->sc_sensor = NULL;

	sc->sc_pc = pa->pa_pc;
	sc->sc_pcitag = pa->pa_tag;
	sc->sc_adjustment = 0;

	switch (sc->sc_family) {
	case 0xf:  /* AMD K8 NPT */
		amdtemp_k8_init(sc, cpu_signature);
		break;

	case 0x10: /* AMD Barcelona/Phenom */
	case 0x11: /* AMD Griffin */
	case 0x12: /* AMD Lynx/Sabine (Llano) */
	case 0x14: /* AMD Brazos (Ontario/Zacate/Desna) */
	case 0x15: /* AMD Bobcat */
	case 0x16: /* AMD Puma/Jaguar */
		amdtemp_family10_init(sc);
		break;

	default:
		aprint_normal(", family 0x%x not supported\n",
		    sc->sc_family);
		return;
	}

	aprint_normal("\n");

	if (sc->sc_adjustment != 0)
		aprint_debug_dev(self, "Workaround enabled\n");

	sc->sc_sme = sysmon_envsys_create();
	sc->sc_sensor_len = sizeof(envsys_data_t) * sc->sc_numsensors;
	sc->sc_sensor = kmem_zalloc(sc->sc_sensor_len, KM_SLEEP);

	switch (sc->sc_family) {
	case 0xf:
		amdtemp_k8_setup_sensors(sc, device_unit(self));
		break;
	case 0x10:
	case 0x11:
	case 0x12:
	case 0x14:
	case 0x15:
	case 0x16:
		amdtemp_family10_setup_sensors(sc, device_unit(self));
		break;
	}

	/*
	 * Set properties in sensors.
	 */
	for (i = 0; i < sc->sc_numsensors; i++) {
		if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[i]))
			goto bad;
	}

	/*
	 * Register the sysmon_envsys device.
	 */
	sc->sc_sme->sme_name = device_xname(self);
	sc->sc_sme->sme_cookie = sc;

	switch (sc->sc_family) {
	case 0xf:
		sc->sc_sme->sme_refresh = amdtemp_k8_refresh;
		break;
	case 0x10:
	case 0x11:
	case 0x12:
	case 0x14:
	case 0x15:
	case 0x16:
		sc->sc_sme->sme_refresh = amdtemp_family10_refresh;
		break;
	}

	error = sysmon_envsys_register(sc->sc_sme);
	if (error) {
		aprint_error_dev(self, "unable to register with sysmon "
			"(error=%d)\n", error);
		goto bad;
	}

	(void)pmf_device_register(self, NULL, NULL);

	return;

bad:
	if (sc->sc_sme != NULL) {
		sysmon_envsys_destroy(sc->sc_sme);
		sc->sc_sme = NULL;
	}

	if (sc->sc_sensor != NULL) {
		kmem_free(sc->sc_sensor, sc->sc_sensor_len);
		sc->sc_sensor = NULL;
	}
}

static int
amdtemp_detach(device_t self, int flags)
{
	struct amdtemp_softc *sc = device_private(self);

	pmf_device_deregister(self);
	if (sc->sc_sme != NULL)
		sysmon_envsys_unregister(sc->sc_sme);

	if (sc->sc_sensor != NULL)
		kmem_free(sc->sc_sensor, sc->sc_sensor_len);

	return 0;
}

static void
amdtemp_k8_init(struct amdtemp_softc *sc, pcireg_t cpu_signature)
{
	pcireg_t data;
	uint32_t cmpcap;
	uint8_t i, j;

	aprint_normal(" (K8");

	for (i = 0; i < __arraycount(amdtemp_core) && sc->sc_rev == '\0'; i++) {
		for (j = 0; amdtemp_core[i].cpu[j].cpuid != 0; j++) {
			if ((cpu_signature & ~0xf)
			    != amdtemp_core[i].cpu[j].cpuid)
				continue;

			sc->sc_rev = amdtemp_core[i].rev[3];
			aprint_normal(": core rev %.4s%.1x",
			    amdtemp_core[i].rev,
			    CPUID_TO_STEPPING(cpu_signature));

			switch (amdtemp_core[i].cpu[j].socket) {
			case K8_SOCKET_AM2:
				if (sc->sc_rev == 'G')
					sc->sc_adjustment = 21000000;
				aprint_normal(", socket AM2");
				break;
			case K8_SOCKET_S1:
				aprint_normal(", socket S1");
				break;
			case K8_SOCKET_F:
				aprint_normal(", socket F");
				break;
			}
		}
	}

	if (sc->sc_rev == '\0') {
		/*
		 * CPUID Family Model Register was introduced in
		 * Revision F
		 */
		sc->sc_rev = 'G';	/* newer than E, assume G */
		aprint_normal(": cpuid 0x%x", cpu_signature);
	}

	aprint_normal(")");

	data = pci_conf_read(sc->sc_pc, sc->sc_pcitag, NORTHBRIDGE_CAP_R);
	cmpcap = (data >> 12) & 0x3;

	sc->sc_numsensors = cmpcap ? 4 : 2;
}

static void
amdtemp_k8_setup_sensors(struct amdtemp_softc *sc, int dv_unit)
{
	uint8_t i;

	/*
	 * There are two sensors per CPU core. So we use the device unit as
	 * socket counter to correctly enumerate the CPUs on multi-socket
	 * machines.
	 */
	dv_unit *= (sc->sc_numsensors / 2);
	for (i = 0; i < sc->sc_numsensors; i++) {
		sc->sc_sensor[i].units = ENVSYS_STEMP;
		sc->sc_sensor[i].state = ENVSYS_SVALID;
		sc->sc_sensor[i].flags = ENVSYS_FHAS_ENTROPY;

		snprintf(sc->sc_sensor[i].desc, sizeof(sc->sc_sensor[i].desc),
		    "CPU%u Sensor%u", dv_unit + (i / 2), i % 2);
	}
}

static void
amdtemp_k8_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
{
	struct amdtemp_softc *sc = sme->sme_cookie;
	pcireg_t status, match, tmp;
	uint32_t value;

	status = pci_conf_read(sc->sc_pc, sc->sc_pcitag, THERMTRIP_STAT_R);

	switch (edata->sensor) { /* sensor number */
	case 0: /* Core 0 Sensor 0 */
		K8_T_SEL_C0(status);
		K8_T_SEL_S0(status);
		break;
	case 1: /* Core 0 Sensor 1 */
		K8_T_SEL_C0(status);
		K8_T_SEL_S1(status);
		break;
	case 2: /* Core 1 Sensor 0 */
		K8_T_SEL_C1(status);
		K8_T_SEL_S0(status);
		break;
	case 3: /* Core 1 Sensor 1 */
		K8_T_SEL_C1(status);
		K8_T_SEL_S1(status);
		break;
	}

	match = status & (K8_THERM_SENSE_CORE_SEL | K8_THERM_SENSE_SEL);
	pci_conf_write(sc->sc_pc, sc->sc_pcitag, THERMTRIP_STAT_R, status);
	status = pci_conf_read(sc->sc_pc, sc->sc_pcitag, THERMTRIP_STAT_R);
	tmp = status & (K8_THERM_SENSE_CORE_SEL | K8_THERM_SENSE_SEL);

	value = 0x3ff & (status >> 14);
	if (sc->sc_rev != 'G')
		value &= ~0x3;

	edata->state = ENVSYS_SINVALID;
	if ((tmp == match) && ((value & ~0x3) != 0)) {
		edata->state = ENVSYS_SVALID;
		edata->value_cur = (value * 250000 - 49000000) + 273150000 +
		    sc->sc_adjustment;
	}
}

static void
amdtemp_family10_init(struct amdtemp_softc *sc)
{
	aprint_normal(" (Family%02xh)", sc->sc_family);

	sc->sc_numsensors = 1;
}

static void
amdtemp_family10_setup_sensors(struct amdtemp_softc *sc, int dv_unit)
{
	/* sanity check for future enhancements */
	KASSERT(sc->sc_numsensors == 1);

	/*
	 * There's one sensor per memory controller (= socket), so we use the
	 * device unit as socket counter to correctly enumerate the CPUs.
	 */
	sc->sc_sensor[0].units = ENVSYS_STEMP;
	sc->sc_sensor[0].state = ENVSYS_SVALID;
	sc->sc_sensor[0].flags = ENVSYS_FHAS_ENTROPY;

	snprintf(sc->sc_sensor[0].desc, sizeof(sc->sc_sensor[0].desc),
	    "cpu%u temperature", dv_unit);
}

static void
amdtemp_family10_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
{
	struct amdtemp_softc *sc = sme->sme_cookie;
	pcireg_t status;
	uint32_t value;

	status = pci_conf_read(sc->sc_pc, sc->sc_pcitag,
	    F10_TEMPERATURE_CTL_R);
	value = __SHIFTOUT(status, F10_TEMP_CURTMP);

	/* From Celsius to micro-Kelvin. */
	edata->value_cur = (value * 125000) + 273150000;
	edata->state = ENVSYS_SVALID;
}

MODULE(MODULE_CLASS_DRIVER, amdtemp, "sysmon_envsys");

#ifdef _MODULE
#include "ioconf.c"
#endif

static int
amdtemp_modcmd(modcmd_t cmd, void *aux)
{
	int error = 0;

	switch (cmd) {
	case MODULE_CMD_INIT:
#ifdef _MODULE
		error = config_init_component(cfdriver_ioconf_amdtemp,
		    cfattach_ioconf_amdtemp, cfdata_ioconf_amdtemp);
#endif
		return error;
	case MODULE_CMD_FINI:
#ifdef _MODULE
		error = config_fini_component(cfdriver_ioconf_amdtemp,
		    cfattach_ioconf_amdtemp, cfdata_ioconf_amdtemp);
#endif
		return error;
	default:
		return ENOTTY;
	}
}