summaryrefslogtreecommitdiffstats
path: root/sys/arch/powerpc64/powerpc64/cpu.c
blob: 771a263448848d53435bfa94545ace5171b0052f (plain) (blame)
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
/*	$OpenBSD: cpu.c,v 1.11 2020/07/10 23:22:48 gkoehler Exp $	*/

/*
 * Copyright (c) 2020 Mark Kettenis <kettenis@openbsd.org>
 *
 * 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/param.h>
#include <sys/device.h>
#include <sys/systm.h>

#include <machine/cpu.h>
#include <machine/cpufunc.h>
#include <machine/fdt.h>

#include <dev/ofw/openfirm.h>
#include <dev/ofw/fdt.h>

/* CPU Identification. */
#define CPU_IBMPOWER8E		0x004b
#define CPU_IBMPOWER8NVL	0x004c
#define CPU_IBMPOWER8		0x004d
#define CPU_IBMPOWER9		0x004e

#define CPU_VERSION(pvr)	((pvr) >> 16)
#define CPU_REV_MAJ(pvr)	(((pvr) >> 8) & 0xf)
#define CPU_REV_MIN(pvr)	(((pvr) >> 0) & 0xf)

struct cpu_version {
	int		version;
	const char	*name;
};

struct cpu_version cpu_version[] = {
	{ CPU_IBMPOWER8, "IBM POWER8" },
	{ CPU_IBMPOWER8E, "IBM POWER8E" },
	{ CPU_IBMPOWER8NVL, "IBM POWER8NVL" },
	{ CPU_IBMPOWER9, "IBM POWER9" },
	{ 0, NULL }
};

char cpu_model[64];
uint64_t tb_freq = 512000000;	/* POWER8, POWER9 */

struct cpu_info cpu_info_primary;

int	cpu_match(struct device *, void *, void *);
void	cpu_attach(struct device *, struct device *, void *);

struct cfattach cpu_ca = {
	sizeof(struct device), cpu_match, cpu_attach
};

struct cfdriver cpu_cd = {
	NULL, "cpu", DV_DULL
};

int
cpu_match(struct device *parent, void *cfdata, void *aux)
{
	struct fdt_attach_args *faa = aux;
	char buf[32];

	if (OF_getprop(faa->fa_node, "device_type", buf, sizeof(buf)) <= 0 ||
	    strcmp(buf, "cpu") != 0)
		return 0;

	if (ncpus < MAXCPUS || faa->fa_reg[0].addr == mfpir())
		return 1;

	return 0;
}

void
cpu_attach(struct device *parent, struct device *dev, void *aux)
{
	struct fdt_attach_args *faa = aux;
	const char *name = NULL;
	uint32_t pvr, clock_freq, iline, dline;
	int node, level, i;

	printf(" pir %llx", faa->fa_reg[0].addr);

	pvr = mfpvr();

	for (i = 0; cpu_version[i].name; i++) {
		if (CPU_VERSION(pvr) == cpu_version[i].version) {
			name = cpu_version[i].name;
			break;
		}
	}

	if (name) {
		printf(": %s %d.%d", name, CPU_REV_MAJ(pvr), CPU_REV_MIN(pvr));
		snprintf(cpu_model, sizeof(cpu_model), "%s %d.%d",
		    name, CPU_REV_MAJ(pvr), CPU_REV_MIN(pvr));
	} else {
		printf(": Unknown, PVR 0x%x", pvr);
		strlcpy(cpu_model, "Unknown", sizeof(cpu_model));
	}

	node = faa->fa_node;
	clock_freq = OF_getpropint(node, "clock-frequency", 0);
	if (clock_freq != 0) {
		clock_freq /= 1000000; /* Hz to MHz */
		printf(", %u MHz", clock_freq);
	}

	iline = OF_getpropint(node, "i-cache-block-size", 128);
	dline = OF_getpropint(node, "d-cache-block-size", 128);
	level = 1;

	while (node) {
		const char *unit = "KB";
		uint32_t isize, iways;
		uint32_t dsize, dways;
		uint32_t cache;

		isize = OF_getpropint(node, "i-cache-size", 0) / 1024;
		iways = OF_getpropint(node, "i-cache-sets", 0);
		dsize = OF_getpropint(node, "d-cache-size", 0) / 1024;
		dways = OF_getpropint(node, "d-cache-sets", 0);

		/* Print large cache sizes in MB. */
		if (isize > 4096 && dsize > 4096) {
			unit = "MB";
			isize /= 1024;
			dsize /= 1024;
		}

		printf("\n%s:", dev->dv_xname);
		
		if (OF_getproplen(node, "cache-unified") == 0) {
			printf(" %d%s %db/line %d-way L%d cache",
			    isize, unit, iline, iways, level);
		} else {
			printf(" %d%s %db/line %d-way L%d I-cache",
			    isize, unit, iline, iways, level);
			printf(", %d%s %db/line %d-way L%d D-cache",
			    dsize, unit, dline, dways, level);
		}

		cache = OF_getpropint(node, "l2-cache", 0);
		node = OF_getnodebyphandle(cache);
		level++;
	}

	printf("\n");

	/* Update timebase frequency to reflect reality. */
	tb_freq = OF_getpropint(faa->fa_node, "timebase-frequency", tb_freq);
}